Skip to content

Commit

Permalink
feat(core): use existing benchmark.js suites
Browse files Browse the repository at this point in the history
refs #8
  • Loading branch information
JamieMason committed Jan 13, 2017
1 parent 85fea14 commit e2e731f
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 68 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -43,6 +43,7 @@
"xo": {
"envs": [
"browser",
"jasmine",
"node"
],
"space": 2
Expand Down
66 changes: 0 additions & 66 deletions src/adapter.js

This file was deleted.

File renamed without changes.
4 changes: 4 additions & 0 deletions src/globals.js
@@ -0,0 +1,4 @@
module.exports = {
Benchmark: global.Benchmark,
lodash: global._
};
12 changes: 12 additions & 0 deletions src/lib/construct.js
@@ -0,0 +1,12 @@
module.exports = function construct(Instance, a, b, c) {
if (c) {
return new Instance(a, b, c);
}
if (b) {
return new Instance(a, b);
}
if (a) {
return new Instance(a);
}
return new Instance();
};
18 changes: 16 additions & 2 deletions src/main.js
@@ -1,3 +1,17 @@
module.exports = function karmaBenchmark() {
// temp...
// 3rd party modules
var karma = require('./vendor/karma');

// modules
var runBenchmarks = require('./run-benchmarks');
var WrappedBenchmark = require('./wrapped-benchmark');

// implementation
global.Benchmark = WrappedBenchmark;

karma.start = function () {
runBenchmarks(function () {
karma.complete({
coverage: global.__coverage__
});
});
};
55 changes: 55 additions & 0 deletions src/run-benchmarks.js
@@ -0,0 +1,55 @@
// 3rd party modules
var karma = require('./vendor/karma');

// modules
var store = require('./store');

// public
module.exports = runBenchmarks;

// implementation
function runBenchmarks(done) {
var suites = store.getSuites();
if (suites.length) {
runSuite(suites.shift(), function () {
runBenchmarks(done);
});
} else {
done();
}
}

function runSuite(suite, done) {
var errors = [];
suite
.on('cycle', function (e) {
karma.result({
id: e.target.id,
description: suite.name + ': ' + e.target.name,
suite: [],
success: errors.length === 0,
log: errors,
skipped: false,
time: e.target.stats.mean * 1000,
benchmark: {
suite: suite.name,
name: e.target.name,
stats: e.target.stats,
count: e.target.count,
cycles: e.target.cycles,
error: e.target.error,
hz: e.target.hz
}
});
errors = [];
})
.on('abort error', function (e) {
errors.push(e.target.error.toString());
})
.on('complete',
done
)
.run({
async: true
});
}
27 changes: 27 additions & 0 deletions src/store.js
@@ -0,0 +1,27 @@
// public
module.exports = {
addBenchmark: addBenchmark,
addSuite: addSuite,
getSuites: getSuites
};

// implementation
var benchmarks = [];
var suites = [];

function addBenchmark(benchmark, hasSuite) {
benchmark.hasSuite = Boolean(hasSuite);
benchmarks.push(benchmark);
console.log('benchmark added:', benchmark, benchmarks);
return benchmark;
}

function addSuite(suite) {
suites.push(suite);
console.log('suite added:', suite, suites);
return suite;
}

function getSuites() {
return suites;
}
1 change: 1 addition & 0 deletions src/vendor/benchmark.js
@@ -0,0 +1 @@
module.exports = global.Benchmark;
1 change: 1 addition & 0 deletions src/vendor/karma.js
@@ -0,0 +1 @@
module.exports = global.__karma__;
1 change: 1 addition & 0 deletions src/vendor/lodash.js
@@ -0,0 +1 @@
module.exports = global._;
32 changes: 32 additions & 0 deletions src/wrapped-benchmark.js
@@ -0,0 +1,32 @@
// 3rd party modules
var Benchmark = require('./vendor/Benchmark');

// modules
var construct = require('./lib/construct');
var store = require('./store');

// public
module.exports = WrappedBenchmark;
module.exports.Suite = WrappedSuite;

// implementation
var Suite = Benchmark.Suite;

function WrappedBenchmark(name, fn, options) {
var benchmark = construct(Benchmark, name, fn, options);
return store.addBenchmark(benchmark, false);
}

function WrappedSuite(name, options) {
var suite = construct(Suite, name, options);
suite.add = addBenchmark;
store.addSuite(suite);
return suite;
}

function addBenchmark(name, fn, options) {
var suite = Suite.prototype.add.call(this, name, fn, options);
var benchmark = suite[suite.length - 1];
store.addBenchmark(benchmark, true);
return suite;
}

0 comments on commit e2e731f

Please sign in to comment.