Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define number of cycles - Benchmark.js

I am trying to execute a sample performance benchmark using Benchmark.js. Here is what I have wrote:

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();

Now like we do in JUnitBenchmarks:

@BenchmarkOptions(clock = Clock.NANO_TIME, callgc = true, benchmarkRounds = 10, warmupRounds = 1)

Here also I want to declare benchmarkRounds and warmupRounds count in benchmarkjs. I think warmupRounds maps to initCount? And how to set exact number of cycles/benchmark iteration?

Or if we have some other good JavaScript library that can handle it would work too.

like image 266
Tariq Avatar asked Sep 17 '15 11:09

Tariq


1 Answers

Using fixed iteration counts in JavaScript benchmarks is risky: we might get zero-time results eventually, as browsers become faster.

Benchmark.js does not allow setting the number of rounds/iterations in advance. Instead it runs tests over and over again until results can be considered reasonably accurate. You should check out the code reading by Monsur Hossain. Some highlights from the article:

  • A cycle in Benchmark.js consists of set-up, tear-down, and multiple iterations of the actual test.
  • Benchmark.js starts with the analysis phase: run a few cycles to find the optimal number of iterations (finish the test as fast as possible while collecting enough samples to generate accurate results).
  • The number of cycles run during analysis is saved in Benchmark.prototype.cycles.
  • Knowing the optimal number of iterations, Benchmark.js begins the sampling phase: runs the tests and actually stores the results.
  • Benchmark.prototype.stats.sample is an array of results from each cycle during sampling.
  • Benchmark.prototype.count is the number of iterations during sampling.
like image 161
approxiblue Avatar answered Oct 03 '22 09:10

approxiblue