name: Benchmarks on: pull_request: types: [opened, synchronize, reopened] repository_dispatch: types: [release-published] workflow_dispatch: permissions: contents: write deployments: write pull-requests: write concurrency: group: benchmark-${{ github.event.pull_request.number || github.run_id }} cancel-in-progress: true env: CI: true jobs: benchmark: runs-on: ubuntu-latest timeout-minutes: 10 steps: - name: Checkout code uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 with: fetch-depth: 0 # --- Change detection --- - name: Check if benchmarks should run (PR) id: pr-check if: github.event_name == 'pull_request' run: | CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD) if echo "$CHANGED" | grep -qE "^(packages/next/|tests/apps/next/middleware/)"; then echo "run=true" >> $GITHUB_OUTPUT else echo "run=false" >> $GITHUB_OUTPUT fi - name: Check if gt-next was published (dispatch) id: dispatch-check if: github.event_name == 'repository_dispatch' run: | PACKAGES='${{ toJSON(github.event.client_payload.publishedPackages) }}' if echo "$PACKAGES" | jq -e '.[] | select(.name == "gt-next")' > /dev/null 2>&1; then echo "run=true" >> $GITHUB_OUTPUT VERSION=$(echo "$PACKAGES" | jq -r '.[] | select(.name == "gt-next") | .version') echo "version=$VERSION" >> $GITHUB_OUTPUT else echo "run=false" >> $GITHUB_OUTPUT fi - name: Determine if benchmarks should run id: should-run run: | if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then echo "run=true" >> $GITHUB_OUTPUT elif [ "${{ github.event_name }}" = "pull_request" ]; then echo "run=${{ steps.pr-check.outputs.run }}" >> $GITHUB_OUTPUT elif [ "${{ github.event_name }}" = "repository_dispatch" ]; then echo "run=${{ steps.dispatch-check.outputs.run }}" >> $GITHUB_OUTPUT fi # --- Setup --- - name: Setup Rust if: steps.should-run.outputs.run == 'true' uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable - name: Setup development tools if: steps.should-run.outputs.run == 'true' uses: jdx/mise-action@5228313ee0372e111a38da051671ca30fc5a96db # v3 with: cache: true - name: Get pnpm store directory if: steps.should-run.outputs.run == 'true' id: pnpm-store run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT" - name: Cache pnpm store if: steps.should-run.outputs.run == 'true' uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: path: ${{ steps.pnpm-store.outputs.path }} key: ${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }} restore-keys: ${{ runner.os }}-pnpm- - name: Install dependencies if: steps.should-run.outputs.run == 'true' run: pnpm install # --- Resolve version --- - name: Read gt-next version id: read-version if: steps.should-run.outputs.run == 'true' && github.event_name != 'repository_dispatch' run: | VERSION=$(node -p "require('./packages/next/package.json').version") echo "version=$VERSION" >> $GITHUB_OUTPUT - name: Set version output id: version if: steps.should-run.outputs.run == 'true' run: | if [ "${{ github.event_name }}" = "repository_dispatch" ]; then echo "value=${{ steps.dispatch-check.outputs.version }}" >> $GITHUB_OUTPUT else echo "value=${{ steps.read-version.outputs.version }}" >> $GITHUB_OUTPUT fi # --- Build packages (required for unit benchmarks to resolve gt-next/middleware) --- - name: Build packages if: steps.should-run.outputs.run == 'true' run: pnpm build # --- Run benchmarks --- # Out of scope of current PR # TODO: this should be more generalizable done with a single pnpm call triggering everything # you pass in a list/object of changed packages and their versions, then it executes only the associated benchmarks # explore having a "gt:package" field in the package.json for the test apps so we know which package each app is associated with - name: Run unit benchmarks if: steps.should-run.outputs.run == 'true' working-directory: packages/next run: pnpm bench:unit:json - name: Run E2E benchmarks if: steps.should-run.outputs.run == 'true' working-directory: tests/apps/next/middleware env: BENCH_OUTPUT_NAME: e2e-latest run: node runBenchE2E.mjs # --- Transform results --- # Out of scope of current PR # TODO: standardize this so it is one call and all of the package/version/etc mapping searching for output files etc is automatic - name: Transform benchmark results if: steps.should-run.outputs.run == 'true' run: | node scripts/transform-bench-results.mjs \ --unit packages/next/benchmarks/results/unit-latest.json \ --e2e tests/apps/next/middleware/benchmarks/results/e2e-latest.json \ --output benchmark-results.json \ --package gt-next \ --version ${{ steps.version.outputs.value }} # --- Store & compare --- # Clean working tree, but leave benchmarks results in place - name: Clean working tree if: steps.should-run.outputs.run == 'true' run: git checkout -- . - name: Store benchmark results if: steps.should-run.outputs.run == 'true' uses: benchmark-action/github-action-benchmark@52576c92bccf6ac60c8223ec7eb2565637cae9ba # v1 with: name: 'Middleware Benchmarks' tool: 'customSmallerIsBetter' output-file-path: benchmark-results.json gh-pages-branch: gh-pages benchmark-data-dir-path: dev/bench github-token: ${{ secrets.GITHUB_TOKEN }} # Comparison & alerting alert-threshold: '150%' comment-on-alert: true fail-on-alert: false alert-comment-cc-users: '@generaltranslation/core' summary-always: true # Data persistence — only save/push on release dispatches save-data-file: ${{ github.event_name == 'repository_dispatch' }} auto-push: ${{ github.event_name == 'repository_dispatch' }}