Skip to content

Commit 926b3a0

Browse files
committed
sim-benchmarks: refactor PeerMetrics benchmarks
Use `env`, rather than storing data on heap for whole run of all benchmarks.
1 parent daa42a9 commit 926b3a0

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

ouroboros-network-testing/ouroboros-network-testing.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ library
6868
cborg >=0.2.1 && <0.3,
6969
containers,
7070
contra-tracer,
71+
deepseq,
7172
deque ^>=0.4,
7273
io-classes:{io-classes, si-timers, strict-stm} ^>=1.8.0.1,
7374
io-sim,

ouroboros-network-testing/src/Test/Ouroboros/Network/Data/Script.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
{-# LANGUAGE DeriveTraversable #-}
2-
{-# LANGUAGE DerivingVia #-}
3-
{-# LANGUAGE TupleSections #-}
1+
{-# LANGUAGE DeriveTraversable #-}
2+
{-# LANGUAGE DerivingVia #-}
3+
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
4+
{-# LANGUAGE TupleSections #-}
45

56
module Test.Ouroboros.Network.Data.Script
67
( -- * Test scripts
@@ -40,6 +41,7 @@ import Data.Set qualified as Set
4041
import Control.Concurrent.Class.MonadSTM (TVar)
4142
import Control.Concurrent.Class.MonadSTM qualified as LazySTM
4243
import Control.Concurrent.Class.MonadSTM.Strict
44+
import Control.DeepSeq
4345
import Control.Monad.Class.MonadAsync
4446
import Control.Monad.Class.MonadFork
4547
import Control.Monad.Class.MonadTimer.SI
@@ -55,6 +57,7 @@ import Test.QuickCheck
5557

5658
newtype Script a = Script (NonEmpty a)
5759
deriving (Eq, Show, Functor, Foldable, Traversable)
60+
deriving newtype NFData
5861

5962
singletonScript :: a -> Script a
6063
singletonScript x = Script (x :| [])

ouroboros-network/bench/Main.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import Test.Ouroboros.Network.PeerSelection.PeerMetric
88
(microbenchmark1GenerateInput, microbenchmark1ProcessInput)
99

1010
main :: IO ()
11-
main = do
12-
is <- mapM (microbenchmark1GenerateInput False . snd) benchmarks
11+
main =
1312
defaultMain
14-
[bgroup "ouroboros-network:sim-benchmarks"
15-
[ bench (unwords ["microbenchmark1",name])
16-
$ nfAppIO microbenchmark1ProcessInput i
17-
| ((name,_),i) <- zip benchmarks is
18-
]
13+
[ bgroup "ouroboros-network:sim-benchmarks"
14+
[ bgroup "PeerMetrics"
15+
[ env (microbenchmark1GenerateInput False 1_000) $ \i ->
16+
bench "1k" $ nfAppIO microbenchmark1ProcessInput i
17+
, env (microbenchmark1GenerateInput False 10_000) $ \i ->
18+
bench "10k" $ nfAppIO microbenchmark1ProcessInput i
19+
, env (microbenchmark1GenerateInput False 100_000) $ \i ->
20+
bench "100k" $ nfAppIO microbenchmark1ProcessInput i
21+
]
22+
]
1923
]
20-
where
21-
benchmarks = [("1k" , 1000)
22-
,("10k" , 10_000)
23-
,("100k",100_000)
24-
]

ouroboros-network/testlib/Test/Ouroboros/Network/PeerSelection/PeerMetric.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveGeneric #-}
24
{-# LANGUAGE DerivingStrategies #-}
35
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
46
{-# LANGUAGE LambdaCase #-}
@@ -27,6 +29,7 @@ import Data.Map.Merge.Strict qualified as Map
2729
import Data.Map.Strict (Map)
2830
import Data.Map.Strict qualified as Map
2931
import Data.Set qualified as Set
32+
import GHC.Generics
3033

3134
import Network.Mux.Trace (TraceLabelPeer (..))
3235

@@ -73,7 +76,7 @@ instance Arbitrary TestAddress where
7376
data Event =
7477
FetchedHeader TestAddress SlotNo
7578
| FetchedBlock TestAddress SlotNo SizeInBytes
76-
deriving Show
79+
deriving (Show, Generic, NFData)
7780

7881
eventPeer :: Event -> TestAddress
7982
eventPeer (FetchedHeader peer _) = peer
@@ -100,6 +103,7 @@ instance Arbitrary Event where
100103

101104
newtype FixedScript = FixedScript { getFixedScript :: Script Event }
102105
deriving Show
106+
deriving newtype NFData
103107

104108
-- | Order events by 'SlotNo'
105109
--
@@ -446,9 +450,12 @@ microbenchmark1GenerateInput verbose' n = do
446450
mapM_ print (let FixedScript s = fixedScript in s)
447451
return fixedScript
448452

453+
-- TODO:
454+
-- * we shouldn't use QuickCheck
455+
-- * and we shouldn't use IOSim (which `prop_simScript`) is using.
449456
microbenchmark1ProcessInput :: FixedScript -> IO ()
450457
microbenchmark1ProcessInput =
451-
quickCheckWith (stdArgs{maxSuccess=1}) . prop_simScript
458+
quickCheckWith (stdArgs{maxSuccess=1,chatty=False}) . prop_simScript
452459

453460
microbenchmark1 :: Bool -> Int -> IO ()
454461
microbenchmark1 verbose' n =

0 commit comments

Comments
 (0)