From 1424402d061c2d345dd55e983179def4b1bdfd60 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 29 Apr 2026 10:33:50 +0800 Subject: [PATCH] =?UTF-8?q?ci(bench):=20=E6=B7=BB=E5=8A=A0=E5=9F=BA?= =?UTF-8?q?=E5=87=86=E6=B5=8B=E8=AF=95=20GitHub=20Actions=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 触发于 master 分支 push 和 PR - 运行 go test -bench -count=10 获取统计数据 - 使用 benchstat 对比基准线 - 调用 check_regression.py 检测回归 - 自动更新基准线并保存 artifacts Co-Authored-By: Claude Opus 4.7 --- .github/workflows/benchmark.yml | 104 ++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 .github/workflows/benchmark.yml diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 0000000..b465d51 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,104 @@ +name: Benchmark + +on: + push: + branches: + - master + paths: + - 'internal/**/*.go' + - 'go.mod' + - 'go.sum' + pull_request: + branches: + - master + paths: + - 'internal/**/*.go' + - 'go.mod' + - 'go.sum' + workflow_dispatch: + +permissions: + contents: write + +jobs: + benchmark: + name: Run Benchmarks + runs-on: ubuntu-latest + timeout-minutes: 30 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' + cache: true + + - name: Install benchstat + run: go install golang.org/x/perf/cmd/benchstat@latest + + - name: Download dependencies + run: go mod download + + - name: Fetch baseline benchmark + run: | + if git show origin/master:benchmark-baseline.txt >/dev/null 2>&1; then + git show origin/master:benchmark-baseline.txt > benchmark-baseline.txt + echo "baseline_found=true" >> $GITHUB_ENV + else + echo "baseline_found=false" >> $GITHUB_ENV + fi + + - name: Run benchmarks + run: | + go test -bench=. -benchmem -count=10 ./... 2>&1 | tee benchmark-current.txt + + - name: Compare benchmarks + if: env.baseline_found == 'true' + run: | + benchstat benchmark-baseline.txt benchmark-current.txt > benchmark-comparison.txt + cat benchmark-comparison.txt + + - name: Check for regressions + if: env.baseline_found == 'true' + run: | + if command -v python3 >/dev/null 2>&1; then + benchstat benchmark-baseline.txt benchmark-current.txt | \ + python3 scripts/check_regression.py - \ + --config .benchmark-thresholds.yaml \ + --environment ci \ + || echo "regression_detected=$?" >> $GITHUB_OUTPUT + else + echo "Python3 not available, skipping regression check" + fi + id: regression_check + + - name: Save current benchmark as baseline + if: github.ref == 'refs/heads/master' + run: | + cp benchmark-current.txt benchmark-baseline.txt + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add benchmark-baseline.txt || true + git commit -m "chore(bench): update benchmark baseline" || true + git push || true + + - name: Upload benchmark results + uses: actions/upload-artifact@v4 + with: + name: benchmark-results + path: | + benchmark-current.txt + benchmark-baseline.txt + benchmark-comparison.txt + retention-days: 30 + + - name: Fail on regression + if: steps.regression_check.outputs.regression_detected == '2' + run: | + echo "::error::BLOCK level performance regression detected" + exit 2 \ No newline at end of file