module Criterion.Config
(
Config(..)
, PrintExit(..)
, MatchType(..)
, Verbosity(..)
, defaultConfig
, fromLJ
, ljust
) where
import Data.Data (Data, Typeable)
import Data.Function (on)
import Data.Monoid (Monoid(..), Last(..))
import GHC.Generics (Generic)
data MatchType = Prefix | Glob
deriving (Eq, Ord, Bounded, Enum, Read, Show, Typeable, Data,
Generic)
data Verbosity = Quiet
| Normal
| Verbose
deriving (Eq, Ord, Bounded, Enum, Read, Show, Typeable, Data,
Generic)
data PrintExit = Nada
| List
| Version
| Help
deriving (Eq, Ord, Bounded, Enum, Read, Show, Typeable, Data,
Generic)
instance Monoid PrintExit where
mempty = Nada
mappend = max
data Config = Config {
cfgBanner :: Last String
, cfgConfInterval :: Last Double
, cfgMatchType :: Last MatchType
, cfgPerformGC :: Last Bool
, cfgPrintExit :: PrintExit
, cfgResamples :: Last Int
, cfgResults :: Last FilePath
, cfgReport :: Last FilePath
, cfgSamples :: Last Int
, cfgSummaryFile :: Last FilePath
, cfgCompareFile :: Last FilePath
, cfgTemplate :: Last FilePath
, cfgVerbosity :: Last Verbosity
, cfgJUnitFile :: Last FilePath
, cfgMeasure :: Last Bool
} deriving (Eq, Read, Show, Typeable, Generic)
instance Monoid Config where
mempty = emptyConfig
mappend = appendConfig
defaultConfig :: Config
defaultConfig = Config {
cfgBanner = ljust "I don't know what version I am."
, cfgConfInterval = ljust 0.95
, cfgMatchType = ljust Prefix
, cfgPerformGC = ljust True
, cfgPrintExit = Nada
, cfgResamples = ljust (100 * 1000)
, cfgResults = mempty
, cfgReport = mempty
, cfgSamples = ljust 100
, cfgSummaryFile = mempty
, cfgCompareFile = mempty
, cfgTemplate = ljust "report.tpl"
, cfgVerbosity = ljust Normal
, cfgJUnitFile = mempty
, cfgMeasure = ljust True
}
ljust :: a -> Last a
ljust = Last . Just
fromLJ :: (Config -> Last a)
-> Config
-> a
fromLJ f cfg = case f cfg of
Last Nothing -> fromLJ f defaultConfig
Last (Just a) -> a
emptyConfig :: Config
emptyConfig = Config {
cfgBanner = mempty
, cfgConfInterval = mempty
, cfgMatchType = mempty
, cfgPerformGC = mempty
, cfgPrintExit = mempty
, cfgReport = mempty
, cfgResamples = mempty
, cfgResults = mempty
, cfgSamples = mempty
, cfgSummaryFile = mempty
, cfgCompareFile = mempty
, cfgTemplate = mempty
, cfgVerbosity = mempty
, cfgJUnitFile = mempty
, cfgMeasure = mempty
}
appendConfig :: Config -> Config -> Config
appendConfig a b =
Config {
cfgBanner = app cfgBanner a b
, cfgConfInterval = app cfgConfInterval a b
, cfgMatchType = app cfgMatchType a b
, cfgPerformGC = app cfgPerformGC a b
, cfgPrintExit = app cfgPrintExit a b
, cfgReport = app cfgReport a b
, cfgResamples = app cfgResamples a b
, cfgResults = app cfgResults a b
, cfgSamples = app cfgSamples a b
, cfgSummaryFile = app cfgSummaryFile a b
, cfgCompareFile = app cfgCompareFile a b
, cfgTemplate = app cfgTemplate a b
, cfgVerbosity = app cfgVerbosity a b
, cfgJUnitFile = app cfgJUnitFile a b
, cfgMeasure = app cfgMeasure a b
}
where app f = mappend `on` f