UNPKG

assertthat

Version:
31 lines (30 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runAveragedBenchmark = void 0; const measure_time_1 = require("measure-time"); const runAveragedBenchmark = async function ({ benchmark, scale, howManyTimes }) { var _a, _b; const results = []; for (let i = 0; i < howManyTimes; i++) { // eslint-disable-next-line no-console console.log(` Average run ${i}`); await ((_a = benchmark.setup) === null || _a === void 0 ? void 0 : _a.call(benchmark, { scale })); const getMeasuredTime = (0, measure_time_1.measureTime)(); await benchmark.benchmark({ scale }); const times = getMeasuredTime(); await ((_b = benchmark.teardown) === null || _b === void 0 ? void 0 : _b.call(benchmark, { scale })); results.push(times.millisecondsTotal); } const averageTime = results.reduce((acc, next) => acc + next, 0) / results.length, deviation = results.reduce((maxDeviation, next) => { const currentDeviation = (next / averageTime) - 1; if (Math.abs(currentDeviation) > Math.abs(maxDeviation)) { return currentDeviation; } return maxDeviation; }, 0); return { averageTime, deviation }; }; exports.runAveragedBenchmark = runAveragedBenchmark;