Skip to content
Open
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ benchmarks.add(
{ setup }
);

benchmarks.add(
'asynchronous',
async (client, { histogram }) => {
const result = await lookup();
return result.name;
},
{ setup }
);


benchmarks.add(
'skippable',
(client, { histogram }) => histogram.observe(1, { a: 1, b: 1 }),
{
setup,
start: (event) => (event.target.name !== 'prom-client@10.1.3') // function not supported or broken in this version
}
);



benchmarks.run().catch(err => {
console.error(err.stack);
process.exit(1);
Expand Down
34 changes: 29 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,28 @@ function createRegressionBenchmark(baseModule, comparisonModules = []) {
for (const bucketName of Object.keys(buckets)) {
const bucket = buckets[bucketName];


for (const benchmarkName of Object.keys(bucket)) {
const { fn, opts: { setup, teardown } } = bucket[benchmarkName];
const { fn, opts: { setup, teardown, start = undefined } } = bucket[benchmarkName];

const fastest = { time: -Infinity };
const slowest = { time: +Infinity };

for (const testModule of testModules) {
const name = `${bucketName} ➭ ${benchmarkName} ➭ ${testModule.name}`;
const ctx = await Promise.resolve(setup(testModule.module));
const result = await new Promise((resolve, reject) => {
const bench = new benchmark.Benchmark(name, () => fn(testModule.module, ctx));
let ctx;

const result = await new Promise(async (resolve, reject) => {
const bench = new benchmark.Benchmark({
name,
defer: true,
fn: async (done) => {
try {
done.resolve(await fn(testModule.module, ctx));
} catch (err) {
done.resolve(err);
}
}
});

bench.on('complete', (event) => {
if (event.target.error) {
Expand Down Expand Up @@ -76,6 +86,20 @@ function createRegressionBenchmark(baseModule, comparisonModules = []) {
}
});


if (start !== undefined) {
// fake a start() call early
bench.on('start', start);
const cancelled = (bench.emit('start') === false);
bench.off('start', start);

if (cancelled) {
bench.emit('complete');
return;
}
}

ctx = await Promise.resolve(setup(testModule.module));
bench.run();
});

Expand Down
54 changes: 51 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const createRegressionBenchmark = require('..');

const benchmarks = createRegressionBenchmark(
Expand Down Expand Up @@ -29,11 +30,52 @@ benchmarks.add(
{ setup }
);

benchmarks.run().catch(err => {
console.error(err.stack);
process.exit(1);
const selftest = createRegressionBenchmark(
{}, []
);

selftest.suite('async support', (suite) => {
suite.add(
'async setup',
(client, { completed }) => assert(completed),
{ setup: asyncSetup },
);

suite.add(
'async fn',
(client, ctx) => {
assert(ctx.running !== true);
ctx.running = true;

return new Promise(resolve => setTimeout(() => {
ctx.running = false;
resolve();
}, 100));
},
{ setup: asyncSetup },
);

suite.add(
'start callback',
() => {
assert.fail('run should have been skipped');
},
{
setup: () => assert.fail('Setup should have been skipped'),
start: (event) => {
return (event.target.name !== 'async support ➭ start callback ➭ current');
}
},
);
});

benchmarks.run()
.then(() => selftest.run())
.catch(err => {
console.error(err.stack);
process.exit(1);
});

function setup(client) {
const registry = new client.Registry();

Expand All @@ -48,3 +90,9 @@ function setup(client) {

return {registry, histogram};
}

async function asyncSetup() {
await new Promise(resolve => setTimeout(resolve, 300));

return { completed: {} };
}