UNPKG

@c4312/matcha

Version:

A caffeine driven, simple command line for benchmarking

67 lines 2.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const readline_1 = require("readline"); const os_1 = require("os"); exports.prettyFactory = { description: 'Pretty prints results to the console', start: () => new PrettyReporter(process.stdout), }; const center = 30; class PrettyReporter { constructor(out) { this.out = out; this.numberFormat = Intl.NumberFormat('en-US', { maximumSignificantDigits: 3 }); this.results = []; this.out.write(os_1.EOL); } onStartCycle(benchmark) { this.out.write(chalk_1.default.yellow('running > '.padStart(center)) + chalk_1.default.gray(benchmark.name)); this.out.write(os_1.EOL); } onFinishCycle(newResult) { this.results.push(newResult); readline_1.moveCursor(this.out, 0, -this.results.length); let minHz = Infinity; let maxHz = 0; for (const benchmark of this.results) { if (benchmark.hz) { minHz = Math.min(minHz, benchmark.hz); maxHz = Math.max(maxHz, benchmark.hz); } } for (const benchmark of this.results) { readline_1.clearLine(this.out, 0); if (benchmark.error) { this.out.write(chalk_1.default.red('error > '.padStart(center)) + chalk_1.default.gray(benchmark.name)); } else { const text = benchmark.name + ` (${this.numberFormat.format(benchmark.hz / minHz)}x)`; this.out.write(chalk_1.default.green(`${this.numberFormat.format(benchmark.hz)} ops/sec > `.padStart(center)) + (benchmark.hz === maxHz ? text : chalk_1.default.gray(text))); } this.out.write(os_1.EOL); } } onComplete() { var _a; const cases = this.results.slice(); const error = cases.find(c => !!c.error); if (error) { this.out.write(chalk_1.default.red(error.error.stack)); return; } cases.sort((a, b) => b.hz - a.hz); const elapsed = cases.reduce((n, c) => n + c.times.elapsed, 0); this.out.write(os_1.EOL); this.out.write(` ${chalk_1.default.grey('Benches')}: ${cases.length}${os_1.EOL}`); this.out.write(` ${chalk_1.default.grey('Fastest')}: ${(_a = cases[0]) === null || _a === void 0 ? void 0 : _a.name}${os_1.EOL}`); this.out.write(` ${chalk_1.default.grey('Elapsed')}: ${this.numberFormat.format(elapsed)}s${os_1.EOL}`); this.out.write(os_1.EOL); } } exports.PrettyReporter = PrettyReporter; //# sourceMappingURL=pretty.js.map