microbench.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var path = require("path"),
  2. async = require("async"),
  3. aggregate = require("../../../");
  4. module.exports = function _runner(opts) {
  5. var testName = path.basename(opts.module.filename),
  6. testTime = Date.now();
  7. // run for different input sizes
  8. async.map(
  9. opts.sizes,
  10. function mapper(size, nextSize) {
  11. var datas = [];
  12. for (var i = 0, n = size; i < n; i++) {
  13. var input = opts.generateInputDoc(i, size);
  14. if (input instanceof Array) {
  15. datas.push.apply(datas, input);
  16. } else {
  17. datas.push(input);
  18. }
  19. }
  20. // run for a couple of iterations
  21. var iterations = Array.apply(null, new Array(opts.nIterations)).map(Number);
  22. async.map(
  23. iterations,
  24. function mapper(iteration, nextIteration) {
  25. var t0 = Date.now();
  26. aggregate(opts.pipeline, datas, function(err, docs) {
  27. if (err) return nextIteration(err);
  28. return nextIteration(null, {
  29. ms: Date.now() - t0,
  30. //docs: docs.length,
  31. //doc0: docs[0],
  32. });
  33. });
  34. },
  35. function done(err, iterations) {
  36. if (err) return nextSize(err);
  37. // get avg
  38. var sum = 0;
  39. iterations.forEach(function(iteration) {
  40. sum += iteration.ms;
  41. });
  42. // report results
  43. console.log("%j", {
  44. n: testName,
  45. t: testTime,
  46. inputs: size,
  47. avgMs: sum / iterations.length,
  48. iterations: iterations,
  49. });
  50. }
  51. );
  52. },
  53. function done(err, sizes) {
  54. if (err) throw err;
  55. }
  56. );
  57. };