purescript-metrics/src/System/Metrics/Timer.purs

132 lines
5.0 KiB
Plaintext
Raw Normal View History

2017-11-15 18:00:34 +05:30
module System.Metrics.Timer
( Timer
2017-11-20 17:06:31 +05:30
, Summary(..)
2017-11-15 18:00:34 +05:30
, new
, update
2017-11-15 18:27:05 +05:30
, time
2017-11-15 18:00:34 +05:30
, fifteenMinuteRate
, fiveMinuteRate
, oneMinuteRate
, meanRate
, clear
, percentiles
, mean
, stdDev
, min
, max
, sum
, count
, read
) where
import Prelude
2017-11-15 18:27:05 +05:30
import Control.Monad.Aff (Aff)
2017-11-15 18:00:34 +05:30
import Control.Monad.Eff (Eff)
2017-11-15 18:27:05 +05:30
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Now (NOW, now)
2017-11-15 18:00:34 +05:30
import Control.Monad.Eff.Ref (REF)
2017-11-15 18:27:05 +05:30
import Data.DateTime.Instant (unInstant)
2017-11-15 19:37:09 +05:30
import Data.Foreign.Class (class Encode)
import Data.Foreign.Generic (defaultOptions, genericEncode)
2017-11-15 18:00:34 +05:30
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Maybe (Maybe)
import Data.Time.Duration (class Duration, Milliseconds(..), fromDuration)
import System.Metrics.Histogram (Histogram)
import System.Metrics.Histogram as Histogram
import System.Metrics.Meter (Meter)
import System.Metrics.Meter as Meter
2017-11-20 17:06:31 +05:30
-- | A timer is a histogram of the duration of a type of event and a meter of the rate of its occurrence.
2017-11-15 18:00:34 +05:30
newtype Timer = Timer { meter :: Meter, histogram :: Histogram }
2017-11-20 17:06:31 +05:30
-- | Creates a new timer.
2017-11-15 18:00:34 +05:30
new :: forall eff. Eff (ref :: REF | eff) Timer
new = Timer <$> ({ meter: _, histogram: _ }
<$> Meter.new
<*> Histogram.newWithExponentialDecaySampling 1028 0.015)
2017-11-20 17:06:31 +05:30
-- | Updates the timer with the duration of a new event.
2017-11-15 18:00:34 +05:30
update :: forall a eff. Duration a => Timer -> a -> Eff (ref :: REF | eff) Unit
update (Timer { meter, histogram }) d = do
let (Milliseconds ms) = fromDuration d
Histogram.update histogram ms
Meter.mark meter
2017-11-20 17:06:31 +05:30
-- | Runs and times the given function, updates the given timer with the run duration, and returns
-- | the return value of the function.
2017-11-15 18:27:05 +05:30
time :: forall a eff. Timer -> Aff (ref :: REF, now :: NOW | eff) a -> Aff (ref :: REF, now :: NOW | eff) a
time timer f = do
start <- liftEff now
r <- f
end <- liftEff now
liftEff $ update timer (unInstant end - unInstant start)
pure r
2017-11-20 17:06:31 +05:30
-- | Returns the fifteen minute moving average rate of the events recorded by the timer.
2017-11-15 18:00:34 +05:30
fifteenMinuteRate :: forall eff. Timer -> Eff (ref :: REF | eff) Number
fifteenMinuteRate (Timer { meter }) = Meter.fifteenMinuteRate meter
2017-11-20 17:06:31 +05:30
-- | Returns the five minute moving average rate of the events recorded by the timer.
2017-11-15 18:00:34 +05:30
fiveMinuteRate :: forall eff. Timer -> Eff (ref :: REF | eff) Number
fiveMinuteRate (Timer { meter }) = Meter.fiveMinuteRate meter
2017-11-20 17:06:31 +05:30
-- | Returns the one minute moving average rate of the events recorded by the timer.
2017-11-15 18:00:34 +05:30
oneMinuteRate :: forall eff. Timer -> Eff (ref :: REF | eff) Number
oneMinuteRate (Timer { meter }) = Meter.oneMinuteRate meter
2017-11-20 17:06:31 +05:30
-- | Returns the mean rate of the events recorded by the timer.
2017-11-15 18:00:34 +05:30
meanRate :: forall eff. Timer -> Eff (ref :: REF | eff) Number
meanRate (Timer { meter }) = Meter.meanRate meter
2017-11-20 17:06:31 +05:30
-- | Clears the timer.
2017-11-15 18:00:34 +05:30
clear :: forall eff. Timer -> Eff (ref :: REF | eff) Unit
clear (Timer { histogram }) = Histogram.clear histogram
2017-11-20 17:06:31 +05:30
-- | Returns the percentiles of the measurements in the timer for the given percentiles array.
2017-11-15 18:00:34 +05:30
percentiles :: forall eff. Timer -> Array Number -> Eff (ref :: REF | eff) (Maybe (Array Number))
percentiles (Timer { histogram }) = Histogram.percentiles histogram
2017-11-20 17:06:31 +05:30
-- | Returns the mean of the measurements in the timer.
2017-11-15 18:00:34 +05:30
mean :: forall eff. Timer -> Eff (ref :: REF | eff) (Maybe Number)
mean (Timer { histogram }) = Histogram.mean histogram
2017-11-20 17:06:31 +05:30
-- | Returns the standard deviation of the measurements in the timer.
2017-11-15 18:00:34 +05:30
stdDev :: forall eff. Timer -> Eff (ref :: REF | eff) (Maybe Number)
stdDev (Timer { histogram }) = Histogram.stdDev histogram
2017-11-20 17:06:31 +05:30
-- | Returns the minimum of the measurements in the timer.
2017-11-15 18:00:34 +05:30
min :: forall eff. Timer -> Eff (ref :: REF | eff) (Maybe Number)
min (Timer { histogram }) = Histogram.min histogram
2017-11-20 17:06:31 +05:30
-- | Returns the maximum of the measurements in the timer.
2017-11-15 18:00:34 +05:30
max :: forall eff. Timer -> Eff (ref :: REF | eff) (Maybe Number)
max (Timer { histogram }) = Histogram.max histogram
2017-11-20 17:06:31 +05:30
-- | Returns the sum of the measurements in the timer.
2017-11-15 18:00:34 +05:30
sum :: forall eff. Timer -> Eff (ref :: REF | eff) (Maybe Number)
sum (Timer { histogram }) = Histogram.sum histogram
2017-11-20 17:06:31 +05:30
-- | Returns the count of the measurements in the timer.
2017-11-15 18:00:34 +05:30
count :: forall eff. Timer -> Eff (ref :: REF | eff) Int
count (Timer { histogram }) = Histogram.count histogram
2017-11-20 17:06:31 +05:30
-- | Summary of the measurements in the meter. Contains:
-- |
-- | - duration: the summary of the underlying event duration histogram.
-- | - rate: the summary of the underlying event rate meter.
2017-11-15 18:00:34 +05:30
newtype Summary = Summary { duration :: Histogram.Summary, rate :: Meter.Summary }
2017-11-15 19:37:09 +05:30
derive instance eqTSummary :: Eq Summary
derive instance genericTSummary :: Generic Summary _
instance showTSummary :: Show Summary where
2017-11-15 18:00:34 +05:30
show = genericShow
2017-11-15 19:37:09 +05:30
instance encodeTSummary :: Encode Summary where
encode = genericEncode $ defaultOptions { unwrapSingleConstructors = true }
2017-11-15 18:00:34 +05:30
2017-11-20 17:06:31 +05:30
-- | Returns the summary of the measurements in the timer.
2017-11-15 18:00:34 +05:30
read :: forall eff. Timer -> Eff (ref :: REF | eff) Summary
read (Timer { meter, histogram }) =
Summary <$> ({ duration: _, rate: _} <$> Histogram.read histogram <*> Meter.read meter)