javascriptbenchmark.js

How to get a report out of benchmark.js


I tried benchmark.js while it appears to be running a benchmark and showing me fastest test, i don't get how to get a nice report out of it I tried this:

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

stats seems undefined, do i miss anything? how come I couldn't find a guide on showing how to print stats? how do i get a report showing me how much time it took to run the test for each method i was testing, what was median, number of errors and all this summary all together? thanks.


Solution

  • You should use 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 });