lynceus-0.0.1.0: tests/Main.hs
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Exception (evaluate)
import Data.Either (isLeft)
import Data.IORef (modifyIORef', newIORef, readIORef)
import Data.List (isInfixOf)
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Text qualified as T
import Data.Version (showVersion)
import System.FilePath ((</>))
import System.IO.Temp (withSystemTempDirectory)
import System.Info (fullCompilerVersion)
import Test.Hspec
import Lynceus
import Lynceus.Benchmark
import Lynceus.Measure
import Lynceus.Options
import Lynceus.Record
import Lynceus.Run
main :: IO ()
main = hspec $ do
describe "parseRecord" $ do
it "reads what renderRecord writes" $
parseRecord (renderRecord sample) `shouldBe` sample
it "reads a record whose lines end in CRLF" $
parseRecord (T.replace "\n" "\r\n" (renderRecord sample))
`shouldBe` sample
it "ignores comments and blank lines" $
recordEntries (parseRecord "# a 1 2 b\n\ngroup 1 - name\n")
`shouldBe` Map.singleton (Key "group" "name") (Entry 1 Nothing)
describe "readRecord" $
it "reads a missing record as an empty one" $
withSystemTempDirectory "lynceus" $ \dir ->
readRecord (dir </> "missing") `shouldReturn` emptyRecord
describe "validate" $ do
it "accepts single-word groups and benchmarks named by words" $
validate [group "parse" [bench "a module" work, bench "b" work]]
`shouldBe` []
it "rejects a group not named by a single word" $
validate [group "two words" []]
`shouldBe` [InvalidGroupName "two words"]
it "rejects a group named by a reserved word" $
validate [group "compiler" []] `shouldBe` [ReservedGroupName "compiler"]
it "rejects a group named like a comment" $
validate [group "#group" []] `shouldBe` [ReservedGroupName "#group"]
it "rejects a group named twice" $
validate [group "g" [], group "g" []] `shouldBe` [DuplicateGroupName "g"]
it "rejects benchmarks named by anything but words" $
validate [group "g" [bench "" work, bench " a" work, bench "a b" work]]
`shouldBe` [ InvalidBenchName "g" "",
InvalidBenchName "g" " a",
InvalidBenchName "g" "a b"
]
it "rejects a benchmark named twice in one group" $
validate [group "g" [bench "a" work, bench "a" work]]
`shouldBe` [DuplicateBenchName "g" "a"]
describe "parseArguments" $ do
it "gives the options given for no arguments" $
parseArguments defaultOptions [] `shouldBe` Right (Just defaultOptions)
it "sets the arguments on top of the options given" $ do
let given = defaultOptions{optRuns = 3}
parseArguments given ["--tolerance", "1"]
`shouldBe` Right (Just given{optTolerance = 0.01})
it "gives nothing where help is asked for" $
parseArguments defaultOptions ["--help"] `shouldBe` Right Nothing
it "reads tolerances as percentages" $
fmap optTolerance <$> parseArguments defaultOptions ["--tolerance", "1"]
`shouldBe` Right (Just 0.01)
it "rejects a run count that is not positive" $
parseArguments defaultOptions ["--runs", "0"] `shouldSatisfy` isLeft
it "rejects arguments that are not options" $
parseArguments defaultOptions ["extra"] `shouldSatisfy` isLeft
describe "usage" $
it "gives the options given as the defaults" $ do
usage defaultOptions `shouldSatisfy` isInfixOf "(default: 0.5)"
usage defaultOptions `shouldSatisfy` isInfixOf "(default: 0.05)"
usage defaultOptions{optTolerance = 0.012}
`shouldSatisfy` isInfixOf "(default: 1.2)"
describe "steadied" $ do
it "keeps what moved no further than runs differ" $
steadied (Just (Entry 100000 (Just 100000))) (Entry 100001 (Just 99999))
`shouldBe` Entry 100000 (Just 100000)
it "takes what moved further" $
steadied (Just (Entry 100 (Just 100))) (Entry 101 (Just 99))
`shouldBe` Entry 101 (Just 99)
it "takes instructions the record does not have" $
steadied (Just (Entry 100 Nothing)) (Entry 100 (Just 7))
`shouldBe` Entry 100 (Just 7)
describe "verdict" $ do
let judged partial ms =
verdict
defaultOptions
( recordOf
[(Key "format" "Foo", 100000), (Key "format" "Bar", 200000)]
)
partial
(fmap measured <$> ms)
it "finds nothing wrong within the tolerances" $
judged False [(Key "format" "Foo", 100000), (Key "format" "Bar", 200000)]
`shouldBe` []
it "finds a benchmark that moved too far" $
judged False [(Key "format" "Foo", 101000), (Key "format" "Bar", 200000)]
`shouldBe` [moved]
it "finds a group that moved too far together" $
judged False [(Key "format" "Foo", 100300), (Key "format" "Bar", 199500)]
`shouldBe` [moved]
it "finds a benchmark not in the record" $
judged True [(Key "format" "Qux", 1)]
`shouldBe` ["The benchmarks marked (new) are not in the record."]
it "finds a benchmark not measured, unless only some were" $ do
judged False [(Key "format" "Foo", 100000)]
`shouldBe` ["In the record but not measured: format Bar"]
judged True [(Key "format" "Foo", 100000)] `shouldBe` []
it "finds a record made with another compiler" $
verdict defaultOptions sample False []
`shouldSatisfy` ((== 1) . length)
describe "measure" $ do
it "measures the same allocations on every run" $ do
m <- measure Nothing 3 (nf (\n -> [1 .. n :: Int]) 1000)
measuredAllocated m `shouldSatisfy` (> 16000)
measuredSpread m `shouldBe` 0
it "evaluates no further than weak head normal form with whnf" $ do
lazy <- measure Nothing 1 (whnf enum 1000)
strict <- measure Nothing 3 (whnf (reverse . enum) 1000)
measuredAllocated lazy `shouldSatisfy` (< 16000)
measuredAllocated strict `shouldSatisfy` (> 16000)
measuredSpread strict `shouldBe` 0
it "runs a setup anew before every run and leaves it unmeasured" $ do
setups <- newIORef (0 :: Int)
let setup = do
modifyIORef' setups (+ 1)
_ <- evaluate (sum (reverse (enum 100000)))
pure (pure () :: IO ())
m <- measure Nothing 3 (withSetupWhnf setup nfIO)
readIORef setups `shouldReturn` 4
measuredAllocated m `shouldSatisfy` (< 16000)
describe "run" $ do
let loose = defaultOptions{optTolerance = 0.5, optGroupTolerance = 0.5}
groups =
[ group
"lists"
[bench "short" (nf enum 1000), bench "long" (nf enum 10000)]
]
running options mode path = run options mode (\_ -> pure ()) path
it "checks what it accepted" $
withRecord groups $ \path ->
running loose Check path groups `shouldReturn` True
it "fails where the record says otherwise" $
withRecord groups $ \path -> do
tamper path
running loose Check path groups `shouldReturn` False
it "marks the costs that moved too far" $
withRecord groups $ \path -> do
tamper path
said <- newIORef []
_ <- run loose Check (\l -> modifyIORef' said (l :)) path groups
lines' <- readIORef said
filter (\l -> any (`T.isInfixOf` l) ["lists", "short", "long"]) lines'
`shouldSatisfy` all ("%)!" `T.isInfixOf`)
lines' `shouldSatisfy` elem moved
it "fails where a benchmark is not in the record" $
withRecord groups $ \path ->
running loose Check path (group "new" [bench "a" work] : groups)
`shouldReturn` False
it "checks only the benchmarks that match" $
withRecord groups $ \path ->
running
loose{optMatch = Just "short"}
Check
path
(group "new" [bench "a" work] : groups)
`shouldReturn` True
-- | A record of two benchmarks of one group and one of another.
sample :: Record
sample =
Record
{ recordCompiler = "ghc 0.0",
recordProcessor = "none",
recordEntries =
Map.fromList
[ (Key "format" "Foo", Entry 100 Nothing),
(Key "format" "Bar baz", Entry 200 Nothing),
(Key "parse" "Foo", Entry 300 (Just 400))
]
}
-- | A record made with the compiler of the tests of what the given
-- benchmarks allocated.
recordOf :: [(Key, Int)] -> Record
recordOf entries =
emptyRecord
{ recordCompiler = compiler,
recordEntries =
Map.fromList [(k, Entry (fromIntegral a) Nothing) | (k, a) <- entries]
}
-- | What the verdict says where costs moved too far.
moved :: Text
moved = "The costs marked with ! moved further than the tolerances allow."
-- | What a benchmark allocated, with nothing else measured.
measured :: Int -> Measurement
measured allocated = Measurement (fromIntegral allocated) Nothing 0 0
-- | The compiler the tests were built with, as the record names it.
compiler :: Text
compiler = T.pack ("ghc " <> showVersion fullCompilerVersion)
-- | Some work.
work :: Work
work = nf enum 10
-- | A list of numbers up to the given one.
enum :: Int -> [Int]
enum n = [1 .. n]
-- | Run an action with a record accepted for the given benchmarks.
withRecord :: [Group] -> (FilePath -> IO ()) -> IO ()
withRecord groups action =
withSystemTempDirectory "lynceus" $ \dir -> do
let path = dir </> "bench.record"
run defaultOptions Accept (\_ -> pure ()) path groups `shouldReturn` True
action path
-- | Make every benchmark of a record allocate ten times as much.
tamper :: FilePath -> IO ()
tamper path = do
record <- readRecord path
writeRecord
path
record
{ recordEntries =
(\e -> e{entryAllocated = 10 * entryAllocated e})
<$> recordEntries record
}