EzDevInfo.com

benchmark.js

A benchmarking library. As used on jsPerf.com. Benchmark.js a robust benchmarking library that works on nearly all javascript platforms, supports high-resolution timers, and returns statistically significant results.

Using string in benchmark.js

The example app for benchmark.js defines its performance tests as strings rather than JavaScript functions:

https://github.com/bestiejs/benchmark.js/blob/master/example/jsperf/index.html#L250

Is there an advantage to defining the performance test in this way? Why isn't it just a function?


Source: (StackOverflow)

Benchmark.js and Bonsai.js

I'm currently testing some Visualization toolkits performance and have a problem testing bonsai.js. Every time I run the benchmark the chrome renderer crashes, and i can't seem to find the problem because bonsai.js is pretty hard to debug.

var i = 0,
    bench = new Benchmark('Bonsai',
    function() {
        var b = bonsai.run(player, {
                code: document.getElementById('bs').innerHTML,
                height: 500,
                width: 500,
                framerate: 40
            });
    },
    {
    'onStart': function() {
        console.log('Running benchmark...');
    },
    'onComplete':  function() {
        console.log('Finished benchmark');
        console.log('Ops/sec: ' + this.hz);
        console.log('Standard Deviation: ' + this.stats.deviation);
        console.log('Cycles: ' + i);
        console.log('Iterations/Cycle: ' + this.count);
    },
    'onCycle': function() {
        $('#bonsai').empty();
        i += 1;
    }
});

I've created a fiddle based on an example Visualization from the bonsai Website that illustrates the issue: http://jsfiddle.net/2uvXM/1/

I had to include benchmark.js inline because I did not find a cdn hosting a current version.


Source: (StackOverflow)

Advertisements

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

Source: (StackOverflow)

Benchmark.js: Callback for each run

I'm setting up a benchmark suite for a library of mine using Benchmark.js. I've seen on jsperf.com that it shows the progress of the benchmarks by updating how many runs of each benchmark it has completed.

Right now, I know how to subscribe to the finish of each benchmark:

// register a listener for an event type  
bench.on('cycle', listener); 

However, this fires every time the suite finishes all runs for a given benchmark, not each time the benchmark itself is run.

Is there a way that I can either:

  • Subscribe so a function is called every time a benchmark is run, so I can show the progress as the number run goes from 1 up to 80 or how every many runs are complete
  • Subscribe so that every given period of time (say 0.1 seconds), a function is called which shows how many runs the given benchmark is completed.

Is this possible? I can't find anything directly doing this in the documentation, but I'm very new to the library.


Source: (StackOverflow)

How to make the output of multiple JavaScript functions display in HTML?

So I'm trying to display the results of a few function calls in a JavaScript file that uses benchmark.js in a separate HTML file. My js file looks something like this (disregard the names of methods and classes):

