{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, GADTs, RecordWildCards #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Criterion.Types
(
Config(..)
, Verbosity(..)
, Benchmarkable(..)
, Benchmark(..)
, Measured(..)
, fromInt
, toInt
, fromDouble
, toDouble
, measureAccessors
, measureKeys
, measure
, rescale
, env
, envWithCleanup
, perBatchEnv
, perBatchEnvWithCleanup
, perRunEnv
, perRunEnvWithCleanup
, toBenchmarkable
, bench
, bgroup
, addPrefix
, benchNames
, nf
, whnf
, nfIO
, whnfIO
, nfAppIO
, whnfAppIO
, Outliers(..)
, OutlierEffect(..)
, OutlierVariance(..)
, Regression(..)
, KDE(..)
, Report(..)
, SampleAnalysis(..)
, DataRecord(..)
) where
import Control.DeepSeq (NFData(rnf))
import Criterion.Measurement.Types
import Data.Aeson (FromJSON(..), ToJSON(..))
import Data.Binary (Binary(..), putWord8, getWord8)
import Data.Binary.Orphans ()
import Data.Data (Data, Typeable)
import Data.Int (Int64)
import Data.Map (Map)
import GHC.Generics (Generic)
import Prelude ()
import Prelude.Compat
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import qualified Statistics.Types as St
import Statistics.Resampling.Bootstrap ()
data Verbosity = Quiet
| Normal
| Verbose
deriving (Eq, Ord, Bounded, Enum, Read, Show, Typeable, Data,
Generic)
data Config = Config {
confInterval :: St.CL Double
, timeLimit :: Double
, resamples :: Int
, regressions :: [([String], String)]
, rawDataFile :: Maybe FilePath
, reportFile :: Maybe FilePath
, csvFile :: Maybe FilePath
, jsonFile :: Maybe FilePath
, junitFile :: Maybe FilePath
, verbosity :: Verbosity
, template :: FilePath
} deriving (Eq, Read, Show, Typeable, Data, Generic)
data Outliers = Outliers {
samplesSeen :: !Int64
, lowSevere :: !Int64
, lowMild :: !Int64
, highMild :: !Int64
, highSevere :: !Int64
} deriving (Eq, Read, Show, Typeable, Data, Generic)
instance FromJSON Outliers
instance ToJSON Outliers
instance Binary Outliers where
put (Outliers v w x y z) = put v >> put w >> put x >> put y >> put z
get = Outliers <$> get <*> get <*> get <*> get <*> get
instance NFData Outliers
data OutlierEffect = Unaffected
| Slight
| Moderate
| Severe
deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)
instance FromJSON OutlierEffect
instance ToJSON OutlierEffect
instance Binary OutlierEffect where
put Unaffected = putWord8 0
put Slight = putWord8 1
put Moderate = putWord8 2
put Severe = putWord8 3
get = do
i <- getWord8
case i of
0 -> return Unaffected
1 -> return Slight
2 -> return Moderate
3 -> return Severe
_ -> fail $ "get for OutlierEffect: unexpected " ++ show i
instance NFData OutlierEffect
instance Semigroup Outliers where
(<>) = addOutliers
instance Monoid Outliers where
mempty = Outliers 0 0 0 0 0
#if !(MIN_VERSION_base(4,11,0))
mappend = addOutliers
#endif
addOutliers :: Outliers -> Outliers -> Outliers
addOutliers (Outliers s a b c d) (Outliers t w x y z) =
Outliers (s+t) (a+w) (b+x) (c+y) (d+z)
{-# INLINE addOutliers #-}
data OutlierVariance = OutlierVariance {
ovEffect :: OutlierEffect
, ovDesc :: String
, ovFraction :: Double
} deriving (Eq, Read, Show, Typeable, Data, Generic)
instance FromJSON OutlierVariance
instance ToJSON OutlierVariance
instance Binary OutlierVariance where
put (OutlierVariance x y z) = put x >> put y >> put z
get = OutlierVariance <$> get <*> get <*> get
instance NFData OutlierVariance where
rnf OutlierVariance{..} = rnf ovEffect `seq` rnf ovDesc `seq` rnf ovFraction
data Regression = Regression {
regResponder :: String
, regCoeffs :: Map String (St.Estimate St.ConfInt Double)
, regRSquare :: St.Estimate St.ConfInt Double
} deriving (Eq, Read, Show, Typeable, Generic)
instance FromJSON Regression
instance ToJSON Regression
instance Binary Regression where
put Regression{..} =
put regResponder >> put regCoeffs >> put regRSquare
get = Regression <$> get <*> get <*> get
instance NFData Regression where
rnf Regression{..} =
rnf regResponder `seq` rnf regCoeffs `seq` rnf regRSquare
data SampleAnalysis = SampleAnalysis {
anRegress :: [Regression]
, anMean :: St.Estimate St.ConfInt Double
, anStdDev :: St.Estimate St.ConfInt Double
, anOutlierVar :: OutlierVariance
} deriving (Eq, Read, Show, Typeable, Generic)
instance FromJSON SampleAnalysis
instance ToJSON SampleAnalysis
instance Binary SampleAnalysis where
put SampleAnalysis{..} = do
put anRegress; put anMean; put anStdDev; put anOutlierVar
get = SampleAnalysis <$> get <*> get <*> get <*> get
instance NFData SampleAnalysis where
rnf SampleAnalysis{..} =
rnf anRegress `seq` rnf anMean `seq`
rnf anStdDev `seq` rnf anOutlierVar
data KDE = KDE {
kdeType :: String
, kdeValues :: U.Vector Double
, kdePDF :: U.Vector Double
} deriving (Eq, Read, Show, Typeable, Data, Generic)
instance FromJSON KDE
instance ToJSON KDE
instance Binary KDE where
put KDE{..} = put kdeType >> put kdeValues >> put kdePDF
get = KDE <$> get <*> get <*> get
instance NFData KDE where
rnf KDE{..} = rnf kdeType `seq` rnf kdeValues `seq` rnf kdePDF
data Report = Report {
reportNumber :: Int
, reportName :: String
, reportKeys :: [String]
, reportMeasured :: V.Vector Measured
, reportAnalysis :: SampleAnalysis
, reportOutliers :: Outliers
, reportKDEs :: [KDE]
} deriving (Eq, Read, Show, Typeable, Generic)
instance FromJSON Report
instance ToJSON Report
instance Binary Report where
put Report{..} =
put reportNumber >> put reportName >> put reportKeys >>
put reportMeasured >> put reportAnalysis >> put reportOutliers >>
put reportKDEs
get = Report <$> get <*> get <*> get <*> get <*> get <*> get <*> get
instance NFData Report where
rnf Report{..} =
rnf reportNumber `seq` rnf reportName `seq` rnf reportKeys `seq`
rnf reportMeasured `seq` rnf reportAnalysis `seq` rnf reportOutliers `seq`
rnf reportKDEs
data DataRecord = Measurement Int String (V.Vector Measured)
| Analysed Report
deriving (Eq, Read, Show, Typeable, Generic)
instance Binary DataRecord where
put (Measurement i n v) = putWord8 0 >> put i >> put n >> put v
put (Analysed r) = putWord8 1 >> put r
get = do
w <- getWord8
case w of
0 -> Measurement <$> get <*> get <*> get
1 -> Analysed <$> get
_ -> error ("bad tag " ++ show w)
instance NFData DataRecord where
rnf (Measurement i n v) = rnf i `seq` rnf n `seq` rnf v
rnf (Analysed r) = rnf r
instance FromJSON DataRecord
instance ToJSON DataRecord