Skip to content

Commit

Permalink
ci(GitHub): compare memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Aug 2, 2024
1 parent a02db2d commit e2d0e4d
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 7 deletions.
85 changes: 85 additions & 0 deletions .github/scripts/compare-memory-usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import json
import pandas as pd
import argparse


# Function to load JSON data from files
def load_json(filename):
with open(filename, 'r') as f:
return json.load(f)


# Function to compare values and create formatted strings
def compare_values(base, current, key):
if key not in base and key not in current:
return None
if key not in base:
return f"🆕 {current[key]}"
if key not in current:
return f"❌"

base_value = base[key]
current_value = current[key]

if base_value == current_value:
return f"🆗 {base_value}"

change = current_value - base_value
sign = "+" if change > 0 else "-"
symbol = "⚠️" if change > 0 else "⬇️"

# round to 2 decimal places
change = round(abs(change), 2)
return f"{symbol} {current_value} <sub>{sign}{change}</sub>"


# Function to generate Markdown table
def generate_markdown_table(base_data, current_data):
table_data = []

all_keys = set(base_data.keys()).union(set(current_data.keys()))
for key in all_keys:
base_ram = base_data.get(key, {}).get("ram", {})
current_ram = current_data.get(key, {}).get("ram", {})

base_flash = base_data.get(key, {}).get("flash", {})
current_flash = current_data.get(key, {}).get("flash", {})

row = [
key,
compare_values(base_ram, current_ram, "percentage"),
compare_values(base_ram, current_ram, "used"),
compare_values(base_flash, current_flash, "percentage"),
compare_values(base_flash, current_flash, "used")
]
table_data.append(row)

df = pd.DataFrame(table_data, columns=["Variant", "RAM, %", "RAM, bytes", "Flash, %", "Flash, bytes"])

return df.to_markdown(index=False)


# Main function to handle CLI arguments and execute the script
def main():
parser = argparse.ArgumentParser(description='Compare JSON files and generate a markdown table.')
parser.add_argument('base', type=str, help='Path to the base JSON file')
parser.add_argument('current', type=str, help='Path to the current JSON file')
parser.add_argument('--output', type=str, help='Path to the output markdown file')
args = parser.parse_args()

base_data = load_json(args.base)
current_data = load_json(args.current)

markdown_table = generate_markdown_table(base_data, current_data)

if args.output:
with open(args.output, 'w') as f:
f.write(markdown_table)
print(f"Markdown table generated and saved to {args.output}")
else:
print(markdown_table)


# usage: python compare-memory-usage.py base.json current.json --output table.md
if __name__ == "__main__":
main()
74 changes: 67 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ jobs:
coverage: true
battery_flag: SS_BATTERY_ENABLED=true
nimble_flag: SS_USE_NIMBLE=true
# - target: bhaptics_tactsuit_x40
# os: ubuntu-latest
# coverage: true
# battery_flag: SS_BATTERY_ENABLED=true
# nimble_flag: SS_USE_NIMBLE=false
- target: bhaptics_tactsuit_x40
os: ubuntu-latest
coverage: true
battery_flag: SS_BATTERY_ENABLED=true
nimble_flag: SS_USE_NIMBLE=false
- target: bhaptics_tactsuit_x40
os: ubuntu-latest
coverage: false
Expand Down Expand Up @@ -532,8 +532,6 @@ jobs:
pattern: '*-usage'
merge-multiple: true

- run: ls -lahR current-usage

- name: Merge usage files
shell: python
run: |
Expand Down Expand Up @@ -579,6 +577,68 @@ jobs:
name: merged-usage
path: current-usage/merged.json

- name: Get base branch job ID
if: github.event_name == 'pull_request'
id: base_branch_workflow
uses: actions/github-script@v7
with:
script: |
const { data: workflows } = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
branch: context.payload.pull_request.base.ref,
event: 'push',
workflow_id: 'ci.yml',
status: 'completed',
per_page: 1
});
const baseBranchJobId = workflows.workflow_runs[0].id;
console.log(baseBranchJobId);
return baseBranchJobId;
- name: Download base branch usage.json artifact
if: github.event_name == 'pull_request'
id: download-base-usage
uses: actions/download-artifact@v4
with:
name: merged-usage
path: base-usage
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
run-id: ${{ steps.base_branch_workflow.outputs.result }}

- uses: actions/setup-python@v2
if: github.event_name == 'pull_request'
with:
python-version: '3.x'

- name: Report memory usage table
if: github.event_name == 'pull_request'
shell: bash
run: |
python -m pip install --upgrade pip pandas tabulate
cat ./base-usage/merged.json
cat ./current-usage/merged.json
echo "## Memory Usage Comparison" > memory-usage-comparison.md
echo "" >> memory-usage-comparison.md
echo "<details>" >> memory-usage-comparison.md
echo " <summary>Click to expand</summary>" >> memory-usage-comparison.md
echo "" >> memory-usage-comparison.md
python ./.github/scripts/compare-memory-usage.py ./base-usage/merged.json ./current-usage/merged.json | tee -a memory-usage-comparison.md
echo "</details>" >> memory-usage-comparison.md
cat ./memory-usage-comparison.md > $GITHUB_STEP_SUMMARY
- uses: thollander/actions-comment-pull-request@v2
if: github.event_name == 'pull_request'
with:
filePath: memory-usage-comparison.md
comment_tag: memory-usage-comparison
wokwi:
needs:
- build-bhaptics
Expand Down

0 comments on commit e2d0e4d

Please sign in to comment.