I want to store my benchmarks.js results
Luciano Graziani
Posted on November 11, 2018
Cover image by Teresa Alba
This past week I've been searching for a library that generates a JSON for benchmark.js' results. Since I didn't find anything, I've written one myself.
By default, it stores everything in a file. But it has a callback function so you can do whatever you want, like storing it in a database.
It also uses systeminformation to store static data about the machine is running the benchmarks.
I think this lib is cool since it makes it possible to run your benchmarks in the CI pipeline and store the results in a database for future analysis.
So, how can I start using it today?
First, you need to install the dependencies:
yarn add benchmark benchmark-json-reporter
Or
npm install --save benchmark benchmark-json-reporter
Last, you have to create a file that will run your benchmarks:
const Benchmark = require('benchmark');
const jsonReporter = require('benchmark-json-reporter');
const suite = new Benchmark.Suite('my-bench-suite');
// Just this
jsonReporter(suite);
suite
.add('bench-name-1', () => {
// Faster heavy process
})
// ...
.add('bench-name-n', () => {
// Slower heavy process
})
// run async
.run({ async: true });
This basic example will store the results in the following file: <rootFolder>/benchmarks/my-bench-suite-({md5-hash}.log)
. The md5-hash is used to identify a machine univocally.
By the way, you can also store your benchmarks in a database like this:
const Benchmark = require('benchmark');
const jsonReporter = require('benchmark-json-reporter');
const suite = new Benchmark.Suite('my-bench-suite');
// Just this
jsonReporter(suite, {
callback(result, hashId, name, folder) {
// 1. Connect to a database
const connection = new SomeEndPoint();
// 2. Store the sysinfo with the hashId as a main ID
connection
.getById(hashId)
.update({ sysinfo: result.sysinfo })
.then(() =>
// 3. Store the benchmarks
Promise.all(
benchs.map(bench =>
// For each benchmark, push the result into the collection
connection
.getById(hashId)
.getProp('benchmarks')
.getCollection(bench.timestamp).push(bench),
)
)
).then(() => {
// 4. Close the database connection
connection.close();
});
// 5. Profit.
},
});
suite
.add('bench-name-1', () => {
// Faster heavy process
})
// ...
.add('bench-name-n', () => {
// Slower heavy process
})
// run async
.run({ async: true });
Thank you for reading! And if you have any question, don't hesitate to ask!
Posted on November 11, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.