lynceus: Deterministic benchmarks for catching tiny performance regressions

[ benchmarking, bsd3, library, performance ] [ Propose Tags ] [ Report a vulnerability ]

Deterministic benchmarks for catching tiny performance regressions.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
dev

Turn on development settings.

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.1.0
Change log CHANGELOG.md
Dependencies base (>=4.15 && <5), containers (>=0.6.0.1 && <0.9), deepseq (>=1.4 && <1.6), directory (>=1.3 && <1.4), text (>=2.1 && <2.2) [details]
Tested with ghc ==9.10.3, ghc ==9.12.4, ghc ==9.14.1
License BSD-3-Clause
Author Mark Karpov <markkarpov92@gmail.com>
Maintainer Mark Karpov <markkarpov92@gmail.com>
Uploaded by mrkkrp at 2026-12-03T19:22:54Z
Category Benchmarking, Performance
Home page https://github.com/mrkkrp/lynceus
Bug tracker https://github.com/mrkkrp/lynceus/issues
Source repo head: git clone https://github.com/mrkkrp/lynceus.git
Distributions
Downloads 2 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for lynceus-0.0.1.0

[back to package description]

Lynceus

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 and Megaparsec 10.

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:

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:

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:

$ 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:

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

Pull requests are also welcome.

License

Copyright © 2026–present Mark Karpov

Distributed under the BSD 3-clause license.