From bf2bb5261cf84958c295dd74e763837e2c021fd8 Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Tue, 23 Jun 2026 12:04:13 +0200 Subject: [PATCH] benchmarks: split off script, emit markdown table Follow-up to #8811. Signed-off-by: Stephan Renatus --- .github/workflows/benchmarks.yaml | 45 ++----------------------- build/bench-comment.sh | 56 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 42 deletions(-) create mode 100755 build/bench-comment.sh diff --git a/.github/workflows/benchmarks.yaml b/.github/workflows/benchmarks.yaml index 62074e2ff7..4fe2f7c2cc 100644 --- a/.github/workflows/benchmarks.yaml +++ b/.github/workflows/benchmarks.yaml @@ -101,53 +101,14 @@ jobs: go-version-file: .go-version - name: Install tools run: cd build/tools && go install tool - - name: Run benchmarks - env: - BEFORE_SHA: ${{ github.event.before }} - AFTER_SHA: ${{ github.event.after }} - BENCH_PKGS: ${{ needs.check-changes.outputs.bench }} - run: | - for pkg in $(echo "$BENCH_PKGS" | jq -r '.[]'); do - benchlab \ - -commit "$BEFORE_SHA","$AFTER_SHA" \ - -pkg "$pkg" \ - -host local:tags=opa_wasm \ - -reps 3 \ - -benchtime 300ms \ - -run '^$' - done - - name: Comment on PR with benchmark results + - name: Run benchmarks and comment on PR env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_SHA: ${{ github.sha }} BEFORE_SHA: ${{ github.event.before }} AFTER_SHA: ${{ github.event.after }} - run: | - PR_NUMBER=$(gh pr list --search "${COMMIT_SHA}" --state merged --json number --jq '.[0].number') - - if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then - echo "Could not find originating PR for commit ${COMMIT_SHA}" - exit 0 - fi - - { - echo "
Benchmark Comparison (\`${BEFORE_SHA}\` vs \`${AFTER_SHA}\`)" - echo "" - echo '```' - # Each benchstat file ends with a compact benchmark × host delta table; - # extract from that header line onward so the comment stays readable. - for f in .benchlab/benchstat.*.txt; do - awk '/^benchmark \\ host/{found=1} found{print}' "$f" - echo "" - done - echo '```' - echo "" - echo "
" - echo "" - echo "_This comment was automatically generated by the benchmarks workflow._" - } > body.md - - gh pr comment "${PR_NUMBER}" --body-file body.md + BENCH_PKGS: ${{ needs.check-changes.outputs.bench }} + run: bash build/bench-comment.sh notebook: permissions: diff --git a/build/bench-comment.sh b/build/bench-comment.sh new file mode 100755 index 0000000000..c66be4a9f1 --- /dev/null +++ b/build/bench-comment.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Run benchlab comparisons and post (or print) a markdown PR comment. +# +# Required env vars: +# BEFORE_SHA base commit +# AFTER_SHA head commit +# BENCH_PKGS JSON array of Go package paths, e.g. '["./topdown","./ast"]' +# +# Optional (needed to post a PR comment): +# GH_TOKEN GitHub token with pull-requests:write +# COMMIT_SHA the merge commit SHA used to look up the PR +set -euo pipefail + +: "${BEFORE_SHA:?}" +: "${AFTER_SHA:?}" +: "${BENCH_PKGS:?}" + +for pkg in $(echo "$BENCH_PKGS" | jq -r '.[]'); do + benchlab \ + -commit "$BEFORE_SHA","$AFTER_SHA" \ + -pkg "$pkg" \ + -host local:tags=opa_wasm \ + -reps 3 \ + -benchtime 300ms \ + -run '^$' +done + +body() { + echo "
Benchmark Comparison (\`${BEFORE_SHA}\` vs \`${AFTER_SHA}\`)" + echo "" + for f in .benchlab/benchstat.*.txt; do + awk ' + /^benchmark \\ host/ { found=1; print "| benchmark | delta |"; print "| --- | --- |"; next } + found && $2 ~ /^[+-]/ { + val = $2; for (i = 3; i <= NF; i++) val = val " " $i + print "| " $1 " | " val " |" + } + ' "$f" + echo "" + done + echo "
" + echo "" + echo "_This comment was automatically generated by the benchmarks workflow._" +} + +if [ -n "${GH_TOKEN:-}" ] && [ -n "${COMMIT_SHA:-}" ]; then + PR_NUMBER=$(gh pr list --search "${COMMIT_SHA}" --state merged --json number --jq '.[0].number') + if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then + echo "Could not find originating PR for commit ${COMMIT_SHA}" + exit 0 + fi + body > body.md + gh pr comment "${PR_NUMBER}" --body-file body.md +else + body +fi