@c4312/matcha
Version:
A caffeine driven, simple command line for benchmarking
100 lines • 3.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const async_1 = require("./async");
const options_1 = require("./options");
const benchmark_1 = __importDefault(require("benchmark"));
const defaultRunFn = (name, options) => {
const bench = new benchmark_1.default(name, options.fn, options);
const prom = new Promise(resolve => bench.on('complete', resolve));
bench.run();
return prom;
};
/**
* Installs a callback or promise-returning benchmark in the bench
* options. This is awkward with their API, but it is what it is.
*/
const installBenchFn = (fn, options) => {
if (fn.length > 0) {
return options.merge({
defer: true,
fn: (deferred) => {
fn(err => (err ? deferred.reject(err) : deferred.resolve(undefined)));
},
});
}
if (async_1.returnsPromiseLike(fn)) {
return options.merge({
defer: true,
fn: (deferred) => {
fn()
.then(() => deferred.resolve(undefined))
.catch(err => deferred.reject(err));
},
});
}
return options.merge({ fn: fn });
};
const runMiddleware = (bench, stack, offset = 0) => {
if (stack.length === offset) {
return Promise.resolve();
}
return stack[offset](bench, b => runMiddleware(b, stack, offset + 1));
};
/**
* A benchmark suite. Wraps around the basic Benchmark.js suites, since they
* don't support asynchronous setup and teardown.
* @see https://github.com/bestiejs/benchmark.js/pull/174
*/
class MatchaSuite {
constructor(runFn = defaultRunFn) {
this.runFn = runFn;
this.benches = [];
}
/**
* Adds a new benchmark case.
*/
addCase(bench) {
this.benches.push(bench);
}
/**
* Runs all benchmarks.
*/
run() {
return __awaiter(this, void 0, void 0, function* () {
for (const bench of this.benches) {
yield runMiddleware(bench, [
...bench.middleware,
(bench) => __awaiter(this, void 0, void 0, function* () {
var _a;
const options = (_a = bench.options, (_a !== null && _a !== void 0 ? _a : options_1.Options.empty));
if (options.setup) {
yield async_1.runMaybeAsync(options.setup);
}
const benchOptions = options.assign({
setup: undefined,
teardown: undefined,
});
yield this.runFn(bench.name, installBenchFn(bench.fn, benchOptions));
if (options.teardown) {
yield async_1.runMaybeAsync(options.teardown);
}
}),
]);
}
});
}
}
exports.MatchaSuite = MatchaSuite;
//# sourceMappingURL=suite.js.map