-
Notifications
You must be signed in to change notification settings - Fork 18
/
benchmark.js
131 lines (123 loc) · 3.07 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const {urlToHttpOptions} = require('url');
const http2 = require('http2');
const https = require('https');
const http = require('http');
const Benchmark = require('benchmark');
const wrapper = require('./source/index.js');
// Configuration
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const destination = new URL('https://localhost:8081');
const destinationHTTP = new URL('http://localhost:8080');
// Benchmarking
const suite = new Benchmark.Suite();
const session = http2.connect(destination);
const wrapperSession = http2.connect(destination);
const destinationOptions = urlToHttpOptions(destination);
const destinationOptionsWithSession = {
...destinationOptions,
h2session: wrapperSession
};
const destinationHTTPOptions = urlToHttpOptions(destinationHTTP);
const httpsKeepAlive = {
agent: new https.Agent({keepAlive: true})
};
const autoHttpsKeepAlive = {
ALPNProtocols: ['http/1.1'],
agent: {
https: new https.Agent({keepAlive: true})
}
};
suite.add('http2-wrapper', {
defer: true,
fn: deferred => {
wrapper.get(destinationOptions, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
});
}
}).add('http2-wrapper - preconfigured session', {
defer: true,
fn: deferred => {
wrapper.get(destinationOptionsWithSession, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
});
},
onComplete: () => {
wrapperSession.close();
}
}).add('http2-wrapper - auto', {
defer: true,
fn: async deferred => {
(await wrapper.auto(destinationOptions, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
})).end();
}
}).add('http2', {
defer: true,
fn: deferred => {
const stream = session.request();
stream.resume();
stream.once('end', () => {
deferred.resolve();
});
}
}).add('https - auto - keepalive', {
defer: true,
fn: async deferred => {
(await wrapper.auto(destinationOptions, autoHttpsKeepAlive, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
})).end();
}
}).add('https - keepalive', {
defer: true,
fn: deferred => {
// If we use `destinationOpts` here, we get `socket hung up` errors for no reason <!>
https.get(destination, httpsKeepAlive, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
});
}
}).add('https', {
defer: true,
fn: deferred => {
// If we use `destinationOpts` here, we get `socket hung up` errors for no reason <!>
https.get(destination, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
});
}
}).add('http', {
defer: true,
fn: deferred => {
http.get(destinationHTTPOptions, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
});
}
}).on('cycle', event => {
console.log(String(event.target));
}).on('complete', function () {
console.log(`Fastest is ${this.filter('fastest').map('name')}`);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}).run({
async: false
});