2016-05-07 04:14:27 +03:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module ProfileTests where
|
|
|
|
|
|
|
|
import Haxl.Prelude
|
|
|
|
import Data.List
|
|
|
|
|
|
|
|
import Haxl.Core
|
2016-08-22 18:21:36 +03:00
|
|
|
import Haxl.Core.Monad
|
2016-05-07 04:14:27 +03:00
|
|
|
|
|
|
|
import Test.HUnit
|
|
|
|
|
2016-08-22 18:21:36 +03:00
|
|
|
import Control.DeepSeq (force)
|
|
|
|
import Control.Exception (evaluate)
|
2016-05-07 04:14:27 +03:00
|
|
|
import Data.IORef
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
import qualified Data.HashSet as HashSet
|
|
|
|
|
|
|
|
import TestUtils
|
|
|
|
|
|
|
|
mkProfilingEnv = do
|
|
|
|
env <- makeTestEnv
|
|
|
|
return env { flags = (flags env) { report = 4 } }
|
|
|
|
|
|
|
|
collectsdata :: Assertion
|
|
|
|
collectsdata = do
|
2017-02-08 12:29:15 +03:00
|
|
|
e <- mkProfilingEnv
|
|
|
|
_x <- runHaxl e $
|
2016-05-07 04:14:27 +03:00
|
|
|
withLabel "bar" $
|
2017-02-08 12:29:15 +03:00
|
|
|
withLabel "foo" $ do
|
|
|
|
u <- env userEnv
|
|
|
|
if length (intersect (HashMap.keys u) ["c"]) > 1
|
2016-05-07 04:14:27 +03:00
|
|
|
then return 5
|
|
|
|
else return (4::Int)
|
2017-02-08 12:29:15 +03:00
|
|
|
profData <- profile <$> readIORef (profRef e)
|
2016-05-07 04:14:27 +03:00
|
|
|
assertEqual "has data" 3 $ HashMap.size profData
|
2016-05-17 14:01:41 +03:00
|
|
|
assertBool "foo allocates" $
|
|
|
|
case profileAllocs <$> HashMap.lookup "foo" profData of
|
|
|
|
Just x -> x > 0
|
|
|
|
Nothing -> False
|
2016-05-07 04:14:27 +03:00
|
|
|
assertEqual "bar does not allocate" (Just 0) $
|
|
|
|
profileAllocs <$> HashMap.lookup "bar" profData
|
|
|
|
assertEqual "foo's parent" (Just ["bar"]) $
|
|
|
|
HashSet.toList . profileDeps <$> HashMap.lookup "foo" profData
|
|
|
|
|
2016-08-22 18:21:36 +03:00
|
|
|
exceptions :: Assertion
|
|
|
|
exceptions = do
|
|
|
|
env <- mkProfilingEnv
|
|
|
|
_x <- runHaxl env $
|
|
|
|
withLabel "outer" $
|
|
|
|
tryToHaxlException $ withLabel "inner" $
|
|
|
|
unsafeLiftIO $ evaluate $ force (error "pure exception" :: Int)
|
|
|
|
profData <- profile <$> readIORef (profRef env)
|
|
|
|
assertBool "inner label not added" $
|
|
|
|
not $ HashMap.member "inner" profData
|
|
|
|
|
|
|
|
env2 <- mkProfilingEnv
|
|
|
|
_x <- runHaxl env2 $
|
|
|
|
withLabel "outer" $
|
|
|
|
tryToHaxlException $ withLabel "inner" $
|
|
|
|
throw $ NotFound "haxl exception"
|
|
|
|
profData <- profile <$> readIORef (profRef env2)
|
|
|
|
assertBool "inner label added" $
|
|
|
|
HashMap.member "inner" profData
|
|
|
|
|
2016-05-07 04:14:27 +03:00
|
|
|
tests = TestList
|
|
|
|
[ TestLabel "collectsdata" $ TestCase collectsdata
|
2016-08-22 18:21:36 +03:00
|
|
|
, TestLabel "exceptions" $ TestCase exceptions
|
2016-05-07 04:14:27 +03:00
|
|
|
]
|