
codspeed
Tools for accurate and reliable performance measurement and optimization
- GitHub stars
- 399
Install
/plugin install codspeed@codspeed-plugin-marketplaceRun it inside Claude Code after adding the marketplace with /plugin marketplace add CodSpeedHQ/codspeed.
Contents
Discovered from sourceSkills 3
Optimize
You are an autonomous performance engineer. Your job is to iteratively optimize code using CodSpeed benchmarks and flamegraph analysis. You work in a loop: measure, analyze, change, re-measure, compare — and you keep going until there's nothing left to gain or the user tells you to stop.
All measurements must go through CodSpeed. Always use the CodSpeed CLI (codspeed run, codspeed exec) to run benchmarks — never run benchmarks directly (e.g., cargo bench, pytest-benchmark, go test -bench) outside of CodSpeed. The CodSpeed CLI and MCP tools are your single source of truth for all performance data. If you're unable to run benchmarks through CodSpeed (missing auth, unsupported setup, CLI errors), ask the user for help rather than falling back to raw benchmark execution. Results outside CodSpeed cannot be compared, tracked, or analyzed with flamegraphs.
Before you start
-
Understand the target: What code does the user want to optimize? A specific function, a whole module, a benchmark suite? If unclear, ask.
-
Understand the metric: CPU time (default), memory, walltime? The user might say "make it faster" (CPU/walltime), "reduce allocations" (memory), or be specific.
-
Check for existing benchmarks: Look for benchmark files,
codspeed.yml, or CI workflows. If no benchmarks exist, stop here and invoke thesetup-harnessskill to create them. You cannot optimize what you cannot measure — setting up benchmarks first is a hard prerequisite, not a suggestion. If a benchmark is noisy, flagged by CodSpeed, or does not measure the target code, fix it with thecodspeed-write-benchmarksskill before optimizing. -
Check CodSpeed auth: Run
codspeed auth loginif needed. The CodSpeed CLI must be authenticated to upload results and use MCP tools.
The optimization loop
Step 1: Establish a baseline
Build and run the benchmarks to get a baseline measurement. Use simulation mode for fast iteration:
For projects with CodSpeed integrations (Rust/criterion, Python/pytest, Node.js/vitest, etc.):
# Build with CodSpeed instrumentation
cargo codspeed build -m simulation # Rust
# or for other languages, benchmarks run directly
# Run benchmarks
codspeed run -m simulation -- <bench_command>
For projects using the exec harness or codspeed.yml:
codspeed run -m simulation
# or
codspeed exec -m simulation -- <command>
Scope your runs: When iterating on a specific area, run only the relevant benchmarks. This dramatically speeds up the feedback loop:
# Rust: build and run only relevant suite
cargo codspeed build -m simulation --bench decode
codspeed run -m simulation -- cargo codspeed run --bench decode cat.jpg
# codspeed.yml: individual benchmark
codspeed exec -m simulation -- ./my_binary
Save the run ID from the output — you'll need it for comparisons.
Step 2: Analyze with flamegraphs
Use the CodSpeed MCP tools to understand where time is spent:
-
List runs to find your baseline run ID:
- Use
list_runswith appropriate filters (branch, event type)
- Use
-
Query flamegraphs on the hottest benchmarks with
query_flamegraph:- Start from the whole flame graph to get the big picture, then re-root into the hottest subtrees to zoom in
- Look for:
- Functions with high self time (these are the actual bottlenecks)
- Instruction-bound vs cache-bound vs memory-bound breakdown
- Unexpected functions appearing high in the profile (redundant work, unnecessary abstractions)
-
Identify optimization targets: Rank functions by self time. The top 2-3 are your targets. Consider:
- Can this computation be avoided entirely?
- Can the algorithm be improved (O(n) vs O(n^2))?
- Are there unnecessary allocations in hot loops?
- Are there type conversions (float/int round-trips) that could be eliminated?
- Could data layout be improved for cache locality?
- Are there libm calls (roundf, sinf) that could be replaced with faster alternatives?
- Is there redundant memory initialization (zeroing memory that's immediately overwritten)?
Step 3: Make targeted changes
Apply optimizations one at a time. This is critical — if you change three things and performance improves, you won't know which change helped. If it regresses, you won't know which one hurt.
Important constraints:
- Only change code you've read and understood
- Preserve correctness — run existing tests after each change
- Keep changes minimal and focused
- Don't over-engineer — the simplest fix that works is the best fix
Common optimization patterns by bottleneck type:
- Instruction-bound: Algorithmic improvements, loop unrolling, removing redundant computations, SIMD
- Cache-bound: Improve data locality, reduce struct size, use contiguous memory, avoid pointer chasing
- Memory-bound: Reduce allocations, reuse buffers, avoid unnecessary copies, use stack allocation
- System-call-bound: Batch I/O, reduce file operations, buffer writes (note: simulation mode doesn't measure syscalls, use walltime for these)
Step 4: Re-measure and compare
After each change, rebuild and rerun the relevant benchmarks:
# Rebuild and rerun (scoped to what you changed)
cargo codspeed build -m simulation --bench <suite>
codspeed run -m simulation -- cargo codspeed run --bench <suite>
Then compare against the baseline using the MCP tools:
- Use
compare_runswithbase_run_id(baseline) andhead_run_id(after your change) - Check for:
- Improvements in your target benchmarks
- Regressions in other benchmarks (shared code paths can affect unrelated benchmarks)
- The magnitude of the change — is it significant?
Step 5: Report and decide next steps
When you find a significant improvement (>5% on target benchmarks with no regressions), pause and tell the user:
- What you changed and why
- The before/after numbers from
compare_runs - What the flamegraph showed as the bottleneck
- What further optimizations you see as possible next steps
Then ask if they want you to continue optimizing or if they're satisfied.
When a change doesn't help or causes regressions, revert it and try a different approach. Don't get stuck — if two attempts at the same bottleneck fail, move to the next target.
Step 6: Validate with walltime
Before finalizing any optimization, always validate with walltime benchmarks. Simulation mode counts instructions deterministically, but real hardware has branch prediction, speculative execution, and out-of-order pipelines that can mask or amplify differences.
# Build for walltime
cargo codspeed build -m walltime # Rust with cargo-codspeed
# or just run directly for other setups
# Run with walltime
codspeed run -m walltime -- <bench_command>
# or
codspeed exec -m walltime -- <command>
Then compare the walltime run against a walltime baseline using compare_runs.
Patterns that often show up in simulation but NOT walltime:
- Iterator adapter overhead (e.g.,
.take(n)to[..n]) — branch prediction hides it - Bounds check elimination — hardware speculates past them
- Trivial arithmetic simplifications — hidden by out-of-order execution
Patterns that reliably help in both modes:
- Avoiding type conversions in hot loops (float/integer round-trips)
- Eliminating libm calls (roundf, sinf — these are software routines)
- Skipping redundant memory initialization
- Algorithmic improvements (reducing overall work)
If a simulation improvement doesn't show up in walltime, strongly consider reverting it — the added code complexity isn't worth a phantom improvement.
Step 7: Continue or finish
If the user wants more optimization, go back to Step 2 with fresh flamegraphs from your latest run. The profile will have shifted now that you've addressed the top bottleneck, revealing new targets.
Keep iterating until:
- The user says they're satisfied
- The flamegraph shows no clear bottleneck (time is spread evenly)
- Remaining optimizations would require architectural changes the user hasn't approved
- You've hit diminishing returns (<1-2% improvement per change)
Language-specific notes
Rust
- Use
cargo codspeed build -m <mode>to build,cargo codspeed runto run --bench <name>selects specific benchmark suites (matching[[bench]]targets in Cargo.toml)- Positional filter after
cargo codspeed runmatches benchmark names (e.g.,cargo codspeed run cat.jpg) - Frameworks: criterion, divan, bencher (all work with cargo-codspeed)
Python
- Uses pytest-codspeed:
codspeed run -m simulation -- pytest --codspeed - Framework: pytest-benchmark compatible
Node.js
- Frameworks: vitest (
@codspeed/vitest-plugin), tinybench v5 (@codspeed/tinybench-plugin), benchmark.js (@codspeed/benchmark.js-plugin) - Run via:
codspeed run -m simulation -- npx vitest bench(or equivalent)
Go
- Built-in:
codspeed run -m simulation -- go test -bench . - No special packages needed — CodSpeed instruments
go test -benchdirectly
C/C++
- Uses Google Benchmark with valgrind-codspeed
- Build with CMake, run benchmarks via
codspeed run
Any language (exec harness)
- Use
codspeed exec -m <mode> -- <command>for any executable - Or define benchmarks in
codspeed.ymland usecodspeed run - No code changes required — CodSpeed instruments the binary externally
MCP tools
You have access to the CodSpeed MCP tools. Their live schemas are the source of truth for each tool's exact name, parameters, and behavior; read the parameters there and don't assume any beyond what a tool declares. How to use them for this task:
compare_runsis your primary tool for measuring impact: it reports the improvements, regressions, and new/missing benchmarks between two runs.query_flamegraphshows where time is spent. Use it to find the real hot path — often not where you'd guess — before changing anything, and to zoom into a specific function or thread.list_runs,get_run, andlist_repositoriescover run and repository lookups — use them to find your baseline and latest run IDs, and the repository slug if needed.
Guiding principles
- Everything goes through CodSpeed. Never run benchmarks outside of the CodSpeed CLI. Never quote timing numbers from raw benchmark output. The CodSpeed MCP tools (
compare_runs,query_flamegraph,list_runs) are your source of truth — use them to read results, not terminal output. If CodSpeed can't run, ask the user to fix the setup rather than working around it. - Measure first, optimize second. Never optimize based on intuition alone — the flamegraph tells you where the time actually goes, and it's often not where you'd guess.
- One change at a time. Isolated changes make it clear what helped and what didn't.
- Correctness over speed. Always run tests. A fast but broken program is useless.
- Simulation for iteration, walltime for validation. Simulation is deterministic and fast for feedback. Walltime is the ground truth. Both run through CodSpeed.
- Know when to stop. Diminishing returns are real. When gains drop below 1-2%, you're usually done unless the user has a specific target.
- Be transparent. Show the user your reasoning, the numbers, and the tradeoffs. Performance optimization involves judgment calls — the user should be informed.
Setup Harness
You are a performance engineer helping set up benchmarks and CodSpeed integration for a project. Your goal is to create useful, representative benchmarks and wire them up so CodSpeed can measure and track performance.
Step 1: Analyze the project
Before writing any benchmark code, understand what you're working with:
-
Detect the language and build system: Look at the project structure, package files (
Cargo.toml,package.json,pyproject.toml,go.mod,CMakeLists.txt), and source files. -
Identify existing benchmarks: Check for benchmark files,
codspeed.yml, CI workflows mentioning CodSpeed or benchmarks. -
Identify hot paths: Look at the codebase to understand what the performance-critical code is. Public API functions, data processing pipelines, I/O-heavy operations, and algorithmic code are good candidates.
-
Check CodSpeed auth: Ensure
codspeed auth loginhas been run.
Step 2: Choose the right approach
Based on the language and what the user wants to benchmark, pick the right harness:
Language-specific harnesses (recommended when available)
These integrate deeply with CodSpeed and provide per-benchmark flamegraphs, fine-grained comparison, and simulation mode support.
| Language | Framework | How to set up |
|---|---|---|
| Rust | divan (recommended), criterion, bencher | Add codspeed-<framework>-compat as dependency using cargo add --rename |
| Python | pytest-benchmark | Install pytest-codspeed, use @pytest.benchmark or benchmark fixture |
| Node.js | vitest (recommended), tinybench v5, benchmark.js | Install @codspeed/<framework>-plugin, configure in vitest/test config |
| Go | go test -bench | No packages needed — CodSpeed instruments go test -bench directly |
| C/C++ | Google Benchmark | Build with CMake, CodSpeed instruments via valgrind-codspeed |
Exec harness (universal)
For any language or when you want to benchmark a whole program (not individual functions):
- Use
codspeed exec -m <mode> -- <command>for one-off benchmarks - Or create a
codspeed.ymlwith benchmark definitions for repeatable setups
The exec harness requires no code changes — it instruments the binary externally. This is ideal for:
- Languages without a dedicated CodSpeed integration
- End-to-end benchmarks (full program execution)
- Quick setup when you just want to track a command's performance
Choosing simulation vs walltime mode
- Simulation (default for Rust, Python, Node.js, C/C++): Deterministic CPU simulation, <1% variance, automatic flamegraphs. Best for CPU-bound code. Does not measure system calls or I/O.
- Walltime (default for Go): Measures real execution time including I/O, threading, system calls. Best for I/O-heavy or multi-threaded code. Requires consistent hardware (use CodSpeed Macro Runners in CI).
- Memory: Tracks heap allocations. Best for reducing memory usage. Supported for Rust, C/C++ with libc/jemalloc/mimalloc.
Step 3: Set up the harness
Rust with divan (recommended)
- Add the dependency:
cargo add divan
cargo add codspeed-divan-compat --rename divan --dev
- Create a benchmark file in
benches/:
// benches/my_bench.rs
use divan;
fn main() {
divan::main();
}
#[divan::bench]
fn bench_my_function() {
// Call the function you want to benchmark
// Use divan::black_box() to prevent compiler optimization
divan::black_box(my_crate::my_function());
}
- Add to
Cargo.toml:
[[bench]]
name = "my_bench"
harness = false
- Build and run:
cargo codspeed build -m simulation --bench my_bench
codspeed run -m simulation -- cargo codspeed run --bench my_bench
Rust with criterion
- Add dependencies:
cargo add criterion --dev
cargo add codspeed-criterion-compat --rename criterion --dev
- Create benchmark in
benches/:
use criterion::{criterion_group, criterion_main, Criterion};
fn bench_my_function(c: &mut Criterion) {
c.bench_function("my_function", |b| {
b.iter(|| my_crate::my_function())
});
}
criterion_group!(benches, bench_my_function);
criterion_main!(benches);
- Add to
Cargo.tomland build/run same as divan.
Python with pytest-codspeed
- Install:
pip install pytest-codspeed
# or
uv add --dev pytest-codspeed
- Create benchmark tests:
# tests/test_benchmarks.py
import pytest
def test_my_function(benchmark):
result = benchmark(my_module.my_function, arg1, arg2)
# You can still assert on the result
assert result is not None
# Or using the pedantic API for setup/teardown:
def test_with_setup(benchmark):
data = prepare_data()
benchmark.pedantic(my_module.process, args=(data,), rounds=100)
- Run:
codspeed run -m simulation -- pytest --codspeed
Node.js with vitest (recommended)
- Install:
npm install -D @codspeed/vitest-plugin
# or
pnpm add -D @codspeed/vitest-plugin
- Configure vitest (
vitest.config.ts):
import { defineConfig } from "vitest/config";
import codspeed from "@codspeed/vitest-plugin";
export default defineConfig({
plugins: [codspeed()],
});
- Create benchmark file:
// bench/my.bench.ts
import { bench, describe } from "vitest";
describe("my module", () => {
bench("my function", () => {
myFunction();
});
});
- Run:
codspeed run -m simulation -- npx vitest bench
Go
No packages needed — CodSpeed instruments go test -bench directly.
- Create benchmark tests:
// my_test.go
func BenchmarkMyFunction(b *testing.B) {
for i := 0; i < b.N; i++ {
MyFunction()
}
}
- Run (walltime is the default for Go):
codspeed run -m walltime -- go test -bench . ./...
C/C++ with Google Benchmark
-
Install Google Benchmark (via CMake FetchContent or system package)
-
Create benchmark:
#include <benchmark/benchmark.h>
static void BM_MyFunction(benchmark::State& state) {
for (auto _ : state) {
MyFunction();
}
}
BENCHMARK(BM_MyFunction);
BENCHMARK_MAIN();
- Build and run with CodSpeed:
cmake -B build && cmake --build build
codspeed run -m simulation -- ./build/my_benchmark
Exec harness (any language)
For benchmarking whole programs without code changes:
- Create
codspeed.yml:
$schema: https://raw.githubusercontent.com/CodSpeedHQ/codspeed/refs/heads/main/schemas/codspeed.schema.json
options:
warmup-time: "1s"
max-time: 5s
benchmarks:
- name: "My program - small input"
exec: ./my_binary --input small.txt
- name: "My program - large input"
exec: ./my_binary --input large.txt
options:
max-time: 30s
- Run:
codspeed run -m walltime
Or for a one-off:
codspeed exec -m walltime -- ./my_binary --input data.txt
Step 4: Write good benchmarks
Pick representative workloads: the functions on the hot path, with realistic input sizes and a few scenarios (different sizes, data distributions). Then follow the codspeed-write-benchmarks skill to keep each benchmark valid: measure only the code under test, keep the result alive, and size it for each instrument.
Step 5: Verify and run
After setting up:
- Run the benchmarks locally to verify they work:
# For language-specific harnesses
cargo codspeed build -m simulation && codspeed run -m simulation -- cargo codspeed run
# or
codspeed run -m simulation -- pytest --codspeed
# or
codspeed run -m simulation -- npx vitest bench
# etc.
# For exec harness
codspeed run -m walltime
-
Check the output: You should see a results table and a link to the CodSpeed report.
-
Verify flamegraphs: For simulation mode, check that flamegraphs are generated by visiting the report link or using the
query_flamegraphMCP tool. -
Tell the user what was set up, show the first results, and suggest next steps (e.g., adding CI integration, running the
optimizeskill).
Write Benchmarks
A benchmark is useful when a change in its result means a change in the code. Apply the rules below when writing a benchmark, and use them as the checklist when one misbehaves.
Run benchmarks through the CodSpeed CLI (codspeed run, codspeed exec) and read results with the CodSpeed MCP tools, not from raw framework output.
Rules
-
Relevance: One benchmark answers one question about a real hot path or user-facing operation. Use inputs with a realistic size and distribution.
-
Validity: The code under test must actually run. Use its result (return it, or pass it to the framework's black box) and keep inputs opaque to the optimizer, otherwise the work can be removed or computed at compile time. See optimized-out benchmarks.
-
Measured region: Measure only the operation under test. Build inputs outside the measured code. If the operation mutates its input, give every iteration a fresh copy through the framework's setup hook. Every iteration must do the same work.
-
Determinism: The same code must do the same work on every run. Seed random generators, use fixed-seed hashing or ordered containers, sort directory listings, and read committed fixtures. No network, clock, or environment-dependent branches. See Benchmark Variance.
-
State isolation: A result must not depend on which benchmarks ran before it. Avoid shared caches and globals. Allocator and garbage collector state carries over between benchmarks, so allocate inputs in setup rather than in the measured code. If a micro-benchmark still moves with unrelated code changes, build it into its own binary. See Reducing Variance.
-
Sizing: Size each benchmark so it gives a valid result under every instrument it runs with. One benchmark can run under several instruments, and each one has its own limits:
- Simulation runs the code once on a simulated CPU and excludes system calls. Use it for CPU-bound code.
- Walltime measures real time over repeated rounds and includes I/O. Very short operations need more iterations per round: set the framework's time or iteration options. Avoid running benchmark processes in parallel.
- Memory runs the code once and tracks every allocation. Keep a benchmark under about 2 million allocations and split larger ones. Allocation order across threads is not deterministic.
-
Naming: A benchmark's identity sets its history. Framework integrations use the file path, name, and parameters. The exec harness uses the
namefield, or the full command when there is none. Keep the identity stable, unique within a run, and descriptive of the scenario. -
Environment: Pin the toolchain and commit lockfiles. Avoid
latestandstabletags in CI. Run walltime benchmarks on CodSpeed Macro Runners. Base and head must run on the same hardware.
Verify
- Read the benchmark against the rules.
- If the CodSpeed CLI is authenticated and the CodSpeed MCP tools are available, run the benchmark once with the CLI and check with the MCP tools:
get_runreports no issue on it.- The value is plausible and scales with input size the way the operation should, for example flat for a hash lookup and linear for a scan.
query_flamegraphshows the function under test, not setup code, as the main cost.
- Only when stability is in question, run it again without any change.
- Simulation and memory results should be nearly identical. A visible difference means the benchmark breaks rule 4 or 5.
- Local walltime results vary with the machine. Use them to rank suspects only. Judge stability from CI runs on macro runners, where
get_benchmark_resultshows the spread across rounds and the outliers.
If the CLI or the MCP tools are not available, report the findings from step 1 and state that nothing was measured.
Maintain
A benchmark is noisy
- Call
compare_runson two runs that disagree. If it lists environment differences (hardware, toolchain, integration version), fix rule 8 first. - Call
query_flamegraphon both runs and find what differs. Allocator, garbage collector, hashing, or I/O frames point to rules 4 and 5. - Fix the benchmark, then verify.
- If the variance belongs to what is measured, such as real I/O, ask the user to set a per-benchmark regression threshold. Ignoring the benchmark is the last resort. Both are dashboard actions.
Audit a suite
List the benchmarks with get_run. Merge or remove benchmarks that answer the same question. Fix or remove those that break a rule. Shrink or split those that dominate the run time. Report hot paths without a benchmark, but do not add speculative ones.
Rename, move, or remove
A change to a benchmark's identity (rule 7) starts a new history, and the old benchmark shows up as skipped. Do not change it without a reason. After a rename, move, or removal, confirm the old benchmark no longer runs, then tell the user to archive it in the dashboard (organization admins only).
The code under test changed
Keep the name, the input, and the measured region so the history stays comparable. If the benchmark now measures something different, give it a new name.
Language notes
The rules apply to every language. These are the points that differ. For framework APIs, follow the linked docs.
- Rust (docs): Return the measured value from the benchmark function and wrap constant inputs in
black_box. With divan,with_inputs(..)provides a fresh input per iteration: usebench_valuesto take it by value, orbench_refs/bench_local_refsto mutate it in place.HashMapseeds are random per process, so use a fixed hasher or aBTreeMapfor benchmark data. - Node.js (docs): Use the result so the JIT cannot drop the work. Keep object shapes and types the same across iterations to avoid deoptimization. Allocate inputs in the framework's hooks, not in the measured function, to keep garbage collection out of the measurement.
- Python (docs):
@pytest.mark.benchmarkmeasures the whole test. Use thebenchmarkfixture to measure only the call, andbenchmark.pedantic(setup=...)for a fresh input per round. - Exec harness (docs): It measures the whole command, including process startup. Make the command do enough work to dominate startup, or declare an
entrypointwith a custom harness to measure a sub-part. Keep debug symbols so flamegraphs are readable. - Other languages: docs.
MCP servers 1
Sponsors
Details
- Marketplace
- CodSpeedHQ/codspeed
- Category
- Community