Skip to content

Commit 0d91b62

Browse files
authored
Report changes to contract construction cost in CI (#3579)
1 parent 81336ae commit 0d91b62

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
"promise/avoid-new": "off",
5858
},
5959
"parserOptions": {
60-
"ecmaVersion": 2018
60+
"ecmaVersion": 2020
6161
}
6262
}

scripts/checks/compareGasReports.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,39 @@ class Report {
4444
if (JSON.stringify(update.config.metadata) !== JSON.stringify(ref.config.metadata)) {
4545
throw new Error('Reports produced with non matching metadata');
4646
}
47-
return Object.keys(update.info.methods)
47+
48+
const deployments = update.info.deployments
49+
.map(contract => Object.assign(
50+
contract,
51+
{ previousVersion: ref.info.deployments.find(({ name }) => name === contract.name) },
52+
))
53+
.filter(contract => contract.gasData?.length && contract.previousVersion?.gasData?.length)
54+
.flatMap(contract => [{
55+
contract: contract.name,
56+
method: '[bytecode length]',
57+
avg: variation(contract.bytecode.length / 2 - 1, contract.previousVersion.bytecode.length / 2 - 1),
58+
}, {
59+
contract: contract.name,
60+
method: '[construction cost]',
61+
avg: variation(...[contract.gasData, contract.previousVersion.gasData].map(x => Math.round(average(...x)))),
62+
}])
63+
.sort((a, b) => `${a.contract}:${a.method}`.localeCompare(`${b.contract}:${b.method}`));
64+
65+
const methods = Object.keys(update.info.methods)
4866
.filter(key => ref.info.methods[key])
4967
.filter(key => update.info.methods[key].numberOfCalls > 0)
5068
.filter(key => update.info.methods[key].numberOfCalls === ref.info.methods[key].numberOfCalls)
5169
.map(key => ({
5270
contract: ref.info.methods[key].contract,
5371
method: ref.info.methods[key].fnSig,
54-
min: variation(...[update, ref].map(x => ~~Math.min(...x.info.methods[key].gasData))),
55-
max: variation(...[update, ref].map(x => ~~Math.max(...x.info.methods[key].gasData))),
56-
avg: variation(...[update, ref].map(x => ~~average(...x.info.methods[key].gasData))),
72+
min: variation(...[update, ref].map(x => Math.min(...x.info.methods[key].gasData))),
73+
max: variation(...[update, ref].map(x => Math.max(...x.info.methods[key].gasData))),
74+
avg: variation(...[update, ref].map(x => Math.round(average(...x.info.methods[key].gasData)))),
5775
}))
58-
.filter(row => !opts.hideEqual || (row.min.delta && row.max.delta && row.avg.delta))
59-
.sort((a, b) => `${a.contract}:${a.method}` < `${b.contract}:${b.method}` ? -1 : 1);
76+
.sort((a, b) => `${a.contract}:${a.method}`.localeCompare(`${b.contract}:${b.method}`));
77+
78+
return [].concat(deployments, methods)
79+
.filter(row => !opts.hideEqual || row.min?.delta || row.max?.delta || row.avg?.delta);
6080
}
6181
}
6282

@@ -70,11 +90,11 @@ function plusSign (num) {
7090
}
7191

7292
function formatCellShell (cell) {
73-
const format = chalk[cell.delta > 0 ? 'red' : cell.delta < 0 ? 'green' : 'reset'];
93+
const format = chalk[cell?.delta > 0 ? 'red' : cell?.delta < 0 ? 'green' : 'reset'];
7494
return [
75-
format((isNaN(cell.value) ? '-' : cell.value.toString()).padStart(8)),
76-
format((isNaN(cell.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()).padStart(8)),
77-
format((isNaN(cell.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%').padStart(8)),
95+
format((!isFinite(cell?.value) ? '-' : cell.value.toString()).padStart(8)),
96+
format((!isFinite(cell?.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()).padStart(8)),
97+
format((!isFinite(cell?.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%').padStart(8)),
7898
];
7999
}
80100

@@ -87,8 +107,8 @@ function formatCmpShell (rows) {
87107
{ txt: 'Contract', length: contractLength },
88108
{ txt: 'Method', length: methodLength },
89109
{ txt: 'Min', length: 30 },
90-
{ txt: 'Avg', length: 30 },
91110
{ txt: 'Max', length: 30 },
111+
{ txt: 'Avg', length: 30 },
92112
{ txt: '', length: 0 },
93113
];
94114
const HEADER = COLS.map(entry => chalk.bold(center(entry.txt, entry.length || 0))).join(' | ').trim();
@@ -102,8 +122,8 @@ function formatCmpShell (rows) {
102122
chalk.grey(entry.contract.padEnd(contractLength)),
103123
entry.method.padEnd(methodLength),
104124
...formatCellShell(entry.min),
105-
...formatCellShell(entry.avg),
106125
...formatCellShell(entry.max),
126+
...formatCellShell(entry.avg),
107127
'',
108128
].join(' | ').trim()),
109129
'',
@@ -132,9 +152,9 @@ function trend (value) {
132152

133153
function formatCellMarkdown (cell) {
134154
return [
135-
(isNaN(cell.value) ? '-' : cell.value.toString()),
136-
(isNaN(cell.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()),
137-
(isNaN(cell.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%') + trend(cell.delta),
155+
(!isFinite(cell?.value) ? '-' : cell.value.toString()),
156+
(!isFinite(cell?.delta) ? '-' : plusSign(cell.delta) + cell.delta.toString()),
157+
(!isFinite(cell?.prcnt) ? '-' : plusSign(cell.prcnt) + cell.prcnt.toFixed(2) + '%' + trend(cell.delta)),
138158
];
139159
}
140160

@@ -146,10 +166,10 @@ function formatCmpMarkdown (rows) {
146166
{ txt: 'Min', align: 'right' },
147167
{ txt: '(+/-)', align: 'right' },
148168
{ txt: '%', align: 'right' },
149-
{ txt: 'Avg', align: 'right' },
169+
{ txt: 'Max', align: 'right' },
150170
{ txt: '(+/-)', align: 'right' },
151171
{ txt: '%', align: 'right' },
152-
{ txt: 'Max', align: 'right' },
172+
{ txt: 'Avg', align: 'right' },
153173
{ txt: '(+/-)', align: 'right' },
154174
{ txt: '%', align: 'right' },
155175
{ txt: '' },
@@ -167,8 +187,8 @@ function formatCmpMarkdown (rows) {
167187
entry.contract,
168188
entry.method,
169189
...formatCellMarkdown(entry.min),
170-
...formatCellMarkdown(entry.avg),
171190
...formatCellMarkdown(entry.max),
191+
...formatCellMarkdown(entry.avg),
172192
'',
173193
].join(' | ').trim()).join('\n'),
174194
'',

0 commit comments

Comments
 (0)