compareGasReports.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const chalk = require('chalk');
  4. const { argv } = require('yargs')
  5. .env()
  6. .options({
  7. style: {
  8. type: 'string',
  9. choices: [ 'shell', 'markdown' ],
  10. default: 'shell',
  11. },
  12. });
  13. // Deduce base tx cost from the percentage denominator
  14. const BASE_TX_COST = 21000;
  15. // Utilities
  16. function sum (...args) {
  17. return args.reduce((a, b) => a + b, 0);
  18. }
  19. function average (...args) {
  20. return sum(...args) / args.length;
  21. }
  22. function variation (current, previous) {
  23. return {
  24. value: current,
  25. delta: current - previous,
  26. prcnt: 100 * (current - previous) / (previous - BASE_TX_COST),
  27. };
  28. }
  29. // Report class
  30. class Report {
  31. // Read report file
  32. static load (filepath) {
  33. return JSON.parse(fs.readFileSync(filepath, 'utf8'));
  34. }
  35. // Compare two reports
  36. static compare (update, ref, opts = { hideEqual: true }) {
  37. if (JSON.stringify(update.config.metadata) !== JSON.stringify(ref.config.metadata)) {
  38. throw new Error('Reports produced with non matching metadata');
  39. }
  40. return Object.keys(update.info.methods)
  41. .filter(key => ref.info.methods[key])
  42. .filter(key => update.info.methods[key].numberOfCalls > 0)
  43. .filter(key => update.info.methods[key].numberOfCalls === ref.info.methods[key].numberOfCalls)
  44. .map(key => ({
  45. contract: ref.info.methods[key].contract,
  46. method: ref.info.methods[key].fnSig,
  47. min: variation(...[update, ref].map(x => ~~Math.min(...x.info.methods[key].gasData))),
  48. max: variation(...[update, ref].map(x => ~~Math.max(...x.info.methods[key].gasData))),
  49. avg: variation(...[update, ref].map(x => ~~average(...x.info.methods[key].gasData))),
  50. }))
  51. .filter(row => !opts.hideEqual || (row.min.delta && row.max.delta && row.avg.delta))
  52. .sort((a, b) => `${a.contract}:${a.method}` < `${b.contract}:${b.method}` ? -1 : 1);
  53. }
  54. }
  55. // Display
  56. function center (text, length) {
  57. return text.padStart((text.length + length) / 2).padEnd(length);
  58. }
  59. function plusSign (num) {
  60. return num > 0 ? '+' : '';
  61. }
  62. function formatCellShell (cell) {
  63. const format = chalk[cell.delta > 0 ? 'red' : cell.delta < 0 ? 'green' : 'reset'];
  64. return [
  65. format((isNaN(cell.value) ? '-' : cell.value.toString()).padStart(8)),
  66. format((isNaN(cell.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()).padStart(8)),
  67. format((isNaN(cell.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%').padStart(8)),
  68. ];
  69. }
  70. function formatCmpShell (rows) {
  71. const contractLength = Math.max(8, ...rows.map(({ contract }) => contract.length));
  72. const methodLength = Math.max(7, ...rows.map(({ method }) => method.length));
  73. const COLS = [
  74. { txt: '', length: 0 },
  75. { txt: 'Contract', length: contractLength },
  76. { txt: 'Method', length: methodLength },
  77. { txt: 'Min', length: 30 },
  78. { txt: 'Avg', length: 30 },
  79. { txt: 'Max', length: 30 },
  80. { txt: '', length: 0 },
  81. ];
  82. const HEADER = COLS.map(entry => chalk.bold(center(entry.txt, entry.length || 0))).join(' | ').trim();
  83. const SEPARATOR = COLS.map(({ length }) => length > 0 ? '-'.repeat(length + 2) : '').join('|').trim();
  84. return [
  85. '',
  86. HEADER,
  87. ...rows.map(entry => [
  88. '',
  89. chalk.grey(entry.contract.padEnd(contractLength)),
  90. entry.method.padEnd(methodLength),
  91. ...formatCellShell(entry.min),
  92. ...formatCellShell(entry.avg),
  93. ...formatCellShell(entry.max),
  94. '',
  95. ].join(' | ').trim()),
  96. '',
  97. ].join(`\n${SEPARATOR}\n`).trim();
  98. }
  99. function alignPattern (align) {
  100. switch (align) {
  101. case 'left':
  102. case undefined:
  103. return ':-';
  104. case 'right':
  105. return '-:';
  106. case 'center':
  107. return ':-:';
  108. }
  109. }
  110. function trend (value) {
  111. return value > 0
  112. ? ':x:'
  113. : value < 0
  114. ? ':heavy_check_mark:'
  115. : ':heavy_minus_sign:';
  116. }
  117. function formatCellMarkdown (cell) {
  118. return [
  119. (isNaN(cell.value) ? '-' : cell.value.toString()),
  120. (isNaN(cell.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()),
  121. (isNaN(cell.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%') + trend(cell.delta),
  122. ];
  123. }
  124. function formatCmpMarkdown (rows) {
  125. const COLS = [
  126. { txt: '' },
  127. { txt: 'Contract', align: 'left' },
  128. { txt: 'Method', align: 'left' },
  129. { txt: 'Min', align: 'right' },
  130. { txt: '(+/-)', align: 'right' },
  131. { txt: '%', align: 'right' },
  132. { txt: 'Avg', align: 'right' },
  133. { txt: '(+/-)', align: 'right' },
  134. { txt: '%', align: 'right' },
  135. { txt: 'Max', align: 'right' },
  136. { txt: '(+/-)', align: 'right' },
  137. { txt: '%', align: 'right' },
  138. { txt: '' },
  139. ];
  140. const HEADER = COLS.map(entry => entry.txt).join(' | ').trim();
  141. const SEPARATOR = COLS.map(entry => entry.txt ? alignPattern(entry.align) : '').join('|').trim();
  142. return [
  143. '# Changes to gas costs',
  144. '',
  145. HEADER,
  146. SEPARATOR,
  147. rows.map(entry => [
  148. '',
  149. entry.contract,
  150. entry.method,
  151. ...formatCellMarkdown(entry.min),
  152. ...formatCellMarkdown(entry.avg),
  153. ...formatCellMarkdown(entry.max),
  154. '',
  155. ].join(' | ').trim()).join('\n'),
  156. '',
  157. ].join('\n').trim();
  158. }
  159. // MAIN
  160. const report = Report.compare(Report.load(argv._[0]), Report.load(argv._[1]));
  161. switch (argv.style) {
  162. case 'markdown':
  163. console.log(formatCmpMarkdown(report));
  164. break;
  165. case 'shell':
  166. default:
  167. console.log(formatCmpShell(report));
  168. break;
  169. }