如何从 benchmark.js 中获取报告

How to get a report out of benchmark.js

我尝试了 benchmark.js,虽然它似乎是 运行 基准测试并向我展示了最快的测试,但我不知道如何从中得到一份好的报告我试过这个:

suite.add('My#test', function() {
    console.log("test")
}).on('complete', function() {
        console.log('Fastest is ' + this.filter('fastest').map('name'));
        console.log('stats: ' + suite.stats) // but stats seems undefined, do i miss anything? how come I couldn't find a guide on showing how to print stats?
}).run({ 'async': true });

统计数据似乎未定义,我错过了什么吗?为什么我找不到显示如何打印统计信息的指南?我如何获得一份报告,显示 运行 我正在测试的每种方法的测试花费了多少时间、中位数、错误数量以及所有这些摘要?谢谢。

你应该使用this

// add listeners 
suite.on('cycle', function(event) {
  console.log(String(event.target));
  console.log(event.target.name);
  console.log(event.target.stats.rme);
  console.log(event.target.stats.sample.length);
  console.log(event.target.count); // The number of times a test was executed.
  console.log(event.target.cycles); // The number of cycles performed while benchmarking.
  console.log(event.target.hz); //The number of executions per second.
})
.on('complete', function() {
    for (var i = 0; i < this.length; i++) {
        console.log(this[i].hz + " ops/sec");
        console.log(this[i].stats.sample.length);
        //console.log(this[i].stats);
    }
  console.log(color('Fastest is ' + this.filter('fastest').map('name'),'green'));
  console.log(color('Slowest is ' + this.filter('slowest').map('name'),'red'));
})
// run async 
.run({ 'async': true });