Skip to content

Commit

Permalink
feat: add gas-comparison PR bot (#173)
Browse files Browse the repository at this point in the history
* feat: add gas-comparison PR bot

* chore: changelog
  • Loading branch information
enitrat committed Aug 23, 2023
1 parent fdfff3c commit b72d0d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/gas_reports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Compare Snapshot

on:
pull_request:
paths:
- "scripts/compare_snapshot.py"

jobs:
compare-snapshot:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.x" # Specify the Python version required

- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "nightly-2023-08-12"

- name: Run compare_snapshot script
id: run-script
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: |
result=$(python scripts/compare_snapshot.py)
result=$(echo "$result" | tail -n +3) # Strip the first two lines
result="${result//'%'/'%25'}"
result="${result//$'\n'/'%0A'}"
result="${result//$'\r'/'%0D'}"
echo "::set-output name=result::${result}"
- name: Comment on PR
uses: thollander/actions-comment-pull-request@v2
with:
message: "Snapshot Comparison Report:\n\n${{
steps.run-script.outputs.result }}" # Access the result output variable
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
comment_tag: gas-report
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to

### Added

- tooling: PR bot displays gas changes induced by PR
- refactor(22/08/2023): refactor project to use scarb workspace
- opcode(22/08/2023): add 0x1A-BYTE opcode
- tooling: CI now generates gas snapshots artifacts. pre-push hook to compare
Expand Down
12 changes: 6 additions & 6 deletions scripts/compare_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ def get_github_token_from_env(file_path=".env"):
if key == "GITHUB_TOKEN":
return value
except FileNotFoundError:
print(f"Error: {file_path} file not found.")
return None
except ValueError:
print(f"Error: Invalid format in {file_path}. Expected 'KEY=VALUE' format.")
return None

def get_previous_snapshot():
REPO = "enitrat/kakarot-ssj" # Replace with your GitHub username and repo name
GITHUB_TOKEN = get_github_token_from_env()
REPO = "kkrt-labs/kakarot-ssj" # Replace with your GitHub username and repo name
GITHUB_TOKEN = get_github_token_from_env() or os.environ.get("GITHUB_TOKEN")

try:
REPO = "kkrt-labs/kakarot-ssj"

# Fetch the list of workflow runs
cmd = f"curl -s -H 'Authorization: token {GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/repos/{REPO}/actions/runs'"
result = subprocess.check_output(cmd, shell=True)
runs = json.loads(result)

# Find the latest successful run
latest_successful_run = next(run for run in runs["workflow_runs"] if run["conclusion"] == "success")
latest_successful_run = next(run for run in runs["workflow_runs"] if (run["conclusion"] == "success" and run["head_branch"] == "main"))

# Fetch the artifacts for that run
run_id = latest_successful_run["id"]
Expand Down Expand Up @@ -118,6 +116,8 @@ def print_colored_output(improvements, worsened, gas_changes):
color = RED if gas_changes > 0 else GREEN
gas_statement = "performance degradation, gas consumption +" if gas_changes > 0 else "performance improvement, gas consumption"
print(color + f"Overall gas change: {gas_statement}{format(gas_changes, '.2f')} %" + ENDC)
else:
print("No changes in gas consumption.")

def total_gas_used(current,previous):
"""Return the total gas used in the current and previous snapshot."""
Expand Down

0 comments on commit b72d0d6

Please sign in to comment.