Skip to content

Commit b31b3c8

Browse files
committed
Fix k6 load test.
1 parent 2a0d951 commit b31b3c8

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

k6/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ This is a simple load test based on k6 (k6.io).
55
To run:
66

77
1. [Install k6](https://k6.io/docs/getting-started/installation/)
8-
2. `k6 run --vus=20 --duration=60s -e ROOM_ID=r2 -e k6/test.js`
8+
2. `k6 run --vus=20 --duration=10s -e ROOM_ID=r2 test.js`
99

1010
The `--vus` flag controls the number of "virtual user" that will be simulated.

k6/test.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ function randomID() {
1414

1515
let lastMutationID = 0;
1616

17-
function sendMutation(socket, name, args) {
17+
function sendMutation(socket, clientID, name, args) {
1818
const mutation = {
1919
id: ++lastMutationID,
2020
name,
2121
args,
2222
timestamp: Date.now(),
2323
};
2424
const pushBody = {
25+
clientID,
2526
mutations: [mutation],
2627
pushVersion: 1,
2728
schemaVersion: "",
@@ -33,14 +34,14 @@ function sendMutation(socket, name, args) {
3334
}
3435

3536
function createShape(socket, clientID, idx) {
36-
sendMutation(socket, "createShape", {
37+
sendMutation(socket, clientID, "createShape", {
3738
id: `${clientID}-${idx}`,
3839
shape: randomShape(),
3940
});
4041
}
4142

4243
function scanShape(socket, clientID, idx) {
43-
sendMutation(socket, "scanShape", {
44+
sendMutation(socket, clientID, "scanShape", {
4445
id: `${clientID}-${idx}`,
4546
dx: 1,
4647
maxX: 500,
@@ -63,14 +64,18 @@ export default function () {
6364
const clientID = randomID();
6465
const userID = randomID();
6566

66-
const socketBaseURL = "ws://127.0.0.1:8787/connect";
67-
const url = `${socketBaseURL}?clientID=${clientID}&roomID=${roomID}&baseCookie=0&ts=${Date.now()}`;
67+
// Note: it looks like wrangler only listens on ipv6!
68+
// https://github.com/cloudflare/wrangler/issues/1198#issuecomment-1204690449
69+
const socketBaseURL = "ws://[::1]:8787/connect";
70+
const url = `${socketBaseURL}?clientID=${clientID}&roomID=${roomID}&baseCookie=0&lmid=0&ts=${Date.now()}`;
6871
const params = {
6972
headers: {
70-
"Sec-WebSocket-Protocol": JSON.stringify({
71-
roomID,
72-
userID,
73-
}),
73+
"Sec-WebSocket-Protocol": encodeURIComponent(
74+
JSON.stringify({
75+
roomID,
76+
userID,
77+
})
78+
),
7479
},
7580
};
7681

@@ -101,9 +106,9 @@ export default function () {
101106
});
102107

103108
socket.setTimeout(function () {
104-
console.log("2 seconds passed, closing the socket");
109+
console.log("test done, closing the socket");
105110
socket.close();
106-
}, 30000);
111+
}, 5000);
107112
});
108113

109114
check(response, { "status is 101": (r) => r && r.status === 101 });

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"dev": "next dev",
77
"dev-worker": "wrangler dev --local --experimental-enable-local-persistence",
8-
"build": "rm -rf dist && next build && npm run build-worker",
8+
"build": "rm -rf dist && next build",
99
"start": "next start",
1010
"format": "prettier --write './**/*.{js,jsx,json,ts,tsx,html,css,md}'",
1111
"check-types": "tsc -p tsconfig.json --noEmit && tsc -p worker/tsconfig.json --noEmit"

0 commit comments

Comments
 (0)