diff_multi_bench_output.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env node
  2. var colors = require('colors'),
  3. fs = require('fs'),
  4. _ = require('underscore'),
  5. metrics = require('metrics'),
  6. // `node diff_multi_bench_output.js before.txt after.txt`
  7. before = process.argv[2],
  8. after = process.argv[3];
  9. if (!before || !after) {
  10. console.log('Please supply two file arguments:');
  11. var n = __filename;
  12. n = n.substring(n.lastIndexOf('/', n.length));
  13. console.log(' ./' + n + ' multiBenchBefore.txt multiBenchAfter.txt');
  14. console.log('To generate multiBenchBefore.txt, run');
  15. console.log(' node multi_bench.js > multiBenchBefore.txt');
  16. console.log('Thank you for benchmarking responsibly.');
  17. return;
  18. }
  19. var before_lines = fs.readFileSync(before, 'utf8').split('\n'),
  20. after_lines = fs.readFileSync(after, 'utf8').split('\n');
  21. console.log('Comparing before,', before.green, '(', before_lines.length,
  22. 'lines)', 'to after,', after.green, '(', after_lines.length, 'lines)');
  23. var total_ops = new metrics.Histogram.createUniformHistogram();
  24. before_lines.forEach(function(b, i) {
  25. var a = after_lines[i];
  26. if (!a || !b || !b.trim() || !a.trim()) {
  27. // console.log('#ignored#', '>'+a+'<', '>'+b+'<');
  28. return;
  29. }
  30. b_words = b.split(' ').filter(is_whitespace);
  31. a_words = a.split(' ').filter(is_whitespace);
  32. var ops =
  33. [b_words, a_words]
  34. .map(function(words) {
  35. // console.log(words);
  36. return parseInt10(words.slice(-2, -1));
  37. }).filter(function(num) {
  38. var isNaN = !num && num !== 0;
  39. return !isNaN;
  40. });
  41. if (ops.length != 2) return
  42. var delta = ops[1] - ops[0];
  43. total_ops.update(delta);
  44. delta = humanize_diff(delta);
  45. console.log(
  46. // name of test
  47. command_name(a_words) == command_name(b_words)
  48. ? command_name(a_words) + ':'
  49. : '404:',
  50. // results of test
  51. ops.join(' -> '), 'ops/sec (∆', delta, ')');
  52. });
  53. console.log('Mean difference in ops/sec:', humanize_diff(total_ops.mean()));
  54. function is_whitespace(s) {
  55. return !!s.trim();
  56. }
  57. function parseInt10(s) {
  58. return parseInt(s, 10);
  59. }
  60. // green if greater than 0, red otherwise
  61. function humanize_diff(num) {
  62. if (num > 0) {
  63. return ('+' + num).green;
  64. }
  65. return ('' + num).red;
  66. }
  67. function command_name(words) {
  68. var line = words.join(' ');
  69. return line.substr(0, line.indexOf(','));
  70. }