Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add setup and teardown for each test in benchmark.js

I am new to using benchmark.js, the documentation is bit jarring, and not able to find many examples, can anyone please confirm if my code is correct, sorry i cannot share the whole code( company policy).

consider setText(choice); as some operation, and I want to compare various choices. (the function works fine independently, i have verified it). Though I am setting setup and teardown function, I am not sure if setting is correct, I want them to run before and after every single run of setText(choice);

using console.log , I found that they run only once for every 200 times of setText(choice);, I want them running everytime.

also, how do I retrive the ops/sec for each opeartion on completion of suite . You can find the my benchmark suite related code below.

var suite = new Benchmark.Suite;
suite.add('innerText', function() {
    setText('innerText');
}, {'setup':setup,'teardown':teardown})
.add('innerHTML', function() {
    setText('innerHTML');
}, {'setup':setup,'teardown':teardown})
.add('textContent', function() {
    setText('textContent');
}, {'setup':setup,'teardown':teardown})
.on('cycle', function(event, bench) {
  log(String(bench));
}).on('complete', function() {
  log('Fastest is ' + JSON.stringify(this));
}).run(false);
like image 418
mido Avatar asked Nov 06 '14 08:11

mido


1 Answers

I am also new to benchmarkjs and hope i can answer correctly.

http://monsur.hossa.in/2012/12/11/benchmarkjs.html

If you read the above article, you will find out the benchmark will go into the analysis and sampling phase.

It will run a number of iterations (i.e. the "test loop") which did setText during sampling phase without calling setup or teardown.

So I guess the setup and teardown is just for something you do before entering the test loop and not exactly for the each test. You can't get more granular than the test-loop

You can get less granular than the test-loop; you can listen for events on the entire Benchmark (vs at the suite-level, or the test-loop level). The second question about each operation on completion of suite, you can get it from on complete event

.on('complete', function(event) {
    //will log out the array of benchmark.suite, you can get it from each benchmark suite.
    //e.g event.currentTarget[0].hz which retrieve the operation/s. for the first one.
    console.log(event.currentTarget);
    //equivalent of above; event.currentTarget===this; both are Benchmark.suite.
    console.log(this); 
})
like image 164
Thomas Lee Avatar answered Oct 27 '22 01:10

Thomas Lee