2121 options :
2222 - ' latest'
2323 - ' rebaseline'
24+ pull :
25+ description : ' Pull request number to benchmark (optional)'
26+ required : false
2427
2528jobs :
26- tests :
27- name : Benchmarks
29+ pr-benchmarks :
30+ name : PR Benchmarks
31+ if : ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.pull }}
2832 runs-on : ubuntu-latest
33+ env :
34+ BENCHMARK_SHA : ${{ github.sha }}
2935
3036 permissions :
31- contents : write
37+ contents : read
3238
3339 steps :
3440 - uses : actions/checkout@v6
3541 with :
36- persist-credentials : true
42+ persist-credentials : false
43+
44+ - name : Checkout PR head
45+ run : |
46+ set -euo pipefail
47+ PR=${{ github.event.inputs.pull }}
48+ # Validate that PR is numeric to avoid injection or unexpected ref resolution
49+ if ! printf '%s\n' "$PR" | grep -Eq '^[0-9]+$'; then
50+ echo "Invalid pull request number: '$PR'. Expected a numeric value." >&2
51+ exit 1
52+ fi
53+ # fetch the pull request head (works for forks)
54+ git fetch origin "pull/${PR}/head:pr-${PR}" || git fetch origin "+refs/pull/${PR}/head:pr-${PR}"
55+ git checkout "pr-${PR}"
56+ echo "Checked out PR #${PR}"
57+ echo "BENCHMARK_SHA=$(git rev-parse --verify HEAD)" >> "$GITHUB_ENV"
58+
3759 - uses : ./.github/actions/setup-action
3860 with :
3961 extensions : xdebug
@@ -48,13 +70,57 @@ jobs:
4870 run : |
4971 # Baseline does not exist or rebaseline requested. Generate it.
5072 if [ -z "$(ls -A .phpbench)" ] || [ "${{ github.event.inputs.baseline || 'latest' }}" = "rebaseline" ]; then
51- vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${GITHUB_SHA }
73+ vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${BENCHMARK_SHA }
5274
5375 # Baseline exists. Compare against it.
5476 else
55- vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${GITHUB_SHA } --ref=latest --tolerate-failure
77+ vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${BENCHMARK_SHA } --ref=latest --tolerate-failure
5678 fi
5779
80+ # Generate report for human consumption
81+ vendor/bin/phpbench report --report=aggregate --ref=latest |
82+ tail -n+2 | head -n-2 | tr '+' '|' > report.md
83+
84+ cat report.md > "$GITHUB_STEP_SUMMARY"
85+
86+ historical-benchmarks :
87+ name : Historical Benchmarks
88+ if : >-
89+ github.event_name != 'workflow_dispatch' ||
90+ !github.event.inputs.pull
91+ runs-on : ubuntu-latest
92+ env :
93+ BENCHMARK_SHA : ${{ github.sha }}
94+
95+ permissions :
96+ contents : write
97+
98+ steps :
99+ - uses : actions/checkout@v6
100+ with :
101+ persist-credentials : true
102+
103+ - uses : ./.github/actions/setup-action
104+ with :
105+ extensions : xdebug
106+
107+ - name : Fetch Benchmarks
108+ run : |
109+ git fetch origin benchmarks
110+ mkdir -p .phpbench
111+ git checkout origin/benchmarks -- .phpbench || echo "No previous benchmarks found"
112+
113+ - name : Run Benchmarks
114+ run : |
115+ # Baseline does not exist or rebaseline requested. Generate it.
116+ if [ -z "$(ls -A .phpbench)" ] || [ "${{ github.event.inputs.baseline || 'latest' }}" = "rebaseline" ]; then
117+ vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${BENCHMARK_SHA}
118+
119+ # Baseline exists. Compare against it.
120+ else
121+ vendor/bin/phpbench run --report=aggregate --progress=plain --store --tag=${BENCHMARK_SHA} --ref=latest --tolerate-failure
122+ fi
123+
58124 # Generate report for human consumption
59125 vendor/bin/phpbench report --report=aggregate --ref=latest |
60126 tail -n+2 | head -n-2 | tr '+' '|' > report.md
0 commit comments