Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

suite.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. module.exports = newSuite;
  3. var benchmark = require("benchmark"),
  4. chalk = require("chalk");
  5. var padSize = 27;
  6. function newSuite(name) {
  7. var benches = [];
  8. return new benchmark.Suite(name)
  9. .on("add", function(event) {
  10. benches.push(event.target);
  11. })
  12. .on("start", function() {
  13. process.stdout.write("benchmarking " + name + " performance ...\n\n");
  14. })
  15. .on("cycle", function(event) {
  16. process.stdout.write(String(event.target) + "\n");
  17. })
  18. .on("complete", function() {
  19. if (benches.length > 1) {
  20. var fastest = this.filter("fastest"), // eslint-disable-line no-invalid-this
  21. fastestHz = getHz(fastest[0]);
  22. process.stdout.write("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest") + "\n");
  23. benches.forEach(function(bench) {
  24. if (fastest.indexOf(bench) === 0)
  25. return;
  26. var hz = hz = getHz(bench);
  27. var percent = (1 - hz / fastestHz) * 100;
  28. process.stdout.write(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1) + "% slower") + "\n");
  29. });
  30. }
  31. process.stdout.write("\n");
  32. });
  33. }
  34. function getHz(bench) {
  35. return 1 / (bench.stats.mean + bench.stats.moe);
  36. }
  37. function pad(str, len, l) {
  38. while (str.length < len)
  39. str = l ? str + " " : " " + str;
  40. return str;
  41. }