Skip to content

Commit

Permalink
use replicateM mechanism of endpoint repetition rather than time based
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Dec 13, 2019
1 parent 8e53c9a commit 79652c0
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions lib/jormungandr/test/bench/Latency.hs
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -402,6 +401,10 @@ measureApiLogs = measureLatency
sampleTimeSeconds :: Int
sampleTimeSeconds = 1

-- | Run tests for at least this long to get accurate timings.
sampleNTimes :: Int
sampleNTimes = 10

-- | Measure how long an action takes based on trace points and taking an
-- average of results over a short time period (also DB footprint).
measureLatency
Expand All @@ -414,7 +417,7 @@ measureLatency
-> IO ([NominalDiffTime], [NominalDiffTime])
measureLatency start finish startDB stopDB tvar action = do
atomically $ writeTVar tvar []
_res <- repeatFor sampleTimeSeconds action
replicateM_ sampleNTimes action
extractTimings start finish startDB stopDB . reverse <$> readTVarIO tvar

-- | Scan through iohk-monitoring logs and extract time differences between
Expand Down Expand Up @@ -451,22 +454,6 @@ extractTimings isStart isFinish isObserveStart isObserveStop msgs =

getTimestamp = tstamp . loMeta

-- | Repeatedly run an action, until total elapsed time in seconds is greater
-- than the given amount.
repeatFor :: Int -> IO a -> IO [a]
repeatFor nSeconds action = do
start <- getSystemTime
let repeater rs = do
now <- getSystemTime
if finished start now
then pure rs
else do
!r <- action
repeater (r:rs)
reverse <$> repeater []
where
finished a b = systemSeconds b - systemSeconds a > fromIntegral nSeconds

setupLatencyLogging
:: TVar [LogObject ServerLog]
-> IO (CM.Configuration, Trace IO ServerLog)
Expand Down

0 comments on commit 79652c0

Please sign in to comment.