-
Notifications
You must be signed in to change notification settings - Fork 0
/
wspromised.js
48 lines (44 loc) · 1.33 KB
/
wspromised.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
const WebSocket = require('websocket-as-promised');
const BufferPlus = require('buffer-plus');
const W3CWebSocket = require('websocket').w3cwebsocket;
const NS_PER_SEC = 1e9;
const MS_PER_NS = 1e-6;
let start;
let end;
const testTimes = 1000000;
const endTime = testTimes - 1;
const client = new WebSocket('ws://127.0.0.1:9001', {
createWebSocket: url => new W3CWebSocket(url)
});
let c = 0;
const sent = (number) => {
const rep = BufferPlus.create(10);
rep.writeUInt8(number);
client.send(rep.toBuffer());
};
client.open()
.then(() => {
console.log('W3Cwebsocket client start...');
start = process.hrtime();
for (let index = 0; index < testTimes; index++) {
sent(0);
}
});
client.onMessage.addListener((message) => {
c++;
if (c === endTime) {
sent(0x2);
end = process.hrtime(start);
console.log('W3Cwebsocket client done');
const ops = Number(c / ((end[0] * NS_PER_SEC + end[1]) * MS_PER_NS) * 1000.0).toFixed(2);
console.log(`Avg. message/sec: ${ops}/s`);
console.log(`Benchmark ${testTimes} messages took ${(end[0] * NS_PER_SEC + end[1]) * MS_PER_NS} milliseconds`);
process.exit(0);
}
});
// client.on('error', (error) => {
// console.error('error', error);
// });
// client.on('close', (close) => {
// console.error('close', close);
// });