[PLUGIN] Continuous JavaScript Performance Monitoring with Benchmark.js and Karma

156 views
Skip to first unread message

Jamie Mason

unread,
Feb 6, 2014, 12:20:49 PM2/6/14
to karma...@googlegroups.com
Hi all,
I just wanted to mention that there is a new framework plugin at https://github.com/JamieMason/karma-benchmark which aims to provide continuous JavaScript performance monitoring with benchmark.js and Karma.

I hope someone finds this useful, I will post the README below.

Thank you for reading,

Jamie Mason, @fold_left

karma-benchmark (Née Perftacular)

Karma plugin to run Benchmark.js over multiple browsers with Jenkins CI compatible output.

Installation

npm install karma-benchmark --save-dev

Karma Configuration

module.exports = function(config) {
  config.set({
    // Everything discussed below goes in here.
    // Other values such as browsers, files, autoWatch, reporters etc.
    // are needed as normal, but have been left out of these example.
  });
};

Adding karma-benchmark

In the options object supplied to config.set(), add benchmark to the list of frameworks in karma.conf.js.

frameworks: ['benchmark']

Feeding Data Into Jenkins

If you'd like to feed your data into Jenkins, install the jUnit reporter.

npm install karma-junit-reporter --save-dev

Then add to the options object supplied to config.set(), the junitReporter configuration with the path you want the output xml to be written to.

junitReporter: {
  suite: 'unit',
  outputFile: 'build/junit-benchmark-results.xml'
}

Timeouts

As large suites of Benchmarks take a long time to run, you may need to increase Karma's timeout from it's default of 60000.

captureTimeout: 60000

Writing Benchmarks

Benchmarks can be written as normal using the var suite = new Benchmark.Suite; format detailed at benchmarkjs.com or with an optional wrapper provided for Benchmark.js that offers a coding style familiar to users of Jasmine and Mocha.

Typical

benchmark('_.each', function () {
  when('iterating over an array', function () {
    _.each([1, 2, 3], function(el){
      return el;
    });
  });
  when('iterating over an object', function () {
    _.each({one : 1, two : 2, three : 3}, function(el){
      return el;
    });
  });
});

Setup/Teardown

The equivalent of the above _.each when iterating over an array benchmark with setup and teardown methods is as follows.

benchmark('_.each', function () {
  when('iterating over an array', {
    before: function() {
      this.list = [5, 4, 3];
    },
    run: function() {
      _.each(this.list, function(el) {
        return el * Math.random();
      });
    },
    after: function() {
      this.list = null;
    }
  });
});


Vojta Jína

unread,
Feb 6, 2014, 4:33:43 PM2/6/14
to karma...@googlegroups.com
Cool! Thanks for sharing!


--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/karma-users/f7e6566d-03d9-48a7-b312-b4796f0cbda3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages