compareGasReports.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. const deployments = update.info.deployments
  41. .map(contract => Object.assign(
  42. contract,
  43. { previousVersion: ref.info.deployments.find(({ name }) => name === contract.name) },
  44. ))
  45. .filter(contract => contract.gasData?.length && contract.previousVersion?.gasData?.length)
  46. .flatMap(contract => [{
  47. contract: contract.name,
  48. method: '[bytecode length]',
  49. avg: variation(contract.bytecode.length / 2 - 1, contract.previousVersion.bytecode.length / 2 - 1),
  50. }, {
  51. contract: contract.name,
  52. method: '[construction cost]',
  53. avg: variation(...[contract.gasData, contract.previousVersion.gasData].map(x => Math.round(average(...x)))),
  54. }])
  55. .sort((a, b) => `${a.contract}:${a.method}`.localeCompare(`${b.contract}:${b.method}`));
  56. const methods = Object.keys(update.info.methods)
  57. .filter(key => ref.info.methods[key])
  58. .filter(key => update.info.methods[key].numberOfCalls > 0)
  59. .filter(key => update.info.methods[key].numberOfCalls === ref.info.methods[key].numberOfCalls)
  60. .map(key => ({
  61. contract: ref.info.methods[key].contract,
  62. method: ref.info.methods[key].fnSig,
  63. min: variation(...[update, ref].map(x => Math.min(...x.info.methods[key].gasData))),
  64. max: variation(...[update, ref].map(x => Math.max(...x.info.methods[key].gasData))),
  65. avg: variation(...[update, ref].map(x => Math.round(average(...x.info.methods[key].gasData)))),
  66. }))
  67. .sort((a, b) => `${a.contract}:${a.method}`.localeCompare(`${b.contract}:${b.method}`));
  68. return [].concat(deployments, methods)
  69. .filter(row => !opts.hideEqual || row.min?.delta || row.max?.delta || row.avg?.delta);
  70. }
  71. }
  72. // Display
  73. function center (text, length) {
  74. return text.padStart((text.length + length) / 2).padEnd(length);
  75. }
  76. function plusSign (num) {
  77. return num > 0 ? '+' : '';
  78. }
  79. function formatCellShell (cell) {
  80. const format = chalk[cell?.delta > 0 ? 'red' : cell?.delta < 0 ? 'green' : 'reset'];
  81. return [
  82. format((!isFinite(cell?.value) ? '-' : cell.value.toString()).padStart(8)),
  83. format((!isFinite(cell?.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()).padStart(8)),
  84. format((!isFinite(cell?.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%').padStart(8)),
  85. ];
  86. }
  87. function formatCmpShell (rows) {
  88. const contractLength = Math.max(8, ...rows.map(({ contract }) => contract.length));
  89. const methodLength = Math.max(7, ...rows.map(({ method }) => method.length));
  90. const COLS = [
  91. { txt: '', length: 0 },
  92. { txt: 'Contract', length: contractLength },
  93. { txt: 'Method', length: methodLength },
  94. { txt: 'Min', length: 30 },
  95. { txt: 'Max', length: 30 },
  96. { txt: 'Avg', length: 30 },
  97. { txt: '', length: 0 },
  98. ];
  99. const HEADER = COLS.map(entry => chalk.bold(center(entry.txt, entry.length || 0))).join(' | ').trim();
  100. const SEPARATOR = COLS.map(({ length }) => length > 0 ? '-'.repeat(length + 2) : '').join('|').trim();
  101. return [
  102. '',
  103. HEADER,
  104. ...rows.map(entry => [
  105. '',
  106. chalk.grey(entry.contract.padEnd(contractLength)),
  107. entry.method.padEnd(methodLength),
  108. ...formatCellShell(entry.min),
  109. ...formatCellShell(entry.max),
  110. ...formatCellShell(entry.avg),
  111. '',
  112. ].join(' | ').trim()),
  113. '',
  114. ].join(`\n${SEPARATOR}\n`).trim();
  115. }
  116. function alignPattern (align) {
  117. switch (align) {
  118. case 'left':
  119. case undefined:
  120. return ':-';
  121. case 'right':
  122. return '-:';
  123. case 'center':
  124. return ':-:';
  125. }
  126. }
  127. function trend (value) {
  128. return value > 0
  129. ? ':x:'
  130. : value < 0
  131. ? ':heavy_check_mark:'
  132. : ':heavy_minus_sign:';
  133. }
  134. function formatCellMarkdown (cell) {
  135. return [
  136. (!isFinite(cell?.value) ? '-' : cell.value.toString()),
  137. (!isFinite(cell?.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()),
  138. (!isFinite(cell?.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%' + trend(cell.delta)),
  139. ];
  140. }
  141. function formatCmpMarkdown (rows) {
  142. const COLS = [
  143. { txt: '' },
  144. { txt: 'Contract', align: 'left' },
  145. { txt: 'Method', align: 'left' },
  146. { txt: 'Min', align: 'right' },
  147. { txt: '(+/-)', align: 'right' },
  148. { txt: '%', align: 'right' },
  149. { txt: 'Max', align: 'right' },
  150. { txt: '(+/-)', align: 'right' },
  151. { txt: '%', align: 'right' },
  152. { txt: 'Avg', align: 'right' },
  153. { txt: '(+/-)', align: 'right' },
  154. { txt: '%', align: 'right' },
  155. { txt: '' },
  156. ];
  157. const HEADER = COLS.map(entry => entry.txt).join(' | ').trim();
  158. const SEPARATOR = COLS.map(entry => entry.txt ? alignPattern(entry.align) : '').join('|').trim();
  159. return [
  160. '# Changes to gas costs',
  161. '',
  162. HEADER,
  163. SEPARATOR,
  164. rows.map(entry => [
  165. '',
  166. entry.contract,
  167. entry.method,
  168. ...formatCellMarkdown(entry.min),
  169. ...formatCellMarkdown(entry.max),
  170. ...formatCellMarkdown(entry.avg),
  171. '',
  172. ].join(' | ').trim()).join('\n'),
  173. '',
  174. ].join('\n').trim();
  175. }
  176. // MAIN
  177. const report = Report.compare(Report.load(argv._[0]), Report.load(argv._[1]));
  178. switch (argv.style) {
  179. case 'markdown':
  180. console.log(formatCmpMarkdown(report));
  181. break;
  182. case 'shell':
  183. default:
  184. console.log(formatCmpShell(report));
  185. break;
  186. }