benchmark-runner.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. var _ = require('@sailshq/lodash');
  2. var Benchmark = require('benchmark');
  3. module.exports = function runBenchmarks(name, testFns) {
  4. var suite = new Benchmark.Suite({
  5. name: name
  6. });
  7. _.each(testFns, function buildTest(testFn) {
  8. suite = suite.add(testFn.name, {
  9. fn: testFn
  10. });
  11. });
  12. suite.on('cycle', function(event) {
  13. console.log(' •', String(event.target));
  14. })
  15. .on('complete', function() {
  16. // Time is measured in microseconds so 1000 = 1ms
  17. var fastestMean = _.first(this.filter('fastest')).stats.mean * 1000;
  18. var slowestMean = _.first(this.filter('slowest')).stats.mean * 1000;
  19. var mean = {
  20. fastest: Benchmark.formatNumber(fastestMean < 1 ? fastestMean.toFixed(2) : Math.round(fastestMean)),
  21. slowest: Benchmark.formatNumber(slowestMean < 1 ? slowestMean.toFixed(2) : Math.round(slowestMean))
  22. };
  23. console.log('Fastest is ' + this.filter('fastest').map('name') + ' with an average of: ' + mean.fastest + 'ms');
  24. console.log('Slowest is ' + this.filter('slowest').map('name') + ' with an average of: ' + mean.slowest + 'ms');
  25. })
  26. .run();
  27. };