-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.mjs
52 lines (46 loc) 路 1.31 KB
/
http.mjs
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
/* eslint-disable no-console */
import autocannon from 'autocannon'; // https://github.com/mcollina/autocannon
import { listen } from 'listhen';
import {
printEnv,
benchSets,
printStats,
logSection,
router,
} from './utils.mjs';
async function main() {
printEnv();
for (const bench of benchSets) {
logSection(`Benchmark: ${bench.title}`);
const { listener, stats } = await createServer();
const instance = autocannon({
url: listener.url,
requests: bench.requests,
});
autocannon.track(instance);
process.once('SIGINT', () => {
instance.stop();
listener.close();
process.exit(1);
});
await instance; // Resolves to details results
printStats(stats);
await listener.close();
}
}
main().catch(console.error);
async function createServer() {
const stats = {};
const listener = await listen(
(req, res) => {
stats[req.url] = (stats[req.url] || 0) + 1;
const match = router.lookup(req.url);
if (!match) {
stats[_nomatch] = (stats[_nomatch] || 0) + 1;
}
res.end(JSON.stringify(match || { error: 404 }));
},
{ showURL: false }
);
return { listener, stats };
}