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

Debug.TimeStats.Unsafe

Contents

Description

A module with time measuring primitives that might not work in all monads that building allows.

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

unsafeMeasureM :: Monad 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.

This function relies on a hack to perform IO in any monad, which does not always work. In particular, we can expect it to fail in monads where

(m >>= \_ -> undefined) == undefined -- for some computation m

An example of such a monad is the list monad

([()] >>= \_ -> undefined) == undefined

Another example is the Control.Monad.Free.Free IO.

(Control.Monad.Free.Pure () >>= \_ -> undefined) == undefined

But it seems to work on IO or ReaderT IO.

seq (print () >>= \_ -> undefined) () == ()

Also, monads that run the continuation of bind multiple times might only have accounted the time to run the first time only.