bench.util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var Benchmark = require('benchmark');
  6. /**
  7. * bench()
  8. *
  9. * (see https://benchmarkjs.com/docs#Benchmark)
  10. * ---------------------------
  11. * @param {String} name
  12. * @param {Array} testFns [array of functions]
  13. * @param {Function} done
  14. */
  15. module.exports = function bench (name, testFns, done) {
  16. var suite = new Benchmark.Suite({ name: name });
  17. _.each(testFns, function (testFn, i) {
  18. suite = suite.add((testFn.name||name)+'#'+i, {
  19. defer: true,
  20. fn: function(deferred){
  21. var oneTickHasElapsed;
  22. setImmediate(function (){
  23. oneTickHasElapsed = true;
  24. });
  25. testFn(function (err) {
  26. if (err) {
  27. console.error('An error occured when attempting to run benchmark:\n',err);
  28. }//>-
  29. // Ensure one tick has elapsed before proceeding
  30. // (otherwise, benchmark doesn't work properly)
  31. if (oneTickHasElapsed) {
  32. deferred.resolve();
  33. }
  34. else {
  35. setImmediate(function (){
  36. deferred.resolve();
  37. });
  38. }
  39. });
  40. }
  41. });
  42. });//</each testFn>
  43. suite.on('cycle', function(event) {
  44. console.log(' •',String(event.target));
  45. })
  46. .on('complete', function() {
  47. // console.log('Fastest is ' + this.filter('fastest').map('name'));
  48. // console.log('Slowest is ' + this.filter('slowest').map('name'));
  49. return done();
  50. })
  51. .run({ async: true });
  52. };
  53. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54. // For posterity, here's how to do it asynchronously:
  55. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  56. // module.exports = function bench (name, testFns, done) {
  57. // var suite = new Benchmark.Suite({ name: name });
  58. // _.each(testFns, function (testFn, i) {
  59. // suite = suite.add(testFn.name+'#'+i, {
  60. // defer: true,
  61. // fn: function (deferred) {
  62. // testFn(function _afterRunningTestFn(err){
  63. // setImmediate(function _afterEnsuringAsynchronous(){
  64. // if (err) {
  65. // console.error('An error occured when attempting to benchmark this code:\n',err);
  66. // }//>- (resolve the deferred either way)
  67. // deferred.resolve();
  68. // });//</afterwards cb from waiting for nextTick>
  69. // });//</afterwards cb from running test fn>
  70. // }
  71. // });//<suite.add>
  72. // });//</each testFn>
  73. // suite.on('cycle', function(event) {
  74. // console.log(' •',String(event.target));
  75. // })
  76. // .on('complete', function() {
  77. // console.log('Fastest is ' + this.filter('fastest').map('name'));
  78. // console.log('Slowest is ' + this.filter('slowest').map('name'));
  79. // return done(undefined, this);
  80. // })
  81. // .run();
  82. // };
  83. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -