-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbenchmark.js
109 lines (82 loc) · 3.22 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
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
const dns = require('dns')
const net = require('net')
const assert = require('assert')
const asyncLines = require('async-lines')
const memwatch = require('node-memwatch')
/* All MX records resolve to themselves */
dns.resolveMx = (hostname, cb) => cb(null, [{ exchange: hostname, priority: 10 }])
async function connectionHandler (connection) {
const lines = asyncLines(connection)
connection.write('220 Test\r\n')
for (let it = await lines.next(); it.done === false; it = await lines.next()) {
const line = it.value
if (line.startsWith('HELO')) connection.write('250 Hello\r\n')
if (line.startsWith('QUIT')) connection.end('221 Bye\r\n')
if (line.startsWith('RSET')) connection.write('250 Done\r\n')
if (line.startsWith('MAIL FROM')) connection.write('250 Okay\r\n')
if (line.startsWith('RCPT TO')) connection.write('250 Sure\r\n')
}
}
function createServer () {
return new Promise((resolve) => {
const server = net.createServer(connectionHandler)
server.listen(0, 'localhost', () => resolve({
close () { return server.close() },
get port () { return server.address().port }
}))
})
}
async function main () {
if (!process.argv[2] || !process.argv[3]) {
process.exitCode = 1
console.log('Usage: benchmark.js <server-count> <iterations>')
return
}
const serverCount = Number(process.argv[2])
const iterations = Number(process.argv[3])
let globalErrorCount = 0
let globalDoneCount = 0
let maxMemory = 0
memwatch.on('stats', (stats) => {
maxMemory = Math.max(maxMemory, stats.current_base)
})
console.time('main()')
console.log(`Running benchmark with ${serverCount} servers and ${iterations} iterations`)
console.log(`Starting ${serverCount} servers`)
const servers = await Promise.all(Array.from({ length: serverCount }, () => createServer()))
console.log(`${serverCount} servers started`)
if (global.gc) global.gc()
console.log(`${(process.memoryUsage().heapUsed / 1024 / 1024) | 0} MB memory used`)
const connect = net.connect
net.connect = (_, hostname) => {
const index = Number(hostname.replace('server-', ''))
return connect(servers[index].port, 'localhost')
}
const serverAcceptsEmail = require('./')
let wait = []
console.log(`Enqueueing ${iterations} tasks`)
for (let i = 0; i < iterations; i++) {
const index = (Math.random() * serverCount) | 0
const server = `server-${index}`
wait.push(serverAcceptsEmail(`hello@${server}`).then((result) => { assert.strictEqual(result, true); globalDoneCount++ }).catch(() => globalErrorCount++))
}
console.log(`${iterations} tasks enqueued`)
if (global.gc) global.gc()
console.log(`${(process.memoryUsage().heapUsed / 1024 / 1024) | 0} MB memory used`)
process.stdout.write('\n')
const printStatus = setInterval(() => {
process.stdout.write(`\rMax Memory: ${(maxMemory / 1024 / 1024) | 0} MB Error count: ${globalErrorCount} Done: ${(globalDoneCount / iterations * 100) | 0} %`)
}, 30)
try {
await Promise.all(wait)
} finally {
clearInterval(printStatus)
process.stdout.write('\n\n')
servers.map((server) => server.close())
console.timeEnd('main()')
}
}
main().catch((err) => {
process.exitCode = 1
console.log(err.stack)
})