2011-12-29 19:45:47 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
-- | Example program that continously computes the mean of a list of
|
|
|
|
-- numbers.
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception
|
2013-02-22 17:42:43 +00:00
|
|
|
import Data.List
|
2014-04-21 13:53:30 +00:00
|
|
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
2014-10-15 07:09:35 +00:00
|
|
|
import qualified System.Metrics.Distribution as Distribution
|
|
|
|
import qualified System.Metrics.Counter as Counter
|
|
|
|
import qualified System.Metrics.Label as Label
|
2011-12-29 19:45:47 +00:00
|
|
|
import System.Remote.Monitoring
|
|
|
|
|
2013-02-22 17:42:43 +00:00
|
|
|
-- 'sum' is using a non-strict lazy fold and will blow the stack.
|
|
|
|
sum' :: Num a => [a] -> a
|
|
|
|
sum' = foldl' (+) 0
|
|
|
|
|
2011-12-29 19:45:47 +00:00
|
|
|
mean :: Fractional a => [a] -> a
|
2013-02-22 17:42:43 +00:00
|
|
|
mean xs = sum' xs / fromIntegral (length xs)
|
2011-12-29 19:45:47 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
handle <- forkServer "localhost" 8000
|
|
|
|
counter <- getCounter "iterations" handle
|
2012-04-18 02:06:32 +00:00
|
|
|
label <- getLabel "args" handle
|
2014-10-15 07:09:35 +00:00
|
|
|
event <- getDistribution "runtime" handle
|
2012-04-18 02:06:32 +00:00
|
|
|
Label.set label "some text string"
|
2011-12-29 19:45:47 +00:00
|
|
|
let loop n = do
|
2014-04-21 13:53:30 +00:00
|
|
|
t <- timed $ evaluate $ mean [1..n]
|
2014-10-15 07:09:35 +00:00
|
|
|
Distribution.add event t
|
2011-12-29 19:45:47 +00:00
|
|
|
threadDelay 2000
|
|
|
|
Counter.inc counter
|
|
|
|
loop n
|
|
|
|
loop 1000000
|
2014-04-21 13:53:30 +00:00
|
|
|
|
|
|
|
timed :: IO a -> IO Double
|
|
|
|
timed m = do
|
|
|
|
start <- getTime
|
|
|
|
m
|
|
|
|
end <- getTime
|
|
|
|
return $! end - start
|
|
|
|
|
|
|
|
getTime :: IO Double
|
|
|
|
getTime = realToFrac `fmap` getPOSIXTime
|