@c4312/matcha
Version:
A caffeine driven, simple command line for benchmarking
72 lines • 2.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = __importDefault(require("commander"));
const reporters_1 = require("./reporters");
const runner_1 = require("./runner");
const path_1 = require("path");
const grep_1 = require("./middleware/grep");
const cpu_profiler_1 = require("./middleware/cpu-profiler");
const util_1 = require("util");
const fs_1 = require("fs");
const os_1 = require("os");
const { version } = require('../package.json');
const args = commander_1.default
.arguments('<file>')
.name('matcha')
.version(version)
.option('-g, --grep <pattern>', 'Run a subset of benchmarks', '')
.option('-R, --reporter <reporter>', 'Specify the reporter to use', 'pretty')
.option('--cpu-profile [pattern]', 'Run on all tests, or those matching the regex. Saves a .cpuprofile file that can be opened in the Chrome devtools.')
.option('--reporters', 'Display available reporters')
.parse(process.argv)
.opts();
if (args.reporters) {
printReporters();
}
else if (!commander_1.default.args.length) {
commander_1.default.help();
}
else {
benchmarkFiles();
}
function benchmarkFiles() {
const reporterFactory = typeof args.reporter === 'string' ? reporters_1.reporters[args.reporter] : args.reporter;
if (!reporterFactory) {
console.error(`Unknown reporter ${args.reporter}`);
commander_1.default.help();
}
const middleware = [];
if (args.grep) {
middleware.push(grep_1.grepMiddleware(new RegExp(args.grep, 'i')));
}
if (args.cpuProfile === true) {
middleware.push(cpu_profiler_1.cpuProfiler(writeProfile));
}
else if (args.cpuProfile) {
middleware.push(cpu_profiler_1.cpuProfiler(writeProfile, new RegExp(args.cpuProfile, 'i')));
}
runner_1.benchmark({
middleware,
reporter: reporterFactory.start(),
prepare: api => {
Object.assign(global, api);
for (const file of commander_1.default.args) {
require(path_1.resolve(process.cwd(), file));
}
},
});
}
function writeProfile(bench, profile) {
const safeName = bench.name.replace(/[^a-z0-9]/gi, '-');
util_1.promisify(fs_1.writeFile)(`${safeName}.cpuprofile`, JSON.stringify(profile));
}
function printReporters() {
for (const [name, reporter] of Object.entries(reporters_1.reporters)) {
process.stdout.write(`${name.padStart(15)} - ${reporter.description}${os_1.EOL}`);
}
}
//# sourceMappingURL=cli.js.map