Skip to content

Commit

Permalink
Add request benchmarks, and make benchmarks opt-in with env var
Browse files Browse the repository at this point in the history
To run benchmarks, do:
BENCHMARK=true mocha test/benchmarks
  • Loading branch information
sgress454 committed Nov 10, 2016
1 parent 132b3aa commit 6b4ba32
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 206 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"waterline-criteria": "^2.0.0"
},
"devDependencies": {
"benchmark": "1.0.0",
"benchmark": "^2.1.2",
"checksum": "0.1.1",
"coffee-script": "1.9.1",
"expect.js": "0.3.1",
Expand Down
4 changes: 2 additions & 2 deletions test/benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
From the root directory of sails:

```sh
$ mocha test/benchmarks
$ BENCHMARK=true mocha test/benchmarks
```

To get a more detailed report with millisecond timings for each benchmark, run:

```sh
$ mocha test/benchmarks -v
$ BENCHMARK=true mocha test/benchmarks -v
```


Expand Down
42 changes: 42 additions & 0 deletions test/benchmarks/helpers/benchmarx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var Benchmark = require('benchmark');
var _ = require('@sailshq/lodash');

/**
* benchmarx()
* ---------------------------
* @param {String} name
* @param {Array} testFns [array of functions]
* @param {Function} notifier
* @param {Function} done
*/
module.exports = function benchmarx (name, testFns, done) {
Benchmark.options.minSamples = 500;
var suite = new Benchmark.Suite({ name: name });
_.each(testFns, function (testFn) {
suite = suite.add(testFn.name, {
defer: true,
fn: function (deferred) {
testFn(function _afterRunningTestFn(err){
process.nextTick(function _afterEnsuringAsynchronous(){
if (err) {
console.error('An error occured when attempting to benchmark this code:\n',err);
// Resolve the deferred either way.
}

deferred.resolve();
});//</afterwards cb from waiting for nextTick>
});//</afterwards cb from running test fn>
}
});//<suite.add>
});//</each testFn>

suite.on('cycle', function(event) {
console.log(' •',String(event.target), '(avg ' + (event.target.stats.mean * 1000) + ' ms)');
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
console.log('Slowest is ' + this.filter('slowest').map('name'));
return done(undefined, this);
})
.run();
};
Loading

0 comments on commit 6b4ba32

Please sign in to comment.