lynceus-0.0.1.0: README.md
# Lynceus
* [Quick start](#quick-start)
* [What is checked](#what-is-checked)
* [In CI](#in-ci)
* [Comparing two versions](#comparing-two-versions)
* [Contribution](#contribution)
* [License](#license)
Lynceus is a benchmarking library designed to reliably guard against the
smallest performance regressions. It measures both allocations and
instructions retired in user space. For the latter it relies on
[perf_events], so that functionality is Linux-specific and suits developers
on Linux best.
When we measure time, a regression of a fraction of a percent cannot be told
from noise however careful the statistics. What a piece of work allocates
and the instructions it retires, on the other hand, are the same from one
run to the next to a hundredth of a percent. That makes it possible to keep
them in the repository and check them on every change, like a golden test.
Lynceus was originally designed to aid development of [Tilia][tilia] and
[Megaparsec 10][megaparsec].
[perf_events]: https://en.wikipedia.org/wiki/Perf_%28Linux%29
[tilia]: https://github.com/mrkkrp/tilia
[megaparsec]: https://github.com/mrkkrp/megaparsec
## Quick start
Add a benchmark to the Cabal file. It must be built so that the RTS collects
statistics (`-T`) and garbage collections (`-A1g -O1g`) and context switches
(`-C1000`) do not fall inside a run, since those make the counts depend on
the rest of the heap and on the clock:
```cabal
benchmark bench
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: bench
build-depends:
base,
lynceus
ghc-options: -O2 -rtsopts "-with-rtsopts=-A1g -O1g -C1000 -T"
```
Then write the benchmarks:
```haskell
import Data.List (sort)
import Lynceus
main :: IO ()
main =
defaultMain
"bench/bench.record"
[ group
"sort"
[ bench "ascending" (nf sort [1 .. 100000 :: Int]),
bench "descending" (nf sort [100000, 99999 .. 1 :: Int])
]
]
```
`nf` applies a function to an argument and evaluates the result to normal
form. Consult the Haddocks for a full overview of the API.
Write the record and commit it:
```console
$ LYNCEUS_ACCEPT=1 cabal bench
```
From then on `cabal bench` checks the benchmarks against it and fails if
they moved:
```
sort 7.218 MB (+0.27%)! 20.14 M instr (+0.37%)!
ascending 4.797 MB (+0.00%) 10.72 M instr (+0.00%)
descending 2.420 MB (+0.80%)! 9.425 M instr (+0.80%)!
The costs marked with ! moved further than the tolerances allow.
If that is the intended change, write the record again:
LYNCEUS_ACCEPT=1 cabal bench
```
The record is a plain text file that lists what each benchmark allocates and
the instructions it retires, sorted by group and name, along with the
compiler the benchmarks were built with and the processor the instructions
were counted on. A change to it shows in review like any other, so an
intended regression or improvement is accepted explicitly. Writing it again
keeps the costs that moved no further than two runs of one build differ, so
that the diff shows only what moved.
## What is checked
A run fails where a benchmark allocates or retires 0.5% more or less than
its record or where all the benchmarks of a group together deviate by 0.05%.
Run the benchmark with `--help` to see the options that change these
tolerances, or set other defaults in code with `defaultMainWith`.
A run also fails on a benchmark that is not in the record, which the report
marks `(new)`, and on one that is in the record but was not run, unless
`--match` selected only some of them.
What is allocated depends on the compiler, so it is included in the record,
and the run fails if there is a mismatch. The instructions depend on the
processor as well, so they are checked only on the processor the record
names, where Linux lets a process count them. Elsewhere the run says so and
checks the rest.
## In CI
Write the record on the machine you develop on and check it in CI. What is
allocated does not depend on the processor, only on the code that runs, so
CI checks the allocations as long as it builds the benchmarks the same way:
with the compiler the record names, the same flags, and the same versions of
the dependencies, which an `index-state` in `cabal.project` or a
`cabal.project.freeze` pins. A dependency that changes between the two moves
the allocations just like a regression would.
The processor of a CI runner is rarely the one the record names, and virtual
machines often hide the hardware counters, so *CI checks only the
allocations*. Running the benchmarks locally before pushing checks both,
which catches regressions in instruction counts. Instruct your agents to run
the benchmarks on every change.
## Comparing two versions
Time is not recorded or checked, but to compare two versions on a machine
with a different processor from the one in the record, you can create a
baseline explicitly and compare against that. This shows the time and the
instructions of all the benchmarks together and the benchmarks that moved
most:
```console
$ cabal bench --benchmark-options='--save /tmp/before'
$ cabal bench --benchmark-options='--baseline /tmp/before'
```
## Contribution
Issues, bugs, and questions may be reported in [the GitHub issue tracker for
this project](https://github.com/mrkkrp/lynceus/issues).
Pull requests are also welcome.
## License
Copyright © 2026–present Mark Karpov
Distributed under the BSD 3-clause license.