Skip to content

Commit 60cd5b6

Browse files
committed
Add some metrics
1 parent b31b3c8 commit 60cd5b6

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

k6/test.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import ws from "k6/ws";
22
import { check } from "k6";
3+
import { Trend } from "k6/metrics";
34

45
const numShapesPerClient = __ENV.SHAPES_PER_CLIENT || 1;
56
const roomID = __ENV.ROOM_ID;
67

8+
// e.g.: wss://replidraw.replicache.workers.dev/connect or ws://[::1]:8787/connect
9+
// Note: it looks like wrangler only listens on ipv6!
10+
// https://github.com/cloudflare/wrangler/issues/1198#issuecomment-1204690449
11+
const socketBaseURL = __ENV.SOCKET_BASE_URL || "ws://[::1]:8787/connect";
12+
713
if (!roomID) {
814
throw new Error("Must specify a ROOM_ID env variable");
915
}
@@ -12,22 +18,29 @@ function randomID() {
1218
return Math.random().toString(36).substring(2);
1319
}
1420

15-
let lastMutationID = 0;
21+
const sentMutations = [];
22+
const receivedPokes = [];
23+
const pokeWaitTrend = new Trend("poke_wait_time");
1624

1725
function sendMutation(socket, clientID, name, args) {
26+
const lastMutation = sentMutations[sentMutations.length - 1];
27+
const lmid = lastMutation ? lastMutation.id : 0;
28+
const id = lmid + 1;
29+
const ts = Date.now();
1830
const mutation = {
19-
id: ++lastMutationID,
31+
id,
2032
name,
2133
args,
22-
timestamp: Date.now(),
34+
timestamp: ts,
2335
};
2436
const pushBody = {
2537
clientID,
2638
mutations: [mutation],
2739
pushVersion: 1,
2840
schemaVersion: "",
29-
timestamp: Date.now(),
41+
timestamp: ts,
3042
};
43+
sentMutations.push({ id, ts });
3144
const msg = JSON.stringify(["push", pushBody]);
3245
console.info("sending", msg);
3346
socket.send(msg);
@@ -64,9 +77,6 @@ export default function () {
6477
const clientID = randomID();
6578
const userID = randomID();
6679

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";
7080
const url = `${socketBaseURL}?clientID=${clientID}&roomID=${roomID}&baseCookie=0&lmid=0&ts=${Date.now()}`;
7181
const params = {
7282
headers: {
@@ -92,6 +102,25 @@ export default function () {
92102
socket.setInterval(() => {
93103
scanShapes(socket, clientID, numShapesPerClient);
94104
}, 16);
105+
} else {
106+
const lastPoke = receivedPokes[receivedPokes.length - 1];
107+
const lastLMID = lastPoke ? lastPoke.id : -1;
108+
const id = body.lastMutationID;
109+
const ts = Date.now();
110+
for (const m of sentMutations.reverse()) {
111+
if (m.id <= id) {
112+
pokeWaitTrend.add(ts - m.ts);
113+
break;
114+
}
115+
}
116+
check(type, {
117+
"message type was poke": (res) => res === "poke",
118+
});
119+
check(body, {
120+
"body is an object": (res) => typeof res === "object",
121+
"received correct lmid": (res) => id >= lastLMID + 1,
122+
});
123+
receivedPokes.push({ id, ts: Date.now() });
95124
}
96125
});
97126

0 commit comments

Comments
 (0)