timestats 0.2.0 → 0.2.1
raw patch · 5 files changed
+48/−15 lines, 5 filesdep +mtl
Dependencies added: mtl
Files
- CHANGELOG.md +4/−0
- src/Debug/TimeStats.hs +3/−2
- src/Debug/TimeStats/Unsafe.hs +1/−5
- tests/Main.hs +29/−5
- timestats.cabal +11/−3
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for timestats +## 0.2.1 -- 2025-07-24++* Avoid negative measures when measuring the lazy State monad (#5).+ ## 0.2.0 -- 2024-05-24 * Expose `Debug.TimeStats.measureMWithLiftIO`.
src/Debug/TimeStats.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE BangPatterns #-} {-# LANGUAGE RankNTypes #-} -- | A module to collect aggregates on how much time is spent in a computation --@@ -243,10 +244,10 @@ -- the given reference to time stats, using the given IO lifting function. measureMWithRef :: Monad m => (forall b. IO b -> m b) -> TimeStatsRef -> m a -> m a measureMWithRef lift tref m = do- t0 <- lift getMonotonicTimeNSec+ !t0 <- lift getMonotonicTimeNSec a <- m lift $ do- tf <- getMonotonicTimeNSec+ !tf <- getMonotonicTimeNSec updateTimeStatsRef tref $ \st -> st { timeStat = (tf - t0) + timeStat st
src/Debug/TimeStats/Unsafe.hs view
@@ -9,11 +9,7 @@ ( unsafeMeasureM ) where -import Debug.TimeStats- ( lookupTimeStatsRef- , measureMWithLiftIO- )-import GHC.Clock (getMonotonicTimeNSec)+import Debug.TimeStats (measureMWithLiftIO) import System.IO.Unsafe (unsafePerformIO) -- | Like 'Debug.TimeStats.measureM' but can measure other monads.
tests/Main.hs view
@@ -1,10 +1,13 @@+{-# OPTIONS_GHC -Wno-x-partial #-} module Main where import Control.Exception (evaluate) import Control.Monad (unless)+import Control.Monad.State import qualified Data.Text.IO as Text import qualified Debug.TimeStats as TimeStats import qualified Debug.TimeStats.Internal as Internal+import qualified Debug.TimeStats.Unsafe as TimeStats import System.Environment (setEnv) import System.Exit (exitFailure) @@ -14,6 +17,7 @@ main :: IO () main = do testMeasureM+ testUnsafeMeasureM testFormatIntWithSeparator testMeasureM :: IO ()@@ -28,13 +32,9 @@ , ("fib2", TimeStats.TimeStats 0 2) ] unless (eqStats xs expected) $ do- putStrLn "unexpected timestats:"+ putStrLn "measureM: unexpected timestats:" Text.putStrLn (TimeStats.asText xs) exitFailure- where- eqStats xs ys = length xs == length ys && and (zipWith eqStat xs ys)- eqStat (lbl0, ts0) (lbl1, ts1) =- lbl0 == lbl1 && TimeStats.countStat ts0 == TimeStats.countStat ts1 testFormatIntWithSeparator :: IO () testFormatIntWithSeparator = do@@ -65,3 +65,27 @@ putStrLn $ "expected: " ++ show (expected ++ "a") putStrLn $ " actual: " ++ show actual exitFailure++testUnsafeMeasureM :: IO ()+testUnsafeMeasureM = do+ setEnv "DEBUG_TIMESTATS_ENABLE" "1"+ TimeStats.reset+ _ <- evaluate $ (`execState` 0) $+ TimeStats.unsafeMeasureM "fib" $ put (fib 40)+ xs <- TimeStats.collect+ let expected =+ [ ("fib", TimeStats.TimeStats 0 1)+ , ("fib2", TimeStats.TimeStats 0 0)+ ]+ -- A large time value indicates an error+ unless (eqStats xs expected+ || TimeStats.timeStat (snd $ head xs) > 1000000000) $ do+ putStrLn "unsafeMeasureM: unexpected timestats:"+ Text.putStrLn (TimeStats.asText xs)+ exitFailure++eqStats :: Eq a => [(a, TimeStats.TimeStats)] -> [(a, TimeStats.TimeStats)] -> Bool+eqStats xs ys = length xs == length ys && and (zipWith eqStat xs ys)+ where+ eqStat (lbl0, ts0) (lbl1, ts1) =+ lbl0 == lbl1 && TimeStats.countStat ts0 == TimeStats.countStat ts1
timestats.cabal view
@@ -1,8 +1,12 @@ cabal-version: 2.4 name: timestats-version: 0.2.0+version: 0.2.1 synopsis: A library for profiling time in Haskell applications+description: This library provides some utilities for instrumenting Haskell+ applications to collect time measures of various fragments of+ code. This is useful to measure wall-clock time when running a+ given piece of Haskell code. See the README for more details. homepage: https://github.com/tweag/timestats bug-reports: https://github.com/tweag/timestats/issues@@ -13,10 +17,14 @@ maintainer: facundo.dominguez@tweag.io category: Profiling-extra-source-files:+extra-doc-files: CHANGELOG.md README.md +source-repository head+ type: git+ location: https://github.com/tweag/timestats+ flag devel default: False manual: True@@ -36,7 +44,7 @@ test-suite tests type: exitcode-stdio-1.0 main-is: Main.hs- build-depends: base, text, timestats+ build-depends: base, mtl, text, timestats if flag(devel) ghc-options: -Wall -Werror hs-source-dirs: tests