-
Notifications
You must be signed in to change notification settings - Fork 263
Add benchmark workflow and fix nats-core protocol benches #974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
caspervonb
wants to merge
4
commits into
main
Choose a base branch
from
bench-workflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1ad14e
Pass bytes to encode_pub/encode_hpub in benches
caspervonb ee52b03
Add benchmark workflow comparing a PR against its base
caspervonb 2ee3e67
Add request/reply benchmarks for nats-core
caspervonb f35afd4
Pass checkout SHAs through env vars
caspervonb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,122 @@ | ||||||||||||||
| name: bench | ||||||||||||||
|
|
||||||||||||||
| on: | ||||||||||||||
| pull_request: | ||||||||||||||
| branches: | ||||||||||||||
| - "**" | ||||||||||||||
| workflow_dispatch: | ||||||||||||||
| inputs: | ||||||||||||||
| compare-fail: | ||||||||||||||
| description: "Regression threshold (pytest-benchmark --benchmark-compare-fail), e.g. median:25%" | ||||||||||||||
| default: "median:25%" | ||||||||||||||
|
|
||||||||||||||
| permissions: | ||||||||||||||
| contents: read | ||||||||||||||
|
|
||||||||||||||
| # Only the most recent run per PR/branch matters; cancel superseded ones. | ||||||||||||||
| concurrency: | ||||||||||||||
| group: bench-${{ github.workflow }}-${{ github.ref }} | ||||||||||||||
| cancel-in-progress: true | ||||||||||||||
|
|
||||||||||||||
| jobs: | ||||||||||||||
| bench: | ||||||||||||||
| name: bench (${{ matrix.os }}) | ||||||||||||||
| runs-on: ${{ matrix.os }} | ||||||||||||||
| timeout-minutes: 30 | ||||||||||||||
| strategy: | ||||||||||||||
| # Each runner compares base vs head against itself, so one runner | ||||||||||||||
| # failing its threshold should not stop the others from reporting. | ||||||||||||||
| fail-fast: false | ||||||||||||||
| matrix: | ||||||||||||||
| os: ["ubuntu-latest", "macos-latest"] | ||||||||||||||
| env: | ||||||||||||||
| # Compare base vs head on the SAME machine; storage lives outside the | ||||||||||||||
| # repo so switching git refs never touches it. | ||||||||||||||
| BENCH_STORAGE: ${{ runner.temp }}/benchmarks | ||||||||||||||
| BENCH_PATHS: benches/bench_protocol.py benches/bench_client.py | ||||||||||||||
| COMPARE_FAIL: ${{ inputs.compare-fail || 'median:25%' }} | ||||||||||||||
| defaults: | ||||||||||||||
| run: | ||||||||||||||
| working-directory: nats-core | ||||||||||||||
| steps: | ||||||||||||||
| - name: Check out repository | ||||||||||||||
| uses: actions/checkout@v5 | ||||||||||||||
| with: | ||||||||||||||
| # Full history so we can check out the PR base commit. | ||||||||||||||
| fetch-depth: 0 | ||||||||||||||
|
|
||||||||||||||
| - name: Set up Go | ||||||||||||||
| uses: actions/setup-go@v6 | ||||||||||||||
| with: | ||||||||||||||
| go-version: "stable" | ||||||||||||||
|
|
||||||||||||||
| - name: Install NATS Server | ||||||||||||||
| run: go install github.com/nats-io/nats-server/v2@latest | ||||||||||||||
| shell: bash | ||||||||||||||
| working-directory: . | ||||||||||||||
|
|
||||||||||||||
| - name: Set up Python | ||||||||||||||
| uses: actions/setup-python@v6 | ||||||||||||||
| with: | ||||||||||||||
| python-version: "3.13" | ||||||||||||||
|
|
||||||||||||||
| - name: Install uv | ||||||||||||||
| uses: astral-sh/setup-uv@v6 | ||||||||||||||
|
|
||||||||||||||
| - name: Resolve refs | ||||||||||||||
| id: refs | ||||||||||||||
| shell: bash | ||||||||||||||
| run: | | ||||||||||||||
| base="${{ github.event.pull_request.base.sha }}" | ||||||||||||||
| head="${{ github.event.pull_request.head.sha || github.sha }}" | ||||||||||||||
| if [ -z "$base" ]; then | ||||||||||||||
| # Not a PR (e.g. workflow_dispatch): compare against parent commit. | ||||||||||||||
| base="$(git rev-parse "${head}^")" | ||||||||||||||
| fi | ||||||||||||||
| echo "base=$base" >>"$GITHUB_OUTPUT" | ||||||||||||||
| echo "head=$head" >>"$GITHUB_OUTPUT" | ||||||||||||||
| echo "Base: $base" | ||||||||||||||
| echo "Head: $head" | ||||||||||||||
|
|
||||||||||||||
| # --- Baseline: the PR's merge base / target --------------------------- | ||||||||||||||
| - name: Check out base | ||||||||||||||
| run: git checkout --force --detach ${{ steps.refs.outputs.base }} | ||||||||||||||
| working-directory: . | ||||||||||||||
|
|
||||||||||||||
| - name: Install dependencies (base) | ||||||||||||||
| run: uv sync --dev | ||||||||||||||
|
|
||||||||||||||
| - name: Run benchmarks (base) | ||||||||||||||
| run: | | ||||||||||||||
| uv run pytest $BENCH_PATHS \ | ||||||||||||||
| --benchmark-only \ | ||||||||||||||
| --benchmark-save=base \ | ||||||||||||||
| --benchmark-storage="file://$BENCH_STORAGE" \ | ||||||||||||||
| --benchmark-json="$RUNNER_TEMP/base.json" | ||||||||||||||
|
|
||||||||||||||
| # --- Candidate: the PR head, compared against the baseline ------------ | ||||||||||||||
| - name: Check out head | ||||||||||||||
| run: git checkout --force --detach ${{ steps.refs.outputs.head }} | ||||||||||||||
| working-directory: . | ||||||||||||||
|
|
||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same expression-injection pattern as the
Suggested change
|
||||||||||||||
| - name: Install dependencies (head) | ||||||||||||||
| run: uv sync --dev | ||||||||||||||
|
|
||||||||||||||
| - name: Run benchmarks (head) and compare | ||||||||||||||
| run: | | ||||||||||||||
| uv run pytest $BENCH_PATHS \ | ||||||||||||||
| --benchmark-only \ | ||||||||||||||
| --benchmark-storage="file://$BENCH_STORAGE" \ | ||||||||||||||
| --benchmark-compare=0001 \ | ||||||||||||||
| --benchmark-compare-fail="$COMPARE_FAIL" \ | ||||||||||||||
| --benchmark-json="$RUNNER_TEMP/head.json" | ||||||||||||||
|
|
||||||||||||||
| - name: Upload benchmark results | ||||||||||||||
| if: always() | ||||||||||||||
| uses: actions/upload-artifact@v4 | ||||||||||||||
| with: | ||||||||||||||
| name: benchmarks-${{ matrix.os }} | ||||||||||||||
| path: | | ||||||||||||||
| ${{ runner.temp }}/base.json | ||||||||||||||
| ${{ runner.temp }}/head.json | ||||||||||||||
| if-no-files-found: warn | ||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
${{ steps.refs.outputs.base }}directly in arun:script is an expression-injection pattern that GitHub's security hardening guide warns against. The value here is always a 40-hex-char SHA so there's no real risk, but the safer idiom passes it through an env var: