@jonahsnider/benchmark
Version:
A Node.js benchmarking library with support for multithreading and TurboFan optimization isolation.
51 lines • 1.77 kB
JavaScript
import { createHistogram, performance } from 'node:perf_hooks';
/**
* Tracks a function's execution in {@link (Test:class).histogram}.
*
* @example
* ```ts
* import { Test } from '@jonahsnider/benchmark';
*
* const test = new Test(() => 'a' + 'b');
* ```
*
* @public
*/
export class Test {
/** Execution times for this {@link (Test:class)}'s implementation. */
histogram = createHistogram();
#implementation;
/**
* Create a new {@link (Test:class)} with a given implementation.
*
* You probably don't want to instantiate this class directly, instead you can register tests with {@link (Suite:class).(addTest:2)}.
* You can also register {@link (Test:class)} instances with {@link (Suite:class).(addTest:1)}.
*
* @example
* ```ts
* const test = new Test(() => 'a' + 'b');
* ```
*
* @param implementation - The implementation function of the test
*/
constructor(implementation) {
this.#implementation = () => {
const startMs = performance.now();
const result = implementation();
const endMs = performance.now();
const durationNs = Math.round((endMs - startMs) * 1e6);
// Sometimes the duration is 0, seems like it's only when running on arm64 - see https://github.com/nodejs/node/issues/41641
this.histogram.record(durationNs || 1);
return result;
};
}
/**
* Runs this {@link (Test:class)}'s implementation once and records the execution time in {@link (Test:class).histogram}.
*
* @returns The return value of this {@link (Test:class)}'s implementation
*/
async run() {
return this.#implementation();
}
}
//# sourceMappingURL=test.js.map