run-benchmarks.helper.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var Benchmark = require('benchmark');
  6. /**
  7. * runBenchmarks()
  8. * ---------------------------
  9. * @param {String} name
  10. * @param {Array} testFns [array of functions]
  11. * @param {Function} notifier
  12. * @param {Function} done
  13. */
  14. module.exports = function runBenchmarks (name, testFns, done) {
  15. var suite = new Benchmark.Suite({ name: name });
  16. _.each(testFns, function (testFn) {
  17. suite = suite.add(testFn.name, {
  18. defer: true,
  19. fn: function (deferred) {
  20. testFn(function _afterRunningTestFn(err){
  21. setImmediate(function _afterEnsuringAsynchronous(){
  22. if (err) {
  23. console.error('An error occured when attempting to benchmark this code:\n',err);
  24. // Resolve the deferred either way.
  25. }
  26. deferred.resolve();
  27. });//</afterwards cb from waiting for nextTick>
  28. });//</afterwards cb from running test fn>
  29. }
  30. });//<suite.add>
  31. });//</each testFn>
  32. suite.on('cycle', function(event) {
  33. console.log(' •',String(event.target));
  34. })
  35. .on('complete', function() {
  36. // console.log('Fastest is ' + this.filter('fastest').map('name'));
  37. // console.log('Slowest is ' + this.filter('slowest').map('name'));
  38. return done(undefined, this);
  39. })
  40. .run();
  41. };