-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmarks.js
More file actions
77 lines (70 loc) · 2.39 KB
/
benchmarks.js
File metadata and controls
77 lines (70 loc) · 2.39 KB
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
"use strict";
const http = require('http');
var async = require('async');
const method = 'GET';
const num = 1000;
const concurrency = 100;
const keepAliveAgent = new http.Agent({ keepAlive: true });
const benchmarks = [];
function request(n, cb) {
const path = '/benchmark?len=256k';
const options = {
method,
path,
headers: { 'content-length': 0 },
host: '127.0.0.1',
port: 9000,
agent: keepAliveAgent
};
const req = http.request(options);
const ret = [];
var stats = {
'req#': n
};
const start = process.hrtime();
req.on('socket', (socket) => {
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
stats['socket'] = elapsed_ms;
/* to avoid "warning: possible EventEmitter memory leak detected. */
socket.setMaxListeners(0);
socket.on('connect', () => {
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
stats['connect'] = elapsed_ms;
});
socket.on('lookup', () => {
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
stats['dns'] = elapsed_ms;
});
socket.setNoDelay(true);
}).on('response', (res) => {
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
stats['response'] = elapsed_ms;
stats['data'] = [];
const contentLen = parseInt(res.headers['content-length'], 10);
let resLen = 0;
res.on('data', (data) => {
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
resLen += data.length;
stats['data'].push(elapsed_ms);
}).on('end', () => {
stats['contentMismatch'] = false;
if(contentLen !== resLen) {
stats['contentMismatch'] = true;
}
const end = process.hrtime(start);
const elapsed_ms = end[0] * 1000 + end[1] / 1000000;
stats['end'] = elapsed_ms;
benchmarks.push(stats);
cb();
});
}).end();
}
async.timesLimit(num, concurrency, request, function(err, res) {
benchmarks.sort((a, b) => { return a['req#'] - b['req#']; });
console.log(benchmarks)
});