Skip to content

Commit b002a6f

Browse files
committed
add k6 load test example
1 parent b59425b commit b002a6f

File tree

8 files changed

+86
-55
lines changed

8 files changed

+86
-55
lines changed

.env.example

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
SANDBOX_MODE=false
2-
UPLOADTHING_SECRET=sk_live_xxxxxx
3-
UPLOADTHING_APP_ID=xxxxxxx
4-
5-
POSTGRES_USER="postgres"
6-
POSTGRES_PASSWORD="postgres"
7-
POSTGRES_DATABASE="postgres"
8-
POSTGRES_HOST="postgres"
9-
POSTGRES_PRISMA_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DATABASE}?schema=public"
10-
POSTGRES_URL_NON_POOLING="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DATABASE}?schema=public"
1+
# -------------------
2+
# Optional environment variables - uncomment to use
3+
# -------------------
4+
5+
#SANDBOX_MODE=false # true or false - if true, the app will use the sandbox mode, which disables resetting the database and other features
6+
#NEXT_PUBLIC_URL="http://yourdomain.com" # When using advanced deployment, this is required. Set to the domain name of your app
7+
#NEXT_PUBLIC_DISABLE_ANALYTICS=true # true or false - if true, the app will not send anonymous analytics data to the server
8+
#INSTALLATION_ID="your-app-name" # A unique identifier for your app, used for analytics. Generated automatically if not set.
9+
10+
# -------------------
11+
# Required environment variables
12+
# -------------------
13+
14+
UPLOADTHING_SECRET=sk_live_xxxxxx # Your UploadThing secret key
15+
UPLOADTHING_APP_ID=xxxxxxx # Your UploadThing app ID
16+
17+
POSTGRES_USER="postgres" # Your PostgreSQL username
18+
POSTGRES_PASSWORD="postgres" # Your PostgreSQL password
19+
POSTGRES_DATABASE="postgres" # Your PostgreSQL database name
20+
POSTGRES_HOST="postgres" # Your PostgreSQL host
21+
POSTGRES_PRISMA_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DATABASE}?schema=public" # A pooled connection URL for Prisma.
22+
POSTGRES_URL_NON_POOLING="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DATABASE}?schema=public" # A non-pooling connection URL for Prisma

load-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { check, group, sleep } from 'k6';
2+
import http, { CookieJar } from 'k6/http';
3+
4+
const HOST = 'host.docker.internal:3000';
5+
6+
export let options = {
7+
stages: [
8+
{ duration: '30s', target: 100 }, // Ramp-up to 150 users over 30 seconds.
9+
{ duration: '1m', target: 100 }, // Stay at 150 users for 1 minute.
10+
{ duration: '30s', target: 0 }, // Ramp down to 0 users over 10 seconds.
11+
],
12+
};
13+
14+
export default function () {
15+
let loginRes = login();
16+
check(loginRes, {
17+
'login successful': (r) => r.status === 200,
18+
});
19+
20+
if (loginRes.status === 200) {
21+
22+
let jar = new CookieJar();
23+
jar.set(`http://${HOST}`, 'auth_session', loginRes.cookies['auth_session'][0].value);
24+
25+
group('Authenticated requests', function () {
26+
let res = http.get(`http://${HOST}/dashboard`, { cookies: jar.cookiesForURL(`http://${HOST}`) });
27+
check(res, {
28+
'is status 200': (r) => r.status === 200,
29+
});
30+
31+
let res2 = http.get(`http://${HOST}/dashboard/settings`, { cookies: jar.cookiesForURL(`http://${HOST}`) });
32+
check(res2, {
33+
'is status 200': (r) => r.status === 200,
34+
});
35+
});
36+
}
37+
38+
sleep(1);
39+
}
40+
41+
function login() {
42+
let payload = '[{"username":"admin","password":"Administrator1!"}]'
43+
44+
let params = {
45+
headers: {
46+
'Next-Action': '36b840346a44c93083679ff0264912dba5c1443f', // No way to get this automatically, I don't think...
47+
'Content-Type': 'text/plain;charset=UTF-8',
48+
},
49+
};
50+
51+
return http.post(`http://${HOST}/signin`, payload, params);
52+
}

middleware.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

middlewares/stackMiddleware.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

middlewares/withErrorHandler.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"start": "next start",
1515
"vercel-build": "node ./handle-migrations.js && next build",
1616
"knip": "knip",
17-
"test": "vitest"
17+
"test": "vitest",
18+
"load-test": "docker run -i grafana/k6 run - <load-test.js"
1819
},
1920
"pnpm": {
2021
"overrides": {
@@ -135,6 +136,7 @@
135136
"eslint-config-prettier": "^9.1.0",
136137
"jest": "^29.7.0",
137138
"jsdom": "^24.0.0",
139+
"k6": "^0.0.0",
138140
"knip": "^5.13.0",
139141
"postcss": "^8.4.38",
140142
"prettier": "^3.2.5",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/getBaseUrl.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { env } from '~/env.mjs';
22

33
export default function getBaseUrl() {
4-
if (typeof window !== 'undefined')
5-
// browser should use relative path
6-
return `${window.location.protocol}//${window.location.host}`;
7-
84
if (env.VERCEL_URL)
95
// reference for vercel.com
106
return `https://${env.VERCEL_URL}`;
@@ -14,5 +10,6 @@ export default function getBaseUrl() {
1410
return env.NEXT_PUBLIC_URL;
1511

1612
// assume localhost
13+
console.log('caught');
1714
return `http://127.0.0.1:3000`;
1815
}

0 commit comments

Comments
 (0)