> ## 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.

# Process Isolation

> Run each benchmark in a fresh process so that other benchmarks and setup code can't change its measurement

Process isolation runs each benchmark in a fresh copy of the benchmark process.
It is on by default in the integrations that support it.

## Why it makes a difference

When all benchmarks run in the same process, each benchmark inherits the state
that the benchmarks and setup code before it leave behind:

* **Allocator state**: free lists, fragmentation, and the memory the allocator
  already holds change how much work each allocation takes.
* **Runtime state**: in JIT-compiled runtimes like Node.js, the code that ran
  before changes which functions are already optimized, and the size of the heap
  changes when the garbage collector runs.
* **Threads and handles**: thread pools, open files, and timers that earlier
  code started keep running.

A benchmark's result then depends on the code around it, not only on the code it
measures. Adding, removing, or reordering a benchmark can change the result of
another benchmark that didn't change, and CodSpeed reports a regression or
improvement that your change didn't cause. In a fresh process, every benchmark
starts from the same state, whatever else your benchmark suite contains.

## How it works

The benchmark process runs the benchmarks one after the other. For each
benchmark, it starts the same program again, with the same arguments and
environment, and the new process measures only that benchmark. The benchmark
process waits for it to finish before it starts the next one.

```mermaid theme={null}
flowchart LR
  P["Benchmark process"] --> A["Process for benchmark A"]
  P --> B["Process for benchmark B"]
  P --> C["Process for benchmark C"]
```

As a consequence:

* Code outside the benchmark functions, e.g., top-level setup, runs once in the
  benchmark process and again in every benchmark process it starts.
* Every benchmark pays the startup cost of a new process, so a benchmark suite
  with many short benchmarks takes longer to run.
* Benchmarks can't depend on each other. State that one benchmark leaves behind,
  e.g., a filled cache, is not there for the next one.
* A benchmark process that fails, e.g., with a non-zero exit code, fails the
  run.

## Supported integrations

| Integration | [CPU Simulation](https://codspeed.io/docs/instruments/cpu.md) | [Memory](https://codspeed.io/docs/instruments/memory.md) | [Walltime](https://codspeed.io/docs/instruments/walltime.md) |
| - | - | - | - |
| [Rust: divan](https://codspeed.io/docs/benchmarks/rust/divan.md) | `>=6.0.0` | `>=6.0.0` | No<sup>1</sup> |
| [Rust: criterion.rs](https://codspeed.io/docs/benchmarks/rust/criterion.md) | `>=6.0.0` | `>=6.0.0` | No<sup>1</sup> |
| [Rust: bencher](https://codspeed.io/docs/benchmarks/rust/bencher.md) | No | No | No |
| [Node.js: tinybench](https://codspeed.io/docs/benchmarks/nodejs/tinybench.md) | No | No | `>=7.0.0-beta.0`<sup>2</sup> |
| [Node.js: Vitest](https://codspeed.io/docs/benchmarks/nodejs/vitest.md) | No | No | `>=7.0.0-beta.0`, with Vitest 5<sup>2</sup> |
| [Node.js: benchmark.js](https://codspeed.io/docs/benchmarks/nodejs/benchmarkjs.md) | No | No | No |
| [Node.js: Playwright](https://codspeed.io/docs/benchmarks/nodejs/playwright.md) | No | No | No |
| [Python: pytest-codspeed](https://codspeed.io/docs/benchmarks/python.md) | No | No | No |
| [C++: google\_benchmark](https://codspeed.io/docs/benchmarks/cpp.md) | No | No | No |
| [Go](https://codspeed.io/docs/benchmarks/go.md) | No | No | No |
| [JVM: JMH](https://codspeed.io/docs/benchmarks/java.md) | No | No | Yes<sup>3</sup> |

<ol>
  <li>
    CPU Simulation and Memory measure each benchmark once, so the state that
    other benchmarks leave behind shows directly in the result. Walltime runs
    each benchmark many times, which makes it less prone to this variance.
  </li>

  <li>
    In Node.js, walltime runs each benchmark in several processes, called forks,
    to measure it across different JIT, allocator, and GC states.
  </li>

  <li>
    JMH runs each benchmark in separate JVMs, called forks, by default. Set the
    number of forks with `@Fork` or `-f`. `CODSPEED_PROCESS_ISOLATION` doesn't
    apply.
  </li>
</ol>

## Turning off process isolation

Set the `CODSPEED_PROCESS_ISOLATION` environment variable to `false` or `0` in
the step that runs your benchmarks to measure all benchmarks in a single
process:

```yaml .github/workflows/codspeed.yml theme={null}
- name: Run the benchmarks
  uses: CodSpeedHQ/action@v5
  env:
    CODSPEED_PROCESS_ISOLATION: false
  with:
    mode: walltime
    run: npm exec vitest bench
```

Other values than `true`, `1`, `false`, and `0` fail the run.


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