benchmark-suite
Version:
A simple suite of benchmarking tests
96 lines (95 loc) • 3.09 kB
JavaScript
// var heapdump = require('heapdump');
// const pify = require('pify');
import gc from 'expose-gc';
import humanize from 'pretty-bytes';
import Stats from 'stats-accumulator';
// const writeSnapshot = pify(heapdump.writeSnapshot);
const writeSnapshot = async (_name)=>{};
let MemoryTest = class MemoryTest {
async run(options = {}) {
const time = options.time;
await this.callibrate(options);
const startTime = Date.now();
const results = {
end: {
name: this.name,
stats: new Stats()
},
delta: {
name: this.name,
stats: new Stats()
}
};
do {
const run = await this.runOnce(options);
results.end.stats.update(run.end);
results.delta.stats.update(run.delta.max);
}while (Date.now() - startTime <= time)
return results;
}
async callibrate(options) {
const dump = options.heapdumpTrigger && !options.heapdumped;
let dumped = false;
let stats = new Stats();
while(stats.n < 5){
gc();
const start = process.memoryUsage().heapUsed;
await this.fn(()=>{});
if (dump && !dumped) {
dumped = true;
await writeSnapshot('hd-calibrate.heapsnapshot');
gc();
}
gc();
const delta = process.memoryUsage().heapUsed - start;
if (delta < 0) stats = new Stats();
else stats.update(delta);
}
}
async runOnce(options = {}) {
const now = Date.now();
const stats = new Stats();
this.n++;
let dumped = false;
const dump = options.heapdumpTrigger && !options.heapdumped;
if (dump) {
await writeSnapshot(`hd-${this.name}-${now}-start.heapsnapshot`);
gc();
}
gc();
const start = process.memoryUsage().heapUsed;
await this.fn(async ()=>{
gc();
const delta = process.memoryUsage().heapUsed - start;
stats.update(delta);
if (dump && !dumped && delta > options.heapdumpTrigger) {
dumped = true;
options.heapdumped = true;
await writeSnapshot(`hd-${this.name}-${now}-triggered.heapsnapshot`);
gc();
}
});
gc();
const delta = process.memoryUsage().heapUsed - start;
if (dump) {
await writeSnapshot(`hd-${this.name}-${now}-end.heapsnapshot`);
gc();
}
return {
end: delta,
delta: stats
};
}
metric(stats) {
return stats.mean;
}
formatStats(stats) {
const memoryStdev = Math.sqrt(stats.variance() / stats.mean) / 100;
return `${humanize(stats.mean)} ±${memoryStdev.toFixed(1)}% (${stats.n} runs sampled)`;
}
constructor(name, fn){
this.name = name;
this.fn = fn;
}
};
export { MemoryTest as default };