Skip to content

Commit

Permalink
feat(filtering): implement xsuite, xbenchmark, ssuite, and bbenchmark
Browse files Browse the repository at this point in the history
closes #4, #5
  • Loading branch information
lazd authored and JamieMason committed Jul 21, 2016
1 parent cf59719 commit 7f76fbd
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 4 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,39 @@ suite('Iteration', function() {
});
});
```


### Running only a specific benchmark or suite

To run only a specific benchmark, use `benchmark.only()` or `bbenchmark()` instead of `benchmark()`:

```js
benchmark.only(function() {
// Only this benchmark will run
// bbenchmark() does the same thing
});

benchmark(function() {
// This benchmark won't run
});
```

The same applies to suites with `suite.only()` and `ssuite()`.


### Skipping benchmarks & suites

To skip a benchmark, use `benchmark.skip()` or `xbenchmark()` instead of `benchmark()`:

```js
benchmark.skip(function() {
// This benchmark won't run
// xbenchmark() does the same thing
});

benchmark(function() {
// This and all other benchmarks will run
});
```

The same applies to suites with `suite.skip()` and `xsuite()`.
105 changes: 101 additions & 4 deletions lib/perftacular.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
var karma = global.__karma__;
var suites = global.__karma_benchmark_suites__ = [];
var totalTests = 0;
var ignoreOtherSuites = false;
var ignoreOtherBenchmarks = false;
var noop = function() {};

/**
* Copy to target every member of source that target does not share a member of the same name.
Expand Down Expand Up @@ -34,9 +37,24 @@
* @param {Object} [suiteOptions]
* @return {Object} suite
*/
global.suite = function(suiteName, addAllBenchmarksToSuite, suiteOptions) {
var addSuite = global.suite = function(suiteName, addAllBenchmarksToSuite, suiteOptions, onlyRunThisSuite) {
if (onlyRunThisSuite) {
// Reset count
totalTests = 0;

// Mark that we shouldn't add other suites
ignoreOtherSuites = true;

// Drop previously added suites
suites.length = 0;
}
else if (ignoreOtherSuites) {
// Skip this suite
return;
}

var currentSuite;
var benchmarks = [];

suiteOptions = suiteOptions || {};

Expand All @@ -46,19 +64,98 @@
* @param {Function} benchmark
* @param {Object} [benchOptions]
*/
function addBenchmarkToSuite(benchName, benchmark, benchOptions) {
currentSuite.add(benchName, benchmark, extendIf(benchOptions || {}, suiteOptions));
function queueBenchmarkAdd(benchName, benchmark, benchOptions, onlyRunThisBenchmark) {
if (onlyRunThisBenchmark) {
// Remove other suites
suites.length = 0;
suites.push(currentSuite);

// Make sure subsequent suites are ignored
ignoreOtherSuites = true;

// Remove other benchmarks in this suite
benchmarks.length = 0;

// Make sure subsequent benchmarks are ignored
ignoreOtherBenchmarks = true;

// Reset test count
totalTests = 0;
}
else if (ignoreOtherBenchmarks) {
// Skip this benchmark
return;
}

// Add benchmark to the queue
benchmarks.push({
benchName: benchName,
benchmark: benchmark,
benchOptions: benchOptions
});
}

/**
* Skip this benchmark.
* @param {[type]} benchName
* @param {Function} benchmark
* @param {Object} [benchOptions]
*/
queueBenchmarkAdd.skip = global.xbenchmark = noop;

/*
* Only run this benchmark.
* @param {[type]} benchName
* @param {Function} benchmark
* @param {Object} [benchOptions]
*/
queueBenchmarkAdd.only = global.bbenchmark = function(benchName, benchmark, benchOptions) {
// Add only this benchmark to the queue
queueBenchmarkAdd(benchName, benchmark, benchOptions, true);
};

/**
* Actually adds a benchmark to the suite
* @param {Object} bench
* @ignore
*/
function doAdd(bench) {
currentSuite.add(bench.benchName, bench.benchmark, extendIf(bench.benchOptions || {}, suiteOptions));
karma.info({
total: ++totalTests
});
}

currentSuite = new Benchmark.Suite(suiteName, suiteOptions);
addSuiteToReport(currentSuite);
global.benchmark = addBenchmarkToSuite;
global.benchmark = queueBenchmarkAdd;
addAllBenchmarksToSuite();

// Add each of the queued benchmarks
benchmarks.forEach(doAdd)

return currentSuite;
};

/**
* Skip this suite.
* @param {String} suiteName
* @param {Function} addAllBenchmarksToSuite
* @param {Object} [suiteOptions]
* @return {Object} suite
*/
global.suite.skip = global.xsuite = noop;

/**
* Only run this suite.
* @param {String} suiteName
* @param {Function} addAllBenchmarksToSuite
* @param {Object} [suiteOptions]
* @return {Object} suite
*/
global.suite.only = global.ssuite = function(suiteName, addAllBenchmarksToSuite, suiteOptions) {
// Add the suite, mark it as the only suite to run
addSuite(suiteName, addAllBenchmarksToSuite, suiteOptions, true);
};

global.dump = function() {
Expand Down

0 comments on commit 7f76fbd

Please sign in to comment.