ac-bench
Tutorials

Tutorial

Your first gate

Turn a comparison into a check that passes, fails or says it cannot decide yet, and see the exit status a CI job reads.

In this tutorial you add a gate to the suite from Your first benchmark: a check that the for loop stays faster than reduce. You will see it pass, fail, and decline to decide.

1. Declare the gate beside the cases

Add ratioGate to the import and one line at the end of the condition:

src/sum.bench.ts
import { durationCase, durationCondition, ratioGate } from "@ac-bench/measure-duration";

const values = Array.from({ length: 100_000 }, (_, index) => index * 0.5);

durationCondition("sum 100 000 numbers", () => {
  durationCase("for loop", () => {
    let total = 0;
    for (let index = 0; index < values.length; index++) total += values[index]!;
    return total;
  });

  durationCase("reduce", () => values.reduce((total, value) => total + value, 0));

  ratioGate("for loop", "reduce", { max: 1.1 });
});

It reads: the time of for loop divided by the time of reduce is at most 1.1.

2. Watch it pass

Terminalexit 0
npx measure -c bench.config.ts…GatesGATE                   RUNTIME  VERDICT  ESTIMATE  INTERVAL                   THRESHOLD  DISPERSION  N    REASONratio:for loop/reduce  node     pass        0.082  [0.07443, 0.08967] (95 %)  ≤ 1.1           0.034  3/3  decided1 pass

The ratio is about 0.08, and the whole interval, [0.074, 0.090], lies under 1.1: the gate passes, and measure exits 0.

3. Watch it fail

Change the threshold to { max: 0.05 } and run again:

Terminalexit 1
npx measure -c bench.config.ts…GATE                   RUNTIME  VERDICT  ESTIMATE  INTERVAL                   THRESHOLD  DISPERSION  N    REASONratio:for loop/reduce  node     fail        0.079  [0.07625, 0.08103] (95 %)  ≤ 0.05          0.041  3/3  decided1 fail  ratio:for loop/reduce (node): for loop/reduce = 0.0786 [0.0763, 0.081] (95 % interval), above 0.05

The whole interval is above 0.05: the gate fails, and measure exits 1. A CI job fails on that status with no extra script. Put the threshold back to 1.1.

4. Watch it decline to decide

--mode quick measures each case in a single process: a fast first look.

Terminalexit 0
npx measure -c bench.config.ts --mode quick…GATE                   RUNTIME  VERDICT       ESTIMATE  INTERVAL  THRESHOLD  DISPERSION  N    REASONratio:for loop/reduce  node     inconclusive                      ≤ 1.1                  1/1  too-few-executions1 inconclusive  ratio:for loop/reduce (node): sum 100 000 numbers > for loop has 1 execution, 3 are needed to decide

One process says nothing about how much a second one would differ, so the gate is inconclusive. In the default mode that is exit status 2; --mode quick accepts it and exits 0, because a quick look is not meant to decide.

What you have done

You declared a gate and saw its three answers. Each is an exit status a CI job reads, and How a gate decides explains why it waits for several processes. To catch a regression against an earlier run of the same suite, see Catch a regression against a baseline.