class.init(function(context) {

    Benchmark("function description", {
        'defer': true,

         fn': function(deferred) {
         var x = context.ones([100, 100]);
         var y = x.repeat(2, 0);
         context.barrier(function (){
             deferred.resolve();
          });
    },
    'onComplete': function(event) {
    //This is what I'd like to print out
    console.log(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms");
    //
    } 
}).run();

There are multiple function calls similar to this.

My HTML just looks something lile:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <title> Sublime Webpage </title>

    </head>
    <body>
        <div class="main">
          <div class="head">
             <h1>Hello</h1>

          </div>
          <script src="filename.js"></script>
        </div>
    </body>
</html>

At the moment, it just prints the results to the console, which is better than nothing, but obviously not what I want. I talked to someone briefly and they suggested I use jQuery. I looked into it, and tried using document.write(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms") in the place of console.log(), but this didn't seem to work. Does anyone have suggestions?


Source: (StackOverflow)

Passing function as arguments in javascript benchmark [passing function vs direct acces]

well i'm building some javascript code and im just curious about benchmark of passing function in argument vs direct access

I got following functions

testIt(function(){
    alert('Hi test');
});

function testIt(func){
    func();
};

function testIt2(){
    alert('Hi test');
};

And now how about testIt vs testIt2? Would testIt be slower?


Source: (StackOverflow)

What do the results from benchmark js mean?

I am using a version of Benchmark JS for node and I can't find any information about how to read the results.

Firstly, is there a place that details what all the data you can extract from Benchmark JS?

Secondly I am currently getting the following result in my console:

Test x 2,276,094 ops/sec ±0.84% (190 runs sampled)

What do all these bits of information mean?

Test: the name of my test, I know that one

x 2,276,094 ops/sec: I am assuming this is the average number of times the code could theoretically run in a second?

±0.84%: No idea

190 runs sampled: The amount of times benchmark ran the code to get the result?


Source: (StackOverflow)

Benchmark Asynchronous Code (Benchmark.js, Node.js)

I'd like to use the Benchmark.js module to test some asynchronous code written in node.js. Specifically, I want to fire ~10,000 requests to two servers (one written in node, one written in PHP), and track how long it takes for each server to complete all requests.

I was planning to write a simple node script to fire these requests using Benchmark, but I'm a little confused regarding how to use it with asynchronous code. Usually in node modules, there's some sort of a callback that you call when your async code is complete, or a Promise is returned from the function etc. But with Benchmark, from everything I'm reading in the docs, it doesn't seem to handle async at all.

Does anyone know what I should be doing or looking at? I can write the benchmark manually if need be; it just seems like a common enough use case that Benchmark or others would probably have already implemented it in their professional-grade testing libraries.

Thanks for any direction, ~ Nate


Source: (StackOverflow)

Strange Object vs. Array benchmark result under --debug-brk flag

I want to decide what is best structure in javascript for small tree nodes. Each node have key, value and references to the parent and child nodes (if exists). So I wrote benchmark for testing performance of the nodes creation:

var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();

suite
.add('Object node creation', function() {
    var parent = {
        key:    'some_key',
        value:  1234123412,
        parent: null,
        left:   null,
        right:  null
    };

    var left = {
        key: 'left child',
        value: 'asdfoasjdfpasdjf',
        parent: null,
        left:   null,
        right:  null
    };

    var right = {
        key:   'right child',
        value: 'qwerqwerqwerqwwq',
        parent: null,
        left:   null,
        right:  null
    };

    parent.left  = left;
    parent.right = right;
    left.parent  = parent;
    right.parent = parent;
})
.add('Array node creation', function() {
    var parent = ['some_key',    1234123412,         null, null, null];
    var left   = ['left_child',  'asdfoasjdfpasdjf', null, null, null];
    var right  = ['right_child', 'qwerqwerqwerqwwq', null, null, null];

    parent[3] = left;
    parent[4] = right;
    left[2]   = parent;
    right[2]  = parent;
})
.on('complete', function() {
    console.log(this[0].toString());
    console.log(this[1].toString());

    console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run();

So I run the test:

node benchmark.js

and have the following results:

Object node creation x 30,613,857 ops/sec ±3.11% (82 runs sampled)
Array node creation x 15,133,139 ops/sec ±1.12% (94 runs sampled)
Fastest is Object node creation

And it's OK.

But then I run benchmark with --debug-brk:

 node --debug-brk benchmark.js

and get the following results:

Object node creation x 4,842,367 ops/sec ±2.81% (90 runs sampled)
Array node creation x 10,906,219 ops/sec ±2.36% (90 runs sampled)
Fastest is Array node creation

As you see: performance of object creation was decreased drastically. Maybe someone can explain me why it so and what is happened.


Source: (StackOverflow)

benchmark.js : how to display/read results (ops/sec)?

I am able to successfully create and run benchmark suite, but not sure how to get the benchmark values of each output, this.filter('fastest').pluck('name') in onComplete gives me the name of the fastest operation, but I want the ops/sec value of each function in the test suite. how to get that?


Source: (StackOverflow)

Using Benchmarkjs with Webpack and Babel

I'm trying to get some basic benchmark tests working and am having trouble figuring out the right configuration. I'm trying to use Benchmarkjs with webpack and babel to transpile my code to es5. I created a benchmarks.webpack.js as an entry point which looks like this:

var context = require.context('./src/js', true, /-benchmark\.js$/);
context.keys().forEach(context);
module.exports = context;

I then have a benchmark file that I want to run (test-benchmark.js):

import benchmark from 'benchmark';
import benchmarks from 'beautify-benchmark';

let suite = new benchmark.Suite;

suite.add('RegExp#test', function() {
  /o/.test('Hello World!');
})
.add('String#indexOf', function() {
  'Hello World!'.indexOf('o') > -1;
})
.on('cycle', function(event) {
  benchmarks.add(event.target);
})
.on('complete', function() {
  benchmarks.log();
})
.run();

I updated my webpack build to try and transpile the benchmarks:

_.assign(config, {
  devtool: 'eval-cheap-module-source-map',
  output: {
    path: path.join(__dirname, 'build/benchmark'),
    filename: 'benchmark.js',
    publicPath: '/'
  },
  entry: [
    './benchmarks.webpack.js'
  ],
  plugins: [

  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel?stage=0'],
        include: path.join(__dirname, 'src/js')
      },
    ]
  },
});

Finally, I want to be able run this from an npm script:

  "scripts": {
    "bench": "webpack --config webpack.bench.config.js && node build/benchmark/benchmark.js"
  },

However, I'm getting warnings that the result of the benchmark dependency is an expression and there no suitable loaders for the .json, .txt, etc files. I tried hacking up Benchmarkjs to export correctly but was not successful.

WARNING in ./~/benchmark/benchmark.js
Critical dependencies:
1122:34-49 the request of a dependency is an expression
 @ ./~/benchmark/benchmark.js 1122:34-49

WARNING in ./~/benchmark/package.json
Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/package.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
|   "name": "benchmark",
|   "version": "1.0.0",
|   "description": "A benchmarking library that works on nearly all JavaScript platforms, supports high-resolution timers, and returns statistically significant results.",
 @ ./~/benchmark ^\.\/.*$

WARNING in ./~/benchmark/LICENSE.txt
Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/LICENSE.txt Line 1: Unexpected number
You may need an appropriate loader to handle this file type.
| Copyright 2010-2012 Mathias Bynens <http://mathiasbynens.be/>
| Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/>
| Modified by John-David Dalton <http://allyoucanleet.com/>
 @ ./~/benchmark ^\.\/.*$

Source: (StackOverflow)