module TestBench.Commands
( RunTestBench(..)
, optionParser
, testBenchConfig
, resetUnusedConfig
, versionInfo
, parseWith
, configParser
, weighIndexArg
, weighFileArg
) where
import Criterion.Main.Options (config, defaultConfig)
import Criterion.Types (Config(..), Verbosity(Quiet))
import Options.Applicative.Builder (auto, footer, fullDesc, header, help, info,
internal, long, option, short, strOption,
switch)
import Options.Applicative.Extra (helper)
import Options.Applicative.Types (Parser, ParserInfo)
import Control.Applicative ((<|>))
import Data.Monoid ((<>))
import Data.Version (showVersion)
import Paths_testbench (version)
data RunTestBench = Version
| List
| Weigh !Int !FilePath
| Run { runTests :: !Bool
, runBench :: !Bool
, benchCfg :: !Config
}
deriving (Eq, Show, Read)
optionParser :: Config -> ParserInfo RunTestBench
optionParser cfg = info (helper <*> parseWith cfg) $
header versionInfo
<> fullDesc
<> footer (unwords [ "Most of these options are for Criterion; see it for more information."
, "However, not all are used: CSV is the only allowed output file, and"
, "verbosity is always set to Quiet."
])
parseWith :: Config -> Parser RunTestBench
parseWith cfg =
runParse
<|> Version <$ switch (long "version" <> short 'V' <> help "Show version information")
<|> List <$ switch (long "list" <> short 'l' <> help "List all benchmarks")
<|> Weigh <$> option auto (long weighIndexArg <> internal)
<*> strOption (long weighFileArg <> internal)
where
runParse = Run <$> (not <$> switch (long "no-tests" <> help "Don't run tests"))
<*> (not <$> switch (long "no-bench" <> help "Don't run benchmarks"))
<*> configParser cfg
versionInfo :: String
versionInfo = "TestBench - " ++ showVersion version
testBenchConfig :: Config
testBenchConfig = resetUnusedConfig defaultConfig
resetUnusedConfig :: Config -> Config
resetUnusedConfig cfg = cfg { rawDataFile = Nothing
, reportFile = Nothing
, jsonFile = Nothing
, junitFile = Nothing
, verbosity = Quiet
}
configParser :: Config -> Parser Config
configParser = config . resetUnusedConfig
weighIndexArg :: String
weighIndexArg = "only-weigh-this-index"
weighFileArg :: String
weighFileArg = "write-weigh-results-to"