benchmarksman_test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var assert = require("assert"),
  2. benchmarksman = require("../");
  3. exports.benchmarksman = {
  4. "should run benchmarks": function(done) {
  5. this.timeout(0);
  6. var benchmarks = {
  7. "do nothing": function() {
  8. null;
  9. },
  10. "do maths": function() {
  11. 1 + 2 - 3 / 4 * 5 % 6;
  12. },
  13. },
  14. benchmarksKeys = Object.keys(benchmarks),
  15. benchmarksCount = benchmarksKeys.length,
  16. benchmarked = {},
  17. benchmarkedKeys = [],
  18. benchmarkedCount = 0,
  19. opts = {
  20. reporter: function(bench) {
  21. benchmarksman.reporter.apply(this, arguments);
  22. bench.on("complete", function() {
  23. benchmarked[bench.name] = bench;
  24. benchmarkedKeys.push(bench.name);
  25. benchmarkedCount++;
  26. if (benchmarkedCount === benchmarksCount){
  27. assert.deepEqual(benchmarkedKeys, benchmarksKeys);
  28. assert(benchmarked["do nothing"].hz > benchmarked["do maths"].hz,
  29. "doing nothing should be faster than doing maths");
  30. return done();
  31. }
  32. });
  33. }
  34. };
  35. benchmarksman.runner.call(opts, benchmarks);
  36. },
  37. };
  38. // Mocha one-liner to make these tests self-hosted
  39. if (!module.main) (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);