> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Detect Go Benchmark Regressions in CI

> Run go test benchmarks in GitHub Actions on a bare-metal runner, compare every pull request against main and fail the check when a benchmark regresses.

Your `go test` benchmarks run under CodSpeed as they are, with no code change
and no extra package. In GitHub Actions, they run with the walltime instrument
on a Macro Runner, a bare-metal machine with isolated CPU cores and a fixed
clock. On the Cortex-A72 runner, noise stays under 1%, so a 3.3% change stands
out with 99% confidence.

## Write the benchmark with testing.B

Benchmark the code your service runs on every request. Here's a benchmark for
decoding a request body with `encoding/json`:

```go theme={null}
package api

import (
	"encoding/json"
	"testing"
)

type ChatRequest struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
	Stream   bool      `json:"stream"`
}

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

var payload = []byte(`{"model":"default","stream":true,"messages":[{"role":"system","content":"You are a support agent."},{"role":"user","content":"Where is my order?"}]}`)

func BenchmarkDecodeChatRequest(b *testing.B) {
	for b.Loop() {
		var req ChatRequest
		if err := json.Unmarshal(payload, &req); err != nil {
			b.Fatal(err)
		}
	}
}
```

Use `b.Loop()` on Go 1.24 and later. The older `for i := 0; i < b.N; i++` loop
works too.

## Run it locally under CodSpeed

Install the CodSpeed CLI and run the same command you use today:

```bash theme={null}
curl -fsSL https://codspeed.io/install.sh | sh
codspeed run --mode walltime --skip-upload -- go test -bench=.
```

The CLI lists every benchmark it found and warns about any it can't run.

## Add the workflow

Create `.github/workflows/codspeed.yml`:

```yaml .github/workflows/codspeed.yml icon="github" theme={null}
name: CodSpeed Benchmarks

on:
  push:
    branches:
      - "main" # or "master"
  pull_request:
  # `workflow_dispatch` allows CodSpeed to trigger backtest
  # performance analysis in order to generate initial data.
  workflow_dispatch:

jobs:
  benchmarks:
    name: Run benchmarks
    runs-on: codspeed-macro-arm64-graviton-ubuntu-22-04
    permissions: # optional for public repositories
      contents: read # required for actions/checkout
      id-token: write # required for OIDC authentication with CodSpeed
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
      - name: Run the benchmarks
        uses: CodSpeedHQ/action@v5
        with:
          mode: walltime
          run: go test -bench=.
```

The `push` trigger on main gives every pull request a baseline.
`workflow_dispatch` lets CodSpeed backtest older commits for the first data
points. Keep the job on one runner label, since a different machine shifts the
baseline.

For lower noise, switch `runs-on` to
`codspeed-macro-x64-ryzen-9950x-ubuntu-24-04`. That runner keeps noise under
0.5%, so a 1.7% change stands out with 99% confidence.

## Catch regressions on the pull request

Each pull request gets a performance report comment and a status check named
"CodSpeed Performance Analysis". Walltime runs record a flamegraph, so a slower
benchmark comes with the functions that got slower. If flamegraphs look cut
short on the ARM64 runner, set `CODSPEED_PERF_UNWINDING_MODE: fp` in the step's
`env`.

## What Bifrost runs

Bifrost, an open-source AI gateway written in Go, runs its request hot path
benchmarks this way: payload parsing, provider request and response conversion,
SSE framing and key selection. Its workflow runs `go test -bench` under the
walltime instrument on a Macro Runner, on every pull request and on pushes to
its main branches. A matrix splits the packages into four jobs, so one module's
failure doesn't hide the others' results.

## Flags to know

The Go integration reads `-bench` to pick benchmarks and `-benchtime` to set how
long each one runs. It ignores other `go test` flags, such as `-count`,
`-benchmem` or `-cpu`, with a warning. To split a long suite across jobs, give
each job its own `-bench` pattern.

Next, see how
[Macro Runners keep walltime results stable](https://codspeed.io/docs/features/macro-runners.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.