iso-bench
Version:
Small benchmark library focused in avoiding optimization/deoptimization pollution between tests by isolating them.
120 lines (119 loc) • 4.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.DynamicStream = exports.TestOutput = void 0;
const Utils_1 = require("./Utils");
const TTYOutput_1 = require("./TTYOutput");
class TestOutput {
_tty;
line;
constructor(_tty, line) {
this._tty = _tty;
this.line = line;
}
log(data) {
this._tty.log(data, this.line);
}
}
exports.TestOutput = TestOutput;
class DynamicStream {
_stream;
_padding = 0;
_outputs = new Map;
_header;
_tty;
_benchName = "";
_groups = new Map();
constructor(_stream) {
this._stream = _stream;
this._tty = new TTYOutput_1.TTYOutput(this._stream);
this._header = new TestOutput(this._tty, 0);
}
initialize(bench, tests) {
let firstGroupName = "";
for (const test of tests) {
const group = this._groups.get(test.group);
if (group) {
group.tests.push(test);
}
else {
firstGroupName = test.group;
this._groups.set(test.group, {
name: test.group,
tests: [test],
started: 0,
ended: 0
});
}
}
this._benchName = bench.name;
this._padding = Math.max(...tests.map(test => test.name.length));
let line = 1;
this._header.log(`${"\u001B[33m"}[ISOBENCH INITIALIZED]${"\u001B[0m"} ${this._benchName}`);
for (const group of this._groups.values()) {
if (this._groups.size > 1 || group.name) {
group.output = new TestOutput(this._tty, line++);
group.output.log(`${"\u001B[30m"}[GROUP PAUSED]${"\u001B[0m"} ${group.name}`);
}
for (const test of group.tests) {
const output = new TestOutput(this._tty, line++);
output.log(`${test.name.padEnd(this._padding, " ")} - ${"\u001B[30m"}Paused${"\u001B[0m"}`);
this._outputs.set(test.index, output);
}
}
}
start(test) {
const group = this._groups.get(test.group);
if (group) {
group.started++;
if (group.started === 1 && group.output) {
group.output.log(`${"\u001B[33m"}[GROUP INITIALIZED]${"\u001B[0m"} ${group.name}`);
}
}
const output = this._outputs.get(test.index);
if (output) {
output.log(`${test.name.padEnd(this._padding, " ")} - ${"\u001B[33m"}Running...${"\u001B[0m"}`);
}
}
sample(test, sample) {
const output = this._outputs.get(test.index);
if (output) {
const logArgs = (0, Utils_1.getTestLog)(this._padding, test, null, true, sample);
logArgs.push(`${"\u001B[33m"}Running...${"\u001B[0m"}`);
output.log(logArgs.join(" "));
}
}
end(test) {
const output = this._outputs.get(test.index);
if (output) {
const logArgs = (0, Utils_1.getTestLog)(this._padding, test, null, true);
output.log(logArgs.join(" "));
}
const group = this._groups.get(test.group);
if (group) {
group.ended++;
if (group.ended === group.tests.length) {
this._completedGroup(group);
}
}
}
_completedGroup(group) {
if (group.output) {
group.output.log(`${"\u001B[32m"}[GROUP ENDED]${"\u001B[0m"} ${group.name}`);
}
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 output = this._outputs.get(test.index);
if (output) {
const logArgs = (0, Utils_1.getTestLog)(this._padding, test, { min, max }, true);
output.log(logArgs.join(" "));
}
}
}
completed(tests) {
this._header.log(`${"\u001B[32m"}[ISOBENCH ENDED]${"\u001B[0m"} ${this._benchName}`);
this._tty.end();
}
}
exports.DynamicStream = DynamicStream;
;