statcmp.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. const fs = require("fs");
  3. const ttest = require("ttest");
  4. const VALID_GROUP_BYS = ["browser", "pdf", "page", "round", "stat"];
  5. function parseOptions() {
  6. const yargs = require("yargs")
  7. .usage(
  8. "Compare the results of two stats files.\n" +
  9. "Usage:\n $0 <BASELINE> <CURRENT> [options]"
  10. )
  11. .demand(2)
  12. .string(["groupBy"])
  13. .describe(
  14. "groupBy",
  15. "How statistics should grouped. Valid options: " +
  16. VALID_GROUP_BYS.join(" ")
  17. )
  18. .default("groupBy", "browser,stat");
  19. const result = yargs.argv;
  20. result.baseline = result._[0];
  21. result.current = result._[1];
  22. if (result.groupBy) {
  23. result.groupBy = result.groupBy.split(/[;, ]+/);
  24. }
  25. return result;
  26. }
  27. function group(stats, groupBy) {
  28. const vals = [];
  29. for (const curStat of stats) {
  30. const keyArr = [];
  31. for (const entry of groupBy) {
  32. keyArr.push(curStat[entry]);
  33. }
  34. const key = keyArr.join(",");
  35. (vals[key] ||= []).push(curStat.time);
  36. }
  37. return vals;
  38. }
  39. /*
  40. * Flatten the stats so that there's one row per stats entry.
  41. * Also, if results are not grouped by 'stat', keep only 'Overall' results.
  42. */
  43. function flatten(stats) {
  44. let rows = [];
  45. stats.forEach(function (curStat) {
  46. curStat.stats.forEach(function (s) {
  47. rows.push({
  48. browser: curStat.browser,
  49. page: curStat.page,
  50. pdf: curStat.pdf,
  51. round: curStat.round,
  52. stat: s.name,
  53. time: s.end - s.start,
  54. });
  55. });
  56. });
  57. // Use only overall results if not grouped by 'stat'
  58. if (!options.groupBy.includes("stat")) {
  59. rows = rows.filter(function (s) {
  60. return s.stat === "Overall";
  61. });
  62. }
  63. return rows;
  64. }
  65. function pad(s, length, dir /* default: 'right' */) {
  66. s = "" + s;
  67. const spaces = new Array(Math.max(0, length - s.length + 1)).join(" ");
  68. return dir === "left" ? spaces + s : s + spaces;
  69. }
  70. function mean(array) {
  71. function add(a, b) {
  72. return a + b;
  73. }
  74. return array.reduce(add, 0) / array.length;
  75. }
  76. /* Comparator for row key sorting. */
  77. function compareRow(a, b) {
  78. a = a.split(",");
  79. b = b.split(",");
  80. for (let i = 0; i < Math.min(a.length, b.length); i++) {
  81. const intA = parseInt(a[i], 10);
  82. const intB = parseInt(b[i], 10);
  83. const ai = isNaN(intA) ? a[i] : intA;
  84. const bi = isNaN(intB) ? b[i] : intB;
  85. if (ai < bi) {
  86. return -1;
  87. }
  88. if (ai > bi) {
  89. return 1;
  90. }
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Dump various stats in a table to compare the baseline and current results.
  96. * T-test Refresher:
  97. * If I understand t-test correctly, p is the probability that we'll observe
  98. * another test that is as extreme as the current result assuming the null
  99. * hypothesis is true. P is NOT the probability of the null hypothesis. The null
  100. * hypothesis in this case is that the baseline and current results will be the
  101. * same. It is generally accepted that you can reject the null hypothesis if the
  102. * p-value is less than 0.05. So if p < 0.05 we can reject the results are the
  103. * same which doesn't necessarily mean the results are faster/slower but it can
  104. * be implied.
  105. */
  106. function stat(baseline, current) {
  107. const baselineGroup = group(baseline, options.groupBy);
  108. const currentGroup = group(current, options.groupBy);
  109. const keys = Object.keys(baselineGroup);
  110. keys.sort(compareRow);
  111. const labels = options.groupBy.slice(0);
  112. labels.push("Count", "Baseline(ms)", "Current(ms)", "+/-", "% ");
  113. if (ttest) {
  114. labels.push("Result(P<.05)");
  115. }
  116. const rows = [];
  117. // collect rows and measure column widths
  118. const width = labels.map(function (s) {
  119. return s.length;
  120. });
  121. rows.push(labels);
  122. for (const key of keys) {
  123. const baselineMean = mean(baselineGroup[key]);
  124. const currentMean = mean(currentGroup[key]);
  125. const row = key.split(",");
  126. row.push(
  127. "" + baselineGroup[key].length,
  128. "" + Math.round(baselineMean),
  129. "" + Math.round(currentMean),
  130. "" + Math.round(currentMean - baselineMean),
  131. ((100 * (currentMean - baselineMean)) / baselineMean).toFixed(2)
  132. );
  133. if (ttest) {
  134. const p =
  135. baselineGroup[key].length < 2
  136. ? 1
  137. : ttest(baselineGroup[key], currentGroup[key]).pValue();
  138. if (p < 0.05) {
  139. row.push(currentMean < baselineMean ? "faster" : "slower");
  140. } else {
  141. row.push("");
  142. }
  143. }
  144. for (let i = 0; i < row.length; i++) {
  145. width[i] = Math.max(width[i], row[i].length);
  146. }
  147. rows.push(row);
  148. }
  149. // add horizontal line
  150. const hline = width.map(function (w) {
  151. return new Array(w + 1).join("-");
  152. });
  153. rows.splice(1, 0, hline);
  154. // print output
  155. console.log("-- Grouped By " + options.groupBy.join(", ") + " --");
  156. const groupCount = options.groupBy.length;
  157. for (const row of rows) {
  158. for (let i = 0; i < row.length; i++) {
  159. row[i] = pad(row[i], width[i], i < groupCount ? "right" : "left");
  160. }
  161. console.log(row.join(" | "));
  162. }
  163. }
  164. function main() {
  165. let baseline, current;
  166. try {
  167. const baselineFile = fs.readFileSync(options.baseline).toString();
  168. baseline = flatten(JSON.parse(baselineFile));
  169. } catch (e) {
  170. console.log('Error reading file "' + options.baseline + '": ' + e);
  171. process.exit(0);
  172. }
  173. try {
  174. const currentFile = fs.readFileSync(options.current).toString();
  175. current = flatten(JSON.parse(currentFile));
  176. } catch (e) {
  177. console.log('Error reading file "' + options.current + '": ' + e);
  178. process.exit(0);
  179. }
  180. stat(baseline, current);
  181. }
  182. const options = parseOptions();
  183. main();