UNPKG

@dapplion/benchmark

Version:

Ensures that new code does not introduce performance regressions with CI. Tracks:

85 lines (84 loc) 3.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.benchmarkReporterWithPrev = benchmarkReporterWithPrev; // eslint-disable-next-line import/no-extraneous-dependencies const mocha_1 = __importDefault(require("mocha")); const globalState_js_1 = require("./globalState.js"); const format_js_1 = require("./format.js"); const utils_js_1 = require("./utils.js"); const { EVENT_RUN_BEGIN, EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_TEST_PENDING, EVENT_SUITE_BEGIN, EVENT_SUITE_END, } = mocha_1.default.Runner.constants; function benchmarkReporterWithPrev(prevBench, threshold) { const prevResults = new Map(); if (prevBench) { for (const bench of prevBench.results) { prevResults.set(bench.id, bench); } } return class BenchmarkReporter extends mocha_1.default.reporters.Base { constructor(runner, options) { super(runner, options); // eslint-disable-next-line no-console const consoleLog = console.log; const color = mocha_1.default.reporters.Base.color; const symbols = mocha_1.default.reporters.Base.symbols; let indents = 0; let n = 0; function indent() { return Array(indents).join(" "); } runner.on(EVENT_RUN_BEGIN, function () { consoleLog(); }); runner.on(EVENT_SUITE_BEGIN, function (suite) { ++indents; consoleLog(color("suite", "%s%s"), indent(), suite.title); }); runner.on(EVENT_SUITE_END, function () { --indents; if (indents === 1) { consoleLog(); } }); runner.on(EVENT_TEST_PENDING, function (test) { const fmt = indent() + color("pending", " - %s"); consoleLog(fmt, test.title); }); runner.on(EVENT_TEST_PASS, function (test) { try { if (!test.parent) throw Error("test has no parent"); const rootSuite = (0, utils_js_1.getRootSuite)(test.parent); const results = globalState_js_1.resultsByRootSuite.get(rootSuite); if (!results) throw Error("root suite not found"); const result = results.get(test.title); if (result) { // Render benchmark const prevResult = prevResults.get(result.id) ?? null; const resultRow = (0, format_js_1.formatResultRow)(result, prevResult, result.threshold ?? threshold); const fmt = indent() + color("checkmark", " " + symbols.ok) + " " + resultRow; consoleLog(fmt); } else { // Render regular test const fmt = indent() + color("checkmark", " " + symbols.ok) + color("pass", " %s"); consoleLog(fmt, test.title); } } catch (e) { // Log error manually since mocha doesn't log errors thrown here consoleLog(e); process.exitCode = 1; throw e; } }); runner.on(EVENT_TEST_FAIL, function (test) { consoleLog(indent() + color("fail", " %d) %s"), ++n, test.title); }); runner.once(EVENT_RUN_END, this.epilogue.bind(this)); } }; }