Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const { setTimeout: delay } = require('node:timers/promises');
const path = require('node:path');
const { spawn } = require('node:child_process');
const assert = require('node:assert');

const autocannon = require('autocannon');
const Benchmark = require('benchmark');

const runner = {
autocannon: (opts) => {
Expand All @@ -15,6 +17,27 @@ const runner = {
requests: opts.http.routes,
})
},
benchmarkjs: (opts) => {
const suite = new Benchmark.Suite;

for (const operation of opts.operations) {
suite.add(operation.name, operation.fn);
}

return new Promise((resolve) => {
const results = [];
suite.on('cycle', function(event) {
results.push({
name: event.target.name,
opsSec: event.target.hz,
samples: event.target.cycles,
});
}).on('complete', function () {
resolve(results);
})
.run({ 'async': true });
})
},
}

const parser = {
Expand All @@ -28,6 +51,13 @@ const parser = {
errors: result.errors,
}
};
},
benchmarkjs: (settings, result) => {
return {
name: settings.name,
method: 'benchmarkjs',
operations: result,
}
}
}

Expand All @@ -54,11 +84,11 @@ function spawnServer(settings) {
}

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

let server = undefined;
if (settings.type === 'http') {
assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned');
server = spawnServer(settings);
// TODO: replace this workaround to use IPC to know when server is up
await delay(1000);
Expand All @@ -78,7 +108,10 @@ async function main() {
for (const file of files) {
if (file.match(/.*-benchmark\.js$/)) {
const bench = require(path.join(__dirname, './src/', file));
console.log(bench.name, 'results', await runBenchmark(bench));
// TODO(rafaelgss): possibly should be more accurate to run each benchmark in
// a separate worker
const result = await runBenchmark(bench)
console.log(bench.name, 'results', JSON.stringify(result, null, 2));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"dependencies": {
"fastify": "4.26.1"
"fastify": "4.26.1",
"lodash": "4.17.21"
},
"devDependencies": {
"autocannon": "7.15.0"
"autocannon": "7.15.0",
"benchmark": "2.1.4"
}
}
Loading