-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (113 loc) · 3.68 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import fs from 'fs'
import { execa } from 'execa'
import timeout from 'p-timeout'
import defer from 'p-defer'
import prettyBytes from 'pretty-bytes'
const PROTOCOL = '/transfer-test/1.0.0'
const TEST_TIMEOUT = Number(process.env.TIMEOUT ?? 600000000)
const dataLength = Number(process.env.DATA_LENGTH ?? (Math.pow(2, 20) * 100)) // how much data to send
const repeat = Number(process.env.TEST_REPEAT ?? 5)
// chunk sizes for data
const chunkSizes = []
// 256 b - 2 MiB - important to test over the multiplexer chunk size limit
for (let i = 8; i <= 21; i++) {
chunkSizes.push(Math.pow(2, i))
}
const versions = [
'0.36.x',
'0.37.x',
'0.38.x',
'0.39.x-mplex',
'0.39.x-yamux',
'0.40.x-mplex',
'0.40.x-yamux',
'0.45.x-mplex',
'0.45.x-yamux',
'0.46.x-mplex',
'0.46.x-yamux'
]
const results = {}
for (const version of versions) {
console.info(`testing ${version}`)
for (let i = 0; i < chunkSizes.length; i++) {
const chunkSize = chunkSizes[i]
results[chunkSize] = results[chunkSize] || []
let receiver
let sender
let time = Infinity
// try each one 5 times and take the lowest value
for (let attempt = 0; attempt < repeat; attempt++) {
try {
let result
let multiaddr
const multiaddrPromise = defer()
//console.info(`PROTOCOL=${PROTOCOL} DATA_LENGTH=${dataLength} CHUNK_SIZE=${chunkSize} node ./versions/${version}/receiver.js`)
receiver = execa('node', [`./versions/${version}/receiver.js`], {
env: {
DATA_LENGTH: dataLength,
PROTOCOL
}
})
receiver.stdout.on('data', (buf) => {
if (!multiaddr) {
multiaddrPromise.resolve(buf.toString().trim())
} else {
result = Number(buf.toString())
}
})
receiver.stderr.on('data', (buf) => {
console.error('receiver', buf.toString().trim())
})
multiaddr = await timeout(multiaddrPromise.promise, {
milliseconds: TEST_TIMEOUT
})
//console.info(`PROTOCOL=${PROTOCOL} DATA_LENGTH=${dataLength} CHUNK_SIZE=${chunkSize} RECEIVER_MULTIADDR=${multiaddr} node ./versions/${version}/sender.js`)
sender = execa('node', [`./versions/${version}/sender.js`], {
env: {
DATA_LENGTH: dataLength,
CHUNK_SIZE: chunkSize,
RECEIVER_MULTIADDR: multiaddr,
PROTOCOL
}
})
sender.stdout.on('data', (buf) => {
console.error('sender', buf.toString().trim())
})
sender.stderr.on('data', (buf) => {
console.error('sender', buf.toString().trim())
})
await timeout(receiver, {
milliseconds: TEST_TIMEOUT
})
// only take result if it is lower than previous tries
if (result < time) {
time = result
}
} catch (err) {
results[chunkSize].push('')
console.info(`${prettyBytes(dataLength)} in ${prettyBytes(chunkSize)} chunks - error:`, err.message)
} finally {
if (receiver != null) {
receiver.kill()
}
if (sender != null) {
sender.kill()
}
}
}
const megs = dataLength / Math.pow(2, 20)
const secs = time / 1000
console.info(`${prettyBytes(dataLength)} in ${prettyBytes(chunkSize)} chunks in ${time}ms at ${Math.round(megs / secs)}MB/s`)
results[chunkSize].push(time)
}
}
const csvData = [
[ 'chunk size', ...versions ].join(', ')
]
Object.entries(results).forEach(([chunkSize, averages]) => {
csvData.push([
chunkSize === '' ? '' : prettyBytes(Number(chunkSize)),
...averages
].join(', '))
})
fs.writeFileSync('results.csv', csvData.join('\r\n'))