-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk6-ws-test.js
More file actions
60 lines (49 loc) · 1.59 KB
/
k6-ws-test.js
File metadata and controls
60 lines (49 loc) · 1.59 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
import ws from 'k6/ws';
import { check } from 'k6';
import { Rate, Counter, Gauge } from 'k6/metrics';
// Custom metrics
export const wsConnections = new Counter('ws_connections_made');
export const wsMessages = new Counter('ws_messages_received');
export const wsDataReceived = new Counter('ws_data_bytes');
export const wsActiveConnections = new Gauge('ws_active_connections');
export const wsSuccessfulSessions = new Rate('ws_successful_sessions');
export const options = {
scenarios: {
ws_load_test: {
executor: 'constant-vus',
vus: 10_000,
duration: '20s',
},
},
thresholds: {
ws_successful_sessions: ['rate>0.95'], // 95% successful sessions
},
};
export default function () {
const url = 'ws://localhost:3000/ws';
let sessionSuccess = false;
wsConnections.add(1);
const response = ws.connect(url, {}, function (socket) {
wsActiveConnections.add(1);
socket.on('message', data => {
wsMessages.add(1);
wsDataReceived.add(data.length);
sessionSuccess = true;
});
socket.on('close', () => {
wsActiveConnections.add(-1);
});
// set new bounding box
socket.setInterval(() => {
socket.send('0|10|10|45')
}, 5_000)
// simulate disconnecting clients
socket.setTimeout(() => {
socket.close();
}, 10_000);
});
check(response, {
'WebSocket connection successful': r => r && r.status === 101,
});
wsSuccessfulSessions.add(sessionSuccess ? 1 : 0);
}