定义循环次数 - Benchmark.js

Tar*_*riq 14 javascript benchmark.js

我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)

就像我们在JUnitBenchmarks中一样:

@BenchmarkOptions(clock = Clock.NANO_TIME, callgc = true, benchmarkRounds = 10, warmupRounds = 1)
Run Code Online (Sandbox Code Playgroud)

在这里,我还要声明benchmarkRoundswarmupRounds计入benchmarkjs.我认为warmupRounds地图initCount?以及如何设置确切的周期数/基准迭代次数?

或者,如果我们有一些其他好的JavaScript库可以处理它也会工作.

app*_*lue 7

在JavaScript基准测试中使用固定迭代计数是有风险的:随着浏览器变得更快,我们最终可能获得零时间结果.

Benchmark.js不允许提前设置轮数/迭代次数.相反,它会一遍又一遍地运行测试,直到结果被认为是相当准确的.你应该查看Monsur Hossain代码阅读.文章中的一些亮点:

  • Benchmark.js中的一个循环包括实际测试的设置,拆除和多次迭代.
  • Benchmark.js从分析阶段开始:运行几个周期以找到最佳迭代次数(尽可能快地完成测试,同时收集足够的样本以生成准确的结果).
  • 分析期间运行的循环数保存在Benchmark.prototype.cycles.
  • 了解最佳迭代次数后,Benchmark.js开始采样阶段:运行测试并实际存储结果.
  • Benchmark.prototype.stats.sample 是采样期间每个周期的结果数组.
  • Benchmark.prototype.count 是采样期间的迭代次数.


Dan*_*anH 5

查看文档:

http://benchmarkjs.com/docs

听起来你是对的

  1. WarmupRounds => initCount ( http://benchmarkjs.com/docs#options_initCount )
  2. 周期 => http://benchmarkjs.com/docs#prototype_cycles

  • 一个周期由测试的设置、拆卸和多次迭代组成。循环并不是测试的单次迭代,就像 [JUnitBenchmarks](http://labs.carrotsearch.com/junit-benchmarks-tutorial.html) 中的情况一样。 (4认同)