-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbenchmark_receive_all.js
70 lines (54 loc) · 1.66 KB
/
benchmark_receive_all.js
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
// Build k6 with xk6-cable like this:
// xk6 build v0.38.3 --with github.com/anycable/[email protected]
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");
}
for(let i = 0; ; 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);
let incoming = channel.receiveAll(1);
for(let message of incoming) {
let received = message.__timestamp__ || Date.now();
if (message.action == "broadcast") {
broadcastsRcvd.add(1);
let ts = message.ts;
rttTrend.add(received - ts);
}
}
sleep(randomIntBetween(5, 10) / 100);
}
}