Skip to content

Commit

Permalink
chore: Update gas report script and add gas report comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Aboudjem committed May 31, 2024
1 parent 7629514 commit cd764c2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/gas_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Gas Report

on:
pull_request:
branches:
- dev
- main

jobs:
generate-gas-report:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for git diff

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Cache Yarn dependencies
uses: actions/cache@v4
with:
path: |
~/.cache/yarn
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install

- name: Run gas report script
run: node generateGasReport.js

- name: Upload gas report artifact
uses: actions/upload-artifact@v4
with:
name: gas-report-${{ github.sha }}
path: GAS_REPORT.md

compare-gas-report:
runs-on: ubuntu-latest
needs: generate-gas-report
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Download previous gas report
uses: actions/download-artifact@v4
with:
name: gas-report-${{ github.event.pull_request.base.sha }}
path: previous_gas_report

- name: Download current gas report
uses: actions/download-artifact@v4
with:
name: gas-report-${{ github.sha }}
path: current_gas_report

- name: Compare gas reports
run: |
node compareGasReports.js previous_gas_report/GAS_REPORT.md current_gas_report/GAS_REPORT.md > gas_report_diff.md
id: compare-gas-reports

- name: Upload gas report comparison
uses: actions/upload-artifact@v4
with:
name: gas-report-comparison-${{ github.sha }}
path: gas_report_diff.md

- name: Create pull request comment
uses: marocchino/sticky-pull-request-comment@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
header: Gas Report Comparison
message: |
```diff
${{ steps.compare-gas-reports.outputs.gas_report_diff }}
```
63 changes: 63 additions & 0 deletions scripts/foundry/compareGasReports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fs = require('fs');

function compareGasReports(prevFile, currFile) {
const prevData = fs.readFileSync(prevFile, 'utf8');
const currData = fs.readFileSync(currFile, 'utf8');

const prevLines = prevData.split('\n');
const currLines = currData.split('\n');

const diffLines = ['# Gas Report Comparison', '| **Protocol** | **Actions / Function** | **Account Type** | **Is Deployed** | **With Paymaster?** | **Receiver Access** | **Gas Used** | **Gas Difference** |', '|:------------:|:---------------------:|:----------------:|:--------------:|:-------------------:|:-------------------:|:------------:|:------------------:|'];

const prevResults = parseGasReport(prevLines);
const currResults = parseGasReport(currLines);

currResults.forEach(curr => {
const prev = prevResults.find(prev => prev.NUMBER === curr.NUMBER);
let gasDiff = '0';
let gasDiffEmoji = '';

if (prev) {
const diff = curr.GAS_USED - prev.GAS_USED;
gasDiff = diff.toString();
gasDiffEmoji = diff > 0 ? '🥵' : (diff < 0 ? '🥳' : '');
}

diffLines.push(`| ${curr.PROTOCOL} | ${curr.ACTION_FUNCTION} | ${curr.ACCOUNT_TYPE} | ${curr.IS_DEPLOYED} | ${curr.WITH_PAYMASTER} | ${curr.RECEIVER_ACCESS} | ${curr.GAS_USED} | ${gasDiffEmoji} ${gasDiff} |`);
});

return diffLines.join('\n');
}

function parseGasReport(lines) {
const results = [];
for (const line of lines) {
if (line.startsWith('|')) {
const parts = line.split('|').map(part => part.trim());
if (parts.length === 9 && !parts[0].includes('**')) {
results.push({
PROTOCOL: parts[1],
ACTION_FUNCTION: parts[2],
ACCOUNT_TYPE: parts[3],
IS_DEPLOYED: parts[4],
WITH_PAYMASTER: parts[5],
RECEIVER_ACCESS: parts[6],
GAS_USED: parseInt(parts[7], 10),
NUMBER: parseInt(parts[7], 10) // Adjusted the assignment to ensure it has a unique identifier
});
}
}
}
return results;
}

if (process.argv.length !== 4) {
console.error('Usage: node compareGasReports.js <previous_gas_report> <current_gas_report>');
process.exit(1);
}

const previousFile = process.argv[2];
const currentFile = process.argv[3];

const comparison = compareGasReports(previousFile, currentFile);
console.log(comparison);

0 comments on commit cd764c2

Please sign in to comment.