UNPKG

iso-bench

Version:

Small benchmark library focused in avoiding optimization/deoptimization pollution between tests by isolating them.

56 lines (55 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StaticStream = void 0; const Utils_1 = require("./Utils"); class StaticStream { _stream; _padding = 0; _groups = new Map(); constructor(_stream) { this._stream = _stream; } initialize(bench, tests) { for (const test of tests) { const group = this._groups.get(test.group); if (group) { group.tests.push(test); } else { this._groups.set(test.group, { name: test.group, tests: [test], started: 0, ended: 0 }); } } this._padding = Math.max(...tests.map(test => test.name.length)); this._stream.write(`[ISOBENCH INITIALIZED] ${bench.name}\n`); } end(test) { const group = this._groups.get(test.group); if (group) { group.ended++; if (group.ended === group.tests.length) { this._completedGroup(group); } } } _completedGroup(group) { if (this._groups.size > 1 || group.name) { this._stream.write(`[GROUP COMPLETED] ${group.name}\n`); } const ops = group.tests.map(test => test.opMs); const min = Math.min(...ops.filter(n => !!n)); const max = Math.max(...ops.filter(n => !!n)); for (const test of group.tests) { const logArgs = (0, Utils_1.getTestLog)(this._padding, test, { min, max }, false); this._stream.write(logArgs.join(" ") + "\n"); } } completed() { this._stream.write("[ISOBENCH COMPLETED]\n"); } } exports.StaticStream = StaticStream;