|
1 | 1 | const fs = require('node:fs/promises'); |
2 | | -const { setTimeout: delay } = require('node:timers/promises'); |
3 | 2 | const path = require('node:path'); |
4 | | -const { spawn } = require('node:child_process'); |
5 | | -const assert = require('node:assert'); |
| 3 | +const Piscina = require('piscina'); |
6 | 4 |
|
7 | | -const autocannon = require('autocannon'); |
8 | | -const Benchmark = require('benchmark'); |
9 | | - |
10 | | -const runner = { |
11 | | - autocannon: (opts) => { |
12 | | - return autocannon({ |
13 | | - url: `http://localhost:${opts.http.serverPort}`, |
14 | | - connections: 100, |
15 | | - pipelining: 1, |
16 | | - duration: 10 * opts.http.routes.length, |
17 | | - requests: opts.http.routes, |
18 | | - }) |
19 | | - }, |
20 | | - benchmarkjs: (opts) => { |
21 | | - const suite = new Benchmark.Suite; |
22 | | - |
23 | | - for (const operation of opts.operations) { |
24 | | - suite.add(operation.name, operation.fn); |
25 | | - } |
26 | | - |
27 | | - return new Promise((resolve) => { |
28 | | - const results = []; |
29 | | - suite.on('cycle', function(event) { |
30 | | - results.push({ |
31 | | - name: event.target.name, |
32 | | - opsSec: event.target.hz, |
33 | | - samples: event.target.cycles, |
34 | | - }); |
35 | | - }).on('complete', function () { |
36 | | - resolve(results); |
37 | | - }) |
38 | | - .run({ 'async': false }); |
39 | | - }) |
40 | | - }, |
41 | | -} |
42 | | - |
43 | | -const parser = { |
44 | | - autocannon: (settings, result) => { |
45 | | - return { |
46 | | - name: settings.name, |
47 | | - method: 'autocannon', |
48 | | - http: { |
49 | | - totalReq: asNumber(result.requests), |
50 | | - duration: result.duration, |
51 | | - errors: result.errors, |
52 | | - } |
53 | | - }; |
54 | | - }, |
55 | | - benchmarkjs: (settings, result) => { |
56 | | - return { |
57 | | - name: settings.name, |
58 | | - method: 'benchmarkjs', |
59 | | - operations: result, |
60 | | - } |
61 | | - } |
62 | | -} |
63 | | - |
64 | | -const ALLOWED_BENCHMARKER = ['autocannon', 'benchmarkjs']; |
65 | | - |
66 | | -function asNumber (stat) { |
67 | | - const result = Object.create(null) |
68 | | - for (const k of Object.keys(stat)) { |
69 | | - result[k] = stat[k].toLocaleString(undefined, { |
70 | | - // to show all digits |
71 | | - maximumFractionDigits: 20 |
72 | | - }) |
73 | | - } |
74 | | - return result |
75 | | -} |
76 | | - |
77 | | -function spawnServer(settings) { |
78 | | - const server = spawn( |
79 | | - process.execPath, |
80 | | - [settings.http.server], |
81 | | - { stdio: 'inherit' }, |
82 | | - ); |
83 | | - return server; |
84 | | -} |
85 | | - |
86 | | -async function runBenchmark(settings) { |
87 | | - assert.ok(ALLOWED_BENCHMARKER.includes(settings.benchmarker), 'Invalid settings.benchmarker'); |
88 | | - |
89 | | - let server = undefined; |
90 | | - if (settings.type === 'http') { |
91 | | - assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned'); |
92 | | - server = spawnServer(settings); |
93 | | - // TODO: replace this workaround to use IPC to know when server is up |
94 | | - await delay(1000); |
95 | | - } |
96 | | - |
97 | | - const benchRunner = runner[settings.benchmarker]; |
98 | | - const benchParser = parser[settings.benchmarker]; |
99 | | - const results = await benchRunner(settings); |
100 | | - if (server) { |
101 | | - server.kill('SIGINT'); |
102 | | - } |
103 | | - return benchParser(settings, results); |
104 | | -} |
| 5 | +const piscina = new Piscina({ |
| 6 | + filename: path.resolve(__dirname, 'worker.js') |
| 7 | +}); |
105 | 8 |
|
106 | 9 | async function main() { |
107 | 10 | const files = await fs.readdir(path.join(__dirname, './src')); |
108 | 11 | for (const file of files) { |
109 | 12 | if (file.match(/.*-benchmark\.js$/)) { |
110 | | - const bench = require(path.join(__dirname, './src/', file)); |
111 | | - // TODO(rafaelgss): possibly should be more accurate to run each benchmark in |
112 | | - // a separate worker |
113 | | - const result = await runBenchmark(bench) |
114 | | - console.log(bench.name, 'results', JSON.stringify(result, null, 2)); |
| 13 | + const benchFile = path.join(__dirname, './src/', file); |
| 14 | + const result = await piscina.run(benchFile); |
| 15 | + console.log('results', JSON.stringify(result, null, 2)); |
115 | 16 | } |
116 | 17 | console.log('Done...', file) |
117 | 18 | } |
|
0 commit comments