Skip to content

Commit 1b7e92e

Browse files
authored
feat: add lodash to benchmark (#2)
1 parent 4e63366 commit 1b7e92e

File tree

4 files changed

+221
-121
lines changed

4 files changed

+221
-121
lines changed

index.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const { setTimeout: delay } = require('node:timers/promises');
33
const path = require('node:path');
44
const { spawn } = require('node:child_process');
55
const assert = require('node:assert');
6+
67
const autocannon = require('autocannon');
8+
const Benchmark = require('benchmark');
79

810
const runner = {
911
autocannon: (opts) => {
@@ -15,6 +17,27 @@ const runner = {
1517
requests: opts.http.routes,
1618
})
1719
},
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': true });
39+
})
40+
},
1841
}
1942

2043
const parser = {
@@ -28,6 +51,13 @@ const parser = {
2851
errors: result.errors,
2952
}
3053
};
54+
},
55+
benchmarkjs: (settings, result) => {
56+
return {
57+
name: settings.name,
58+
method: 'benchmarkjs',
59+
operations: result,
60+
}
3161
}
3262
}
3363

@@ -54,11 +84,11 @@ function spawnServer(settings) {
5484
}
5585

5686
async function runBenchmark(settings) {
57-
assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned');
5887
assert.ok(ALLOWED_BENCHMARKER.includes(settings.benchmarker), 'Invalid settings.benchmarker');
5988

6089
let server = undefined;
6190
if (settings.type === 'http') {
91+
assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned');
6292
server = spawnServer(settings);
6393
// TODO: replace this workaround to use IPC to know when server is up
6494
await delay(1000);
@@ -78,7 +108,10 @@ async function main() {
78108
for (const file of files) {
79109
if (file.match(/.*-benchmark\.js$/)) {
80110
const bench = require(path.join(__dirname, './src/', file));
81-
console.log(bench.name, 'results', await runBenchmark(bench));
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));
82115
}
83116
}
84117
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"dependencies": {
3-
"fastify": "4.26.1"
3+
"fastify": "4.26.1",
4+
"lodash": "4.17.21"
45
},
56
"devDependencies": {
6-
"autocannon": "7.15.0"
7+
"autocannon": "7.15.0",
8+
"benchmark": "2.1.4"
79
}
810
}

0 commit comments

Comments
 (0)