Skip to content

Commit 4ccf8b0

Browse files
ductm54ilanDoron
andcommitted
Add reports for CI and local compilation. Will report contract size changes and and trade gas consumption changes. (#947)
Co-authored-by: Ilan Doron <[email protected]>
1 parent fa8c76d commit 4ccf8b0

10 files changed

+743
-44
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ jspm_packages
4141

4242
# OS file
4343
.DS_Store
44+
45+
report

.travis.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
sudo: required
2-
31
language: node_js
42
node_js:
53
- 10
@@ -13,7 +11,7 @@ env:
1311
global:
1412
- COVERAGE_BRANCH=Katalyst
1513

16-
matrix:
14+
jobs:
1715
include:
1816
- env:
1917
- TEST_PART: Sol6
@@ -23,14 +21,27 @@ matrix:
2321
install:
2422
- npm ci
2523

26-
before script:
24+
before_script:
2725
- npx solhint contracts/sol6/**/*.sol
2826

2927
script:
3028
- ./cmp.sh
3129
- .travis/test-part.sh
30+
- .travis/report.sh
3231

3332
deploy:
33+
- provider: s3
34+
access_key_id: $AWS_ACCESS_KEY_ID
35+
secret_access_key: $AWS_SECRET_ACCESS_KEY
36+
bucket: katalyst-coverage.knstats.com
37+
region: ap-southeast-1
38+
acl: public_read
39+
local_dir: report
40+
upload_dir: report
41+
skip_cleanup: true
42+
on:
43+
all_branches: true
44+
condition: $TEST_PART = Sol6
3445
- provider: s3
3546
access_key_id: $AWS_ACCESS_KEY_ID
3647
secret_access_key: $AWS_SECRET_ACCESS_KEY

.travis/report.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
node contractSizeReport.js $TRAVIS_PULL_REQUEST_BRANCH
4+
node gasUsedReport.js $TRAVIS_PULL_REQUEST_BRANCH

cmp.sh

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export NODE_OPTIONS=--max-old-space-size=4096
33
npx buidler compile
44
npx buidler compile --config buidlerConfigSol5.js
55
npx buidler compile --config buidlerConfigSol4.js
6+
node contractSizeReport.js

cmpSol6.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/sh
22
npx buidler compile
3+
node contractSizeReport.js

contractSizeReport.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
const fs = require('fs');
3+
const util = require('util');
4+
const got = require('got');
5+
const yargs = require('yargs');
6+
7+
let path = 'artifacts';
8+
9+
const readdir = util.promisify(fs.readdir);
10+
11+
let argv = yargs.default('branch', 'Katalyst').alias('b', 'branch').argv;
12+
13+
async function generateCodeSizeReport() {
14+
let result = {};
15+
let fileNames = await readdir(path);
16+
for (let i = 0; i < fileNames.length; i++) {
17+
let fileName = fileNames[i];
18+
let rawData = fs.readFileSync(path + '/' + fileName);
19+
let contractData = JSON.parse(rawData);
20+
let codeSize = contractData.deployedBytecode.length / 2 - 1;
21+
if (codeSize > 0) {
22+
result[fileName] = codeSize;
23+
}
24+
}
25+
return result;
26+
}
27+
28+
async function writeReport(report) {
29+
let jsonContent = JSON.stringify(report, null, '\t');
30+
let reportDir = 'report';
31+
if (process.env.TRAVIS_BRANCH !== undefined) {
32+
reportDir = `report/${process.env.TRAVIS_BRANCH}`;
33+
}
34+
let reportFile = `${reportDir}/contractSize.json`;
35+
if (!fs.existsSync(reportDir)) {
36+
fs.mkdirSync(reportDir, {recursive: true});
37+
}
38+
fs.writeFile(reportFile, jsonContent, 'utf8', function (err) {
39+
if (err) {
40+
console.log('An error occured while writing JSON Object to File.');
41+
return console.log(err);
42+
}
43+
});
44+
}
45+
46+
async function getRemoteReport() {
47+
try {
48+
const url = `http://katalyst-coverage.knstats.com/report/${argv.branch}/contractSize.json`;
49+
return await got(url).json();
50+
} catch (error) {
51+
// console.log(error);
52+
return false;
53+
}
54+
}
55+
56+
async function compareContractSize() {
57+
let contractSizeReport = await generateCodeSizeReport();
58+
await writeReport(contractSizeReport);
59+
let remoteReport = await getRemoteReport();
60+
if (!remoteReport) {
61+
console.log(`Could not get report for ${argv.branch}`);
62+
console.log("Current contract size report");
63+
console.table(contractSizeReport);
64+
return false;
65+
}
66+
let diffDict = {};
67+
for (let contract in contractSizeReport) {
68+
if (contract in remoteReport) {
69+
let baseBranchSize = remoteReport[contract];
70+
let currentSize = contractSizeReport[contract];
71+
let diff = currentSize - baseBranchSize;
72+
if (diff != 0) {
73+
diffDict[contract] = {
74+
[argv.branch]: baseBranchSize,
75+
current: currentSize,
76+
diff: diff,
77+
};
78+
}
79+
}
80+
}
81+
for (let contract in remoteReport) {
82+
if (contract in remoteReport && !(contract in diffDict)) {
83+
let baseBranchSize = remoteReport[contract];
84+
let currentSize = contractSizeReport[contract];
85+
let diff = currentSize - baseBranchSize;
86+
if (diff != 0) {
87+
diffDict[contract] = {
88+
[argv.branch]: baseBranchSize,
89+
current: currentSize,
90+
diff: diff,
91+
};
92+
}
93+
}
94+
}
95+
if (Object.keys(diffDict).length > 0) {
96+
console.log(`There is change in following contract size with ${argv.branch}`);
97+
console.table(diffDict);
98+
} else {
99+
console.log("Contract size didn't change");
100+
console.table(contractSizeReport);
101+
}
102+
}
103+
104+
compareContractSize();

gasUsedReport.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
const fs = require('fs');
3+
const util = require('util');
4+
const got = require('got');
5+
const yargs = require('yargs');
6+
7+
let argv = yargs.default('branch', 'Katalyst').alias('b', 'branch').argv;
8+
9+
async function getRemoteReport() {
10+
try {
11+
const url = `http://katalyst-coverage.knstats.com/report/${argv.branch}/gasUsed.json`;
12+
return await got(url).json();
13+
} catch (error) {
14+
// console.log(error);
15+
return false;
16+
}
17+
}
18+
19+
async function getLocalReport() {
20+
let reportFile = `report/gasUsed.json`;
21+
if (process.env.TRAVIS_BRANCH !== undefined) {
22+
reportFile = `report/${process.env.TRAVIS_BRANCH}/gasUsed.json`;
23+
}
24+
let report = JSON.parse(fs.readFileSync(reportFile, 'utf8'));
25+
return report
26+
}
27+
28+
async function compareGasConsumtion() {
29+
let localReport = await getLocalReport();
30+
let remoteReport = await getRemoteReport();
31+
if (!remoteReport) {
32+
console.log(`Could not get report for ${argv.branch}. Current report`);
33+
console.table(localReport);
34+
return false;
35+
}
36+
let diffDict = {};
37+
for (let testCase in localReport) {
38+
if (testCase in remoteReport) {
39+
let baseBranchSize = remoteReport[testCase];
40+
let currentSize = localReport[testCase];
41+
let diff = currentSize - baseBranchSize;
42+
if (diff != 0) {
43+
diffDict[testCase] = {
44+
[argv.branch]: baseBranchSize,
45+
current: currentSize,
46+
diff: diff,
47+
};
48+
}
49+
}
50+
}
51+
for (let testCase in remoteReport) {
52+
if (testCase in remoteReport && !(testCase in diffDict)) {
53+
let baseBranchSize = remoteReport[testCase];
54+
let currentSize = localReport[testCase];
55+
let diff = currentSize - baseBranchSize;
56+
if (diff != 0) {
57+
diffDict[testCase] = {
58+
[argv.branch]: baseBranchSize,
59+
current: currentSize,
60+
diff: diff,
61+
};
62+
}
63+
}
64+
}
65+
if (Object.keys(diffDict).length > 0) {
66+
console.log(`There is change in following gas report with ${argv.branch}`);
67+
console.table(diffDict);
68+
} else {
69+
console.log("Gas report didn't change");
70+
console.table(localReport);
71+
}
72+
}
73+
74+
compareGasConsumtion();

0 commit comments

Comments
 (0)