lynceus-0.0.1.0: src/Lynceus/Benchmark.hs
{-# LANGUAGE LambdaCase #-}
-- |
-- Module : Lynceus.Benchmark
-- Copyright : © 2026–present Mark Karpov
-- License : BSD 3 clause
--
-- Maintainer : Mark Karpov <markkarpov92@gmail.com>
-- Stability : experimental
-- Portability : portable
--
-- Benchmarks and the groups they are checked in.
module Lynceus.Benchmark
( Bench,
benchName,
benchWork,
Group,
group,
bench,
groupName,
groupBenchmarks,
Problem (..),
validate,
explain,
)
where
import Data.List (intercalate, isPrefixOf)
import Data.Map.Strict qualified as Map
import Lynceus.Measure
-- | A piece of work to measure.
data Bench = Bench String Work
-- | Name a piece of work.
bench :: String -> Work -> Bench
bench = Bench
-- | The name of a benchmark, unique within its group.
benchName :: Bench -> String
benchName (Bench name _) = name
-- | The work a benchmark measures.
benchWork :: Bench -> Work
benchWork (Bench _ work) = work
-- | Benchmarks whose costs are also checked together.
data Group = Group String [Bench]
-- | Group benchmarks under a name, a single word.
group :: String -> [Bench] -> Group
group = Group
-- | The name of a group, a single word.
groupName :: Group -> String
groupName (Group name _) = name
-- | The benchmarks of a group.
groupBenchmarks :: Group -> [Bench]
groupBenchmarks (Group _ benchmarks) = benchmarks
-- | What is wrong with the name of a group or a benchmark.
data Problem
= -- | A group not named by a single word.
InvalidGroupName String
| -- | A group named @compiler@ or @processor@ or starting with @#@, which
-- the record would misread.
ReservedGroupName String
| -- | A group named more than once.
DuplicateGroupName String
| -- | A benchmark not named by words separated by single spaces, by the
-- names of its group and its own.
InvalidBenchName String String
| -- | A benchmark named more than once in a group, by the names of the
-- group and the benchmark.
DuplicateBenchName String String
deriving (Eq, Show)
-- | What is wrong with the names of the groups and benchmarks.
validate :: [Group] -> [Problem]
validate groups =
[InvalidGroupName g | g <- names, not (word g)]
<> [ ReservedGroupName g
| g <- names,
word g,
g `elem` reserved || "#" `isPrefixOf` g
]
<> [DuplicateGroupName g | g <- repeated names]
<> [ InvalidBenchName (groupName g) n
| g <- groups,
n <- benchName <$> groupBenchmarks g,
null n || unwords (words n) /= n
]
<> [ DuplicateBenchName (groupName g) n
| g <- groups,
n <- repeated (benchName <$> groupBenchmarks g)
]
where
names = groupName <$> groups
word t = words t == [t]
repeated xs =
Map.keys
(Map.filter (> (1 :: Int)) (Map.fromListWith (+) [(x, 1) | x <- xs]))
-- | Explain a problem in a line.
explain :: Problem -> String
explain = \case
InvalidGroupName g ->
"A group must be named by a single word: " <> quoted g
ReservedGroupName g ->
"A group must not be named "
<> intercalate " or " reserved
<> " or start with #: "
<> quoted g
DuplicateGroupName g -> "Group named more than once: " <> g
InvalidBenchName g n ->
"A benchmark must be named by words separated by single spaces: "
<> g
<> " "
<> quoted n
DuplicateBenchName g n -> "Benchmark named more than once: " <> g <> " " <> n
where
quoted t = "\"" <> t <> "\""
-- | The words that start the lines of the record's header.
reserved :: [String]
reserved = ["compiler", "processor"]