|
| 1 | +name: Python Benchmarks |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + full_benchmark: |
| 11 | + description: 'Run full benchmark suite (slower)' |
| 12 | + required: false |
| 13 | + default: 'false' |
| 14 | + type: boolean |
| 15 | + |
| 16 | +env: |
| 17 | + UV_SYSTEM_PYTHON: 1 |
| 18 | + |
| 19 | +jobs: |
| 20 | + benchmark: |
| 21 | + name: Benchmark Python ${{ matrix.python-version }} |
| 22 | + runs-on: ubuntu-latest |
| 23 | + strategy: |
| 24 | + fail-fast: false |
| 25 | + matrix: |
| 26 | + python-version: |
| 27 | + - "3.12" |
| 28 | + - "3.13" |
| 29 | + - "3.14" |
| 30 | + - "3.14t" # Free-threaded |
| 31 | + |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + |
| 35 | + - name: Install uv |
| 36 | + uses: astral-sh/setup-uv@v4 |
| 37 | + with: |
| 38 | + version: "latest" |
| 39 | + |
| 40 | + - name: Set up Python ${{ matrix.python-version }} |
| 41 | + run: | |
| 42 | + uv python install ${{ matrix.python-version }} |
| 43 | + uv venv --python ${{ matrix.python-version }} .venv |
| 44 | +
|
| 45 | + - name: Install dependencies |
| 46 | + run: | |
| 47 | + uv pip install pytest pytest-benchmark pytest-asyncio numpy --python .venv/bin/python |
| 48 | +
|
| 49 | + - name: Check Python version and GIL status |
| 50 | + run: | |
| 51 | + .venv/bin/python -c " |
| 52 | + import sys |
| 53 | + print(f'Python version: {sys.version}') |
| 54 | + print(f'GIL enabled: {sys._is_gil_enabled()}') |
| 55 | + " |
| 56 | +
|
| 57 | + - name: Run quick benchmarks (PR) |
| 58 | + if: github.event_name == 'pull_request' |
| 59 | + run: | |
| 60 | + .venv/bin/pytest benchmarks/ \ |
| 61 | + --benchmark-only \ |
| 62 | + --benchmark-json=benchmark_results_${{ matrix.python-version }}.json \ |
| 63 | + --benchmark-min-rounds=3 \ |
| 64 | + --benchmark-max-time=0.5 \ |
| 65 | + -x \ |
| 66 | + -q \ |
| 67 | + --ignore=benchmarks/pytorch/ \ |
| 68 | + 2>&1 | tee benchmark_output.txt |
| 69 | +
|
| 70 | + - name: Run full benchmarks (push to main) |
| 71 | + if: github.event_name == 'push' || github.event.inputs.full_benchmark == 'true' |
| 72 | + run: | |
| 73 | + .venv/bin/pytest benchmarks/ \ |
| 74 | + --benchmark-only \ |
| 75 | + --benchmark-json=benchmark_results_${{ matrix.python-version }}.json \ |
| 76 | + --benchmark-min-rounds=5 \ |
| 77 | + -q \ |
| 78 | + --ignore=benchmarks/pytorch/ \ |
| 79 | + 2>&1 | tee benchmark_output.txt |
| 80 | +
|
| 81 | + - name: Upload benchmark results |
| 82 | + uses: actions/upload-artifact@v4 |
| 83 | + with: |
| 84 | + name: benchmark-results-${{ matrix.python-version }} |
| 85 | + path: | |
| 86 | + benchmark_results_*.json |
| 87 | + benchmark_output.txt |
| 88 | + retention-days: 30 |
| 89 | + |
| 90 | + compare: |
| 91 | + name: Compare Results |
| 92 | + needs: benchmark |
| 93 | + runs-on: ubuntu-latest |
| 94 | + if: github.event_name == 'push' |
| 95 | + |
| 96 | + steps: |
| 97 | + - uses: actions/checkout@v4 |
| 98 | + |
| 99 | + - name: Download all benchmark results |
| 100 | + uses: actions/download-artifact@v4 |
| 101 | + with: |
| 102 | + pattern: benchmark-results-* |
| 103 | + merge-multiple: true |
| 104 | + |
| 105 | + - name: Install uv and dependencies |
| 106 | + run: | |
| 107 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 108 | + source $HOME/.local/bin/env |
| 109 | + uv pip install --system tabulate |
| 110 | +
|
| 111 | + - name: Generate comparison report |
| 112 | + run: | |
| 113 | + python3 << 'EOF' |
| 114 | + import json |
| 115 | + import glob |
| 116 | + from pathlib import Path |
| 117 | +
|
| 118 | + results = {} |
| 119 | + for f in glob.glob("benchmark_results_*.json"): |
| 120 | + version = f.replace("benchmark_results_", "").replace(".json", "") |
| 121 | + with open(f) as fp: |
| 122 | + data = json.load(fp) |
| 123 | + results[version] = { |
| 124 | + b["name"]: b["stats"]["mean"] |
| 125 | + for b in data.get("benchmarks", []) |
| 126 | + } |
| 127 | +
|
| 128 | + print("# Benchmark Comparison Report\n") |
| 129 | + print(f"Versions compared: {', '.join(sorted(results.keys()))}\n") |
| 130 | +
|
| 131 | + # Find common benchmarks |
| 132 | + if results: |
| 133 | + common = set.intersection(*[set(r.keys()) for r in results.values()]) |
| 134 | + print(f"Common benchmarks: {len(common)}\n") |
| 135 | +
|
| 136 | + # Compare 3.14 vs 3.14t if both exist |
| 137 | + if "3.14" in results and "3.14t" in results: |
| 138 | + print("## GIL vs Free-threaded Comparison (3.14 vs 3.14t)\n") |
| 139 | +
|
| 140 | + faster_ft = 0 |
| 141 | + slower_ft = 0 |
| 142 | +
|
| 143 | + for name in sorted(common)[:20]: # Top 20 for brevity |
| 144 | + gil_time = results["3.14"].get(name, 0) |
| 145 | + ft_time = results["3.14t"].get(name, 0) |
| 146 | + if gil_time and ft_time: |
| 147 | + ratio = gil_time / ft_time |
| 148 | + status = "🚀" if ratio > 1.1 else ("🐢" if ratio < 0.9 else "➡️") |
| 149 | + print(f"- {status} {name.split('::')[-1][:40]}: {ratio:.2f}x") |
| 150 | + if ratio > 1.1: |
| 151 | + faster_ft += 1 |
| 152 | + elif ratio < 0.9: |
| 153 | + slower_ft += 1 |
| 154 | +
|
| 155 | + print(f"\nSummary: {faster_ft} faster in free-threaded, {slower_ft} slower") |
| 156 | + EOF |
| 157 | +
|
| 158 | + - name: Create summary |
| 159 | + run: | |
| 160 | + echo "## Benchmark Results" >> $GITHUB_STEP_SUMMARY |
| 161 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 162 | + echo "Benchmark results have been uploaded as artifacts." >> $GITHUB_STEP_SUMMARY |
| 163 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 164 | + ls -la benchmark_results_*.json >> $GITHUB_STEP_SUMMARY 2>/dev/null || echo "No results found" |
0 commit comments