packages feed

lynceus-0.0.1.0: src/Lynceus.hs

-- |
-- Module      :  Lynceus
-- Copyright   :  © 2026–present Mark Karpov
-- License     :  BSD 3 clause
--
-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- Benchmarks that measure allocations and, where Linux lets a process count
-- them, the instructions it retires in user space. Both are the same from
-- one run to the next to a hundredth of a percent, so they are kept in a
-- record in the repository and checked like a golden test, which catches
-- regressions far smaller than timing can.
--
-- > 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])
-- >         ]
-- >     ]
--
-- The benchmark must be built so that the RTS collects statistics and
-- collections and context switches do not fall inside a run, which would
-- make the counts depend on the rest of the heap and on the clock:
--
-- > ghc-options: -rtsopts "-with-rtsopts=-A1g -O1g -C1000 -T"
--
-- Run the benchmarks with @LYNCEUS_ACCEPT=1@ to write the record, and
-- without it to check it.
module Lynceus
  ( -- * Running
    defaultMain,
    defaultMainWith,

    -- * Options
    Options (..),
    defaultOptions,

    -- * Benchmarks
    Group,
    group,
    Bench,
    bench,

    -- * Work
    Work,
    nf,
    whnf,
    nfIO,
    whnfIO,
    withSetup,
    withSetupWhnf,
  )
where

import Control.Monad (unless)
import Data.Text.IO qualified as T
import GHC.Stats (getRTSStatsEnabled)
import System.Environment (getArgs, lookupEnv)
import System.Exit (die, exitFailure, exitSuccess)
import System.IO
  ( BufferMode (..),
    hSetBuffering,
    hSetEncoding,
    stderr,
    stdout,
    utf8,
  )

import Lynceus.Benchmark
import Lynceus.Measure
import Lynceus.Options
import Lynceus.Run

-- | Measure the benchmarks and check what they cost against the record at
-- the given path, or write the record anew where @LYNCEUS_ACCEPT@ is set to
-- anything but empty, @0@, @no@, or @false@.
defaultMain :: FilePath -> [Group] -> IO ()
defaultMain = defaultMainWith defaultOptions

-- | 'defaultMain' with the given options as the defaults that the command
-- line overrides.
defaultMainWith :: Options -> FilePath -> [Group] -> IO ()
defaultMainWith defaults path groups = do
  hSetBuffering stdout LineBuffering
  hSetEncoding stdout utf8
  hSetEncoding stderr utf8
  enabled <- getRTSStatsEnabled
  unless enabled $
    die
      "The RTS does not collect statistics: build the benchmark with\n\
      \    ghc-options: -rtsopts \"-with-rtsopts=-A1g -O1g -C1000 -T\""
  options <-
    getArgs >>= \args -> case parseArguments defaults args of
      Left err -> die (err <> "\n" <> usage defaults)
      Right Nothing -> putStr (usage defaults) >> exitSuccess
      Right (Just options) -> pure options
  case validate groups of
    [] -> pure ()
    problems -> die (unlines (explain <$> problems))
  accepting <-
    maybe False (`notElem` ["", "0", "no", "false"])
      <$> lookupEnv "LYNCEUS_ACCEPT"
  passed <-
    run options (if accepting then Accept else Check) T.putStrLn path groups
  unless passed exitFailure