我正在尝试使用Benchmark.js执行一个示例性能基准测试。以下是我所写的:
var Benchmark = require('benchmark');
var arr = []
benchmark = new Benchmark('testPerf',function(){
arr.push(1000);
},
{
delay: 0,
initCount: 1,
minSamples: 1000,
onComplete : function(){ console.log(this);},
onCycle: function(){}
});
benchmark.run();现在就像我们在JUnitBenchmarks中所做的那样:
@BenchmarkOptions(clock = Clock.NANO_TIME, callgc = true, benchmarkRounds = 10, warmupRounds = 1)在这里,我还想在基准测试in中声明benchmarkRounds和warmupRounds计数。我认为warmupRounds映射到initCount?以及如何设置确切的循环/基准迭代次数?
或者,如果我们有一些其他好的JavaScript库可以处理它,也可以工作。
发布于 2015-10-01 07:21:04
在JavaScript基准测试中使用固定的迭代计数是危险的:我们最终可能会得到零时间的结果,因为浏览器变得更快。
Benchmark.js不允许预先设置循环/迭代次数。相反,它一遍又一遍地运行测试,直到结果被认为是合理准确的。你应该看看的代码读取。这篇文章的一些要点:
Benchmark.prototype.cycles中。Benchmark.prototype.stats.sample是采样过程中每个周期的结果数组。Benchmark.prototype.count是抽样期间的迭代次数。发布于 2015-09-30 00:02:53
查看文档:
http://benchmarkjs.com/docs
听起来你是对的
发布于 2016-05-12 14:32:29
不管这是否一个好主意,如果您将minTime和maxTime设置为一些负值,那么minSamples和initCount将被保留为唯一的条件,它们将对应于在每个周期运行的#循环和热身迭代。因此,测试函数将执行(initCount+1) * minSamples次数。至少我的实验证明了这一点。
var Benchmark = require('benchmark');
var counter = 0;
Benchmark('counting', {
'fn': function() { ++counter; },
minSamples: 3,
initCount: 1,
minTime: -Infinity,
maxTime: -Infinity,
onCycle: function () { console.log('[onCycle] counter: ' + counter); },
onComplete : function(){ console.log('mean: ' + this.stats.mean);},
}).run();让我使用benchmark.js 2.1.0:
$ node count.js
[onCycle] counter: 2
[onCycle] counter: 4
[onCycle] counter: 6
mean: 0.0000034683333333333333https://stackoverflow.com/questions/32629779
复制相似问题