Skip to content

Commit

Permalink
Merge branch 'main' into MLOAD-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeljy authored Sep 7, 2023
2 parents d6422c3 + 1a65503 commit 8b401af
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gas_reports.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Compare Snapshot

on: pull_request_target
on: pull_request

permissions:
pull-requests: write
Expand Down
69 changes: 52 additions & 17 deletions scripts/compare_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import subprocess
import re
import json
import os
import re
import subprocess

# ANSI escape codes for coloring text
GREEN = '\033[92m'
RED = '\033[91m'
ENDC = '\033[0m'
GREEN = "\033[92m"
RED = "\033[91m"
ENDC = "\033[0m"


def get_github_token_from_env(file_path=".env"):
"""Read the .env file and extract the GITHUB_TOKEN value."""
Expand All @@ -15,7 +16,7 @@ def get_github_token_from_env(file_path=".env"):
for line in file:
if line.startswith("#"):
continue
key, value = line.strip().split('=', 1)
key, value = line.strip().split("=", 1)
if key == "GITHUB_TOKEN":
return value
except FileNotFoundError:
Expand All @@ -24,9 +25,14 @@ def get_github_token_from_env(file_path=".env"):
print(f"Error: Invalid format in {file_path}. Expected 'KEY=VALUE' format.")
return None


def get_previous_snapshot():
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")
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", get_github_token_from_env())
if GITHUB_TOKEN is None:
raise ValueError(
"GITHUB_TOKEN doesn't exist in current shell nor is defined .env"
)

try:
# Fetch the list of workflow runs
Expand All @@ -35,7 +41,15 @@ def get_previous_snapshot():
runs = json.loads(result)

# Find the latest successful run
latest_successful_run = next((run for run in runs["workflow_runs"] if run["conclusion"] == "success" and run["name"] == "Generate and Upload Gas Snapshot"), None)
latest_successful_run = next(
(
run
for run in runs["workflow_runs"]
if run["conclusion"] == "success"
and run["name"] == "Generate and Upload Gas Snapshot"
),
None,
)

# Fetch the artifacts for that run
run_id = latest_successful_run["id"]
Expand All @@ -44,7 +58,11 @@ def get_previous_snapshot():
artifacts = json.loads(result)

# Find the gas_snapshot.json artifact
snapshot_artifact = next(artifact for artifact in artifacts["artifacts"] if artifact["name"] == "gas-snapshot")
snapshot_artifact = next(
artifact
for artifact in artifacts["artifacts"]
if artifact["name"] == "gas-snapshot"
)

# Download the gas_snapshots.json archive
archive_name = "gas_snapshot.zip"
Expand Down Expand Up @@ -77,12 +95,13 @@ def get_previous_snapshot():

def get_current_gas_snapshot():
"""Execute command and return current gas snapshots."""
output = subprocess.check_output("scarb cairo-test", shell=True).decode('utf-8')
output = subprocess.check_output("scarb cairo-test", shell=True).decode("utf-8")
pattern = r"test (.+?) \.\.\. ok \(gas usage est.: (\d+)\)"
matches = re.findall(pattern, output)
matches.sort()
return {match[0]: int(match[1]) for match in matches}


def compare_snapshots(current, previous):
"""Compare current and previous snapshots and return differences."""
worsened = []
Expand All @@ -93,14 +112,19 @@ def compare_snapshots(current, previous):
continue
prev = previous[key]
cur = current[key]
percentage_change = (cur - prev)*100/prev
percentage_change = (cur - prev) * 100 / prev
if prev < cur:
worsened.append(f"{key} {prev} --> {cur} {format(percentage_change, '.2f')} %")
worsened.append(
f"{key} {prev} --> {cur} {format(percentage_change, '.2f')} %"
)
elif prev > cur:
improvements.append(f"{key} {prev} --> {cur} | {format(percentage_change, '.2f')} %" )
improvements.append(
f"{key} {prev} --> {cur} | {format(percentage_change, '.2f')} %"
)

return improvements, worsened


def print_colored_output(improvements, worsened, gas_changes):
"""Print results in a colored format."""
if improvements or worsened:
Expand All @@ -114,15 +138,25 @@ def print_colored_output(improvements, worsened, gas_changes):
print(RED + elem + ENDC)

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)
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):

def total_gas_used(current, previous):
"""Return the total gas used in the current and previous snapshot."""
return sum(current.values()), sum(previous.values())


def main():
"""Main function to execute the snapshot test framework."""
# Load previous snapshot
Expand All @@ -134,7 +168,8 @@ def main():
current_snapshots = get_current_gas_snapshot()
improvements, worsened = compare_snapshots(current_snapshots, previous_snapshot)
cur_gas, prev_gas = total_gas_used(current_snapshots, previous_snapshot)
print_colored_output(improvements, worsened, (cur_gas-prev_gas)*100/prev_gas)
print_colored_output(improvements, worsened, (cur_gas - prev_gas) * 100 / prev_gas)


if __name__ == "__main__":
main()
20 changes: 2 additions & 18 deletions scripts/install_hook.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
#!/bin/bash

# Define the hook content
HOOK_CONTENT='#!/bin/sh
# Run the compare_snapshot.py script
echo "Running gas snapshot comparison..."
python scripts/compare_snapshot.py
# Check the return status of the script
if [ $? -ne 0 ]; then
echo "Error: Snapshot comparison failed!"
exit 1
fi'

COMMIT_CONTENT='#!/bin/sh
PRE_COMMIT='#!/bin/sh
# Run scarb fmt to format the project.
scarb fmt
Expand Down Expand Up @@ -41,13 +28,10 @@ if [ ! -d .git ]; then
fi

# Write the hook content to the pre-push file
echo "$COMMIT_CONTENT" > .git/hooks/pre-commit
echo "$HOOK_CONTENT" > .git/hooks/pre-push
echo "$PRE_COMMIT" > .git/hooks/pre-commit

# Make the hook executable
chmod +x .git/hooks/pre-commit
chmod +x .git/hooks/pre-push

echo "pre-commit hook has been installed successfully!"
echo "pre-push hook has been installed successfully!"

0 comments on commit 8b401af

Please sign in to comment.