-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbenchmark_async_loop.js
More file actions
75 lines (58 loc) · 1.61 KB
/
benchmark_async_loop.js
File metadata and controls
75 lines (58 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Build k6 with xk6-cable like this:
// xk6 build v0.38.3 --with github.com/anycable/xk6-cable@v0.3.0
import { check, sleep, fail } from "k6";
import cable from "k6/x/cable";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js";
import { Trend, Counter } from "k6/metrics";
let rttTrend = new Trend("rtt", true);
let broadcastsRcvd = new Counter("broadcasts_rcvd");
let broadcastsSent = new Counter("broadcasts_sent");
let config = __ENV
config.URL = config.URL || "ws://localhost:8080/cable";
let url = config.URL;
let channelName = 'BenchmarkChannel';
export default function () {
let cableOptions = {
receiveTimeoutMs: 15000
}
let client = cable.connect(url, cableOptions);
if (
!check(client, {
"successful connection": (obj) => obj,
})
) {
fail("connection failed");
}
let channel = client.subscribe(channelName);
if (
!check(channel, {
"successful subscription": (obj) => obj,
})
) {
fail("failed to subscribe");
}
channel.ignoreReads();
channel.onMessage(message => {
let now = Date.now();
if (!message) {
return
}
if (message.action == "broadcast") {
broadcastsRcvd.add(1);
let ts = message.ts;
rttTrend.add(now - ts);
}
})
let i = 0;
client.loop(() => {
i++;
// Sampling
if (randomIntBetween(1, 10) > 8) {
let start = Date.now();
broadcastsSent.add(1);
// Create message via cable instead of a form
channel.perform("broadcast", { ts: start, content: `hello from ${__VU} numero ${i+1}` });
}
sleep(randomIntBetween(5, 10) / 100);
})
}