-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench.js
79 lines (77 loc) · 1.89 KB
/
bench.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
71
72
73
74
75
76
77
78
79
const evnet = require('.')
const rndPort = () => 9300 + Math.round(Math.random() * 1000)
const assert = require('assert')
const tests = [
{
name: 'Send String',
fn: count => new Promise(resolve => {
const port = rndPort()
const server = evnet.start('0.0.0.0', port)
const e = evnet('0.0.0.0', port)
let counter = 0
e.on('foo', data => {
counter += 1
if (counter >= count) {
server.close()
e.close()
resolve()
}
})
for (let i = 0; i < count; i++) {
e.emit('foo', 'bar')
}
})
},
{
name: 'Send Object',
fn: count => new Promise(resolve => {
const port = rndPort()
const server = evnet.start('0.0.0.0', port)
const e = evnet('0.0.0.0', port)
let counter = 0
e.on('foo', data => {
assert.deepStrictEqual(data, { a: 1, b: 2, c: 3 })
counter += 1
if (counter >= count) {
server.close()
e.close()
resolve()
}
})
for (let i = 0; i < count; i++) {
e.emit('foo', { a: 1, b: 2, c: 3 })
}
})
}
,
{
name: 'Lots of `on`',
fn: count => new Promise(resolve => {
const port = rndPort()
const server = evnet.start('0.0.0.0', port)
const e = evnet('0.0.0.0', port)
let counter = 0
for (let i = 0; i < count; i++) {
e.on('foo', data => {
assert.deepStrictEqual(data, { a: 1, b: 2, c: 3 })
counter += 1
if (counter >= count) {
e.close()
server.close()
resolve()
}
})
}
e.emit('foo', { a: 1, b: 2, c: 3 })
})
}
]
const main = async () => {
const runs = process.env.COUNT || 10000
for (let test of tests) {
const startTime = Date.now()
await test.fn(runs)
console.log(`${Date.now() - startTime}ms ${test.name}`)
}
}
main()