-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
54 lines (45 loc) · 1.04 KB
/
benchmark.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
'use strict'
const Channel = require('./')
const { PassThrough } = require('stream')
const mode = process.argv[2] || 'channel'
const highWatermark = process.argv[3] || 16384
const bytes = {}
const iter = mode === 'channel'
? new Channel()
: new PassThrough({ highWatermark })
function uid () {
return Buffer.from(Math.random().toString(35).substr(2, 16))
}
const id = uid()
async function consume (name) {
bytes[name] = 0
for await (let chunk of iter) {
bytes[name] += chunk.length
}
}
const produce = mode === 'channel'
? async () => {
let bytes = 0
while (true) {
bytes += id.length
const p = iter.give(id)
if (bytes >= highWatermark) {
bytes = 0
await p
}
}
}
: () => {
while (iter.write(id));
iter.once('drain', () => setImmediate(produce))
}
Promise.all([
produce(),
consume(mode)
]).catch(console.error)
setInterval(() => {
for (let [ key, value ] of Object.entries(bytes)) {
console.log(`${key} - ${value} bytes/sec`)
bytes[key] = 0
}
}, 1000)