timestats-0.1.1: A library for profiling time in Haskell applications
Safe HaskellSafe-Inferred
LanguageHaskell2010

Debug.TimeStats

Description

A module to collect aggregates on how much time is spent in a computation

Aggregates can be identified with a label that determines where the time of each computation is accounted for.

Measures are collected only if the environment variable DEBUG_TIMESTATS_ENABLE is set to any value ahead of invoking any function in this module.

Synopsis

Measuring

measureM :: MonadIO m => String -> m a -> m a Source #

Measure the time it takes to run the action.

Add the time to the stats of the given label and increase its count by one.

measureM keeps the stats in a globally available store in order to minimize the changes necessary when instrumenting a program. Otherwise a reference to the store would need to be passed to every function that might invoke functions that need this reference.

A time measure isn't collected if the given action fails with an exception. This is a deliberate choice to demand less of the monad in which measures are taken.

Time measures aren't collected either if the environment variable DEBUG_TIMESTATS_ENABLE isn't set the first time this function is evaluated.

measurePure :: String -> a -> a Source #

Pure version of measureM. Measures the time taken to reduce the given value to head normal form.

measurePure is a bit dangerous to use in contexts where there are monadic computations. If measurePure is applied to a monadic computation it will measure the time of constructing the computation rather than the time of executing it, and the typechecker won't catch the mistake. We try to fence against it with a longer name.

Time stats manipulation

printTimeStats :: MonadIO m => m () Source #

Prints the time stats to stderr.

hPrintTimeStats :: MonadIO m => Handle -> m () Source #

Prints the time stats to the given handle.

reset :: MonadIO m => m () Source #

Set all statistics to initial values.

data TimeStats Source #

Reports how much time (in nanoseconds) the invocations to measureM took for a given label and how many times it was invoked on a given label.

Constructors

TimeStats 

Fields

Instances

Instances details
Show TimeStats Source # 
Instance details

Defined in Debug.TimeStats

collect :: MonadIO m => m [(String, TimeStats)] Source #

Yields the labels and the stats collected thus far.

asText :: [(String, TimeStats)] -> Text Source #

Renders the given time stats in a tabular format

scope :: MonadIO m => m a -> m a Source #

Run an action by previously reseting all stats to initial values and printing them afterwards.

Not intended for direct use

These definitions are not intended for instrumenting applications, but they can be handy to implement other measuring primitives.

data TimeStatsRef Source #

A reference to a TimeStats value

lookupTimeStatsRef :: String -> IO TimeStatsRef Source #

Looks up the stats of a label. If no stats are found for the label, a new TimeStatsRef is created with initial values.

updateTimeStatsRef :: TimeStatsRef -> (TimeStats -> TimeStats) -> IO () Source #

Updates the TimeStats in a TimeStatsRef