@c4312/马查
现代化 matcha,由基准.js提供动力. 这些年我发现Matcha超级有用,但它已经 准确的问题,不支持承诺 或与节点12, 而且显然被抛弃。 我们在这里解决!

使用量
npm install --global @c4312/matcha
然后,你可以创建一个文件 bench 函数,例如: 我的本钱,js数字 :
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
bench('forEach', () => arr.forEach(v => (sum += v)));
bench('for-of loop', () => {
for (const v of arr) {
sum += v;
}
});
然后,简单运行:
matcha my-bench.js
命令行选项
Usage: matcha [options] <file>
Options:
-V, --version output the version number
-g, --grep <pattern> Run a subset of benchmarks (default: "")
-R, --reporter <reporter> Specify the reporter to use (default: "pretty")
--cpu-profile [pattern] Run on all tests, or those matching the regex.
Saves a .cpuprofile file that can be opened in the Chrome devtools.
--reporters Display available reporters
-h, --help output usage information
同步基准
你可以从基准返回承诺 并收回召回:
bench('plain fs', callback => readFile(__filename, callback));
bench('promisifed fs', async () => await readFileAsync(__filename));
设置
您可以设置任何 基准.js 选项 通过 set() 帮手 也可以采用Aync设置和撕裂方法.
// in a promise:
set('setup', async () => {
await waitUntilPageIsLoaded();
});
// or a callback:
set('teardown', callback => closePage(callback));
// as well as base options:
set('maxTime', 1);
set('minSamples', 2000);
嵌入式套装
多个基准套房可以被嵌入. 包括设置和拆卸在内的选项是继承的、被锁住和被推翻的.
set('setup', globalPrepare);
// globalPrepare() run before:
bench('a', runA);
// globalPrepare() and nestedPrepare() run before
// these, and they run for 1 second at most:
suite('nested', () => {
set('setup', nestedPrepare);
set('maxTime', 1);
bench('b', runB);
});
API 密码
您可以在程序上使用 Matcha API :
const { GatherReporter, benchmark } = require('@c4312/benchmark');
// A reporter that just stores results in an array:
const reporter = new GatherReporter();
await benchmark({
reporter,
prepare(api) {
// The standard API as described above!
api.bench('myFunction', fn);
// This function may be async and return a promise,
// which we'll wait for before we start benchmarking.
},
});
for (const result of reporter.results) {
console.log('Benchmark', result.name, 'runs at', result.hz, 'ops/sec');
}