Skip to content

Commit 7219242

Browse files
tbagrel1agustinmista
authored andcommitted
Add bench for makeDecision (ObjectDiffusion V2)
1 parent 53fd6f7 commit 7219242

File tree

3 files changed

+459
-0
lines changed

3 files changed

+459
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE ImportQualifiedPost #-}
4+
{-# LANGUAGE NumericUnderscores #-}
5+
{-# LANGUAGE TypeApplications #-}
6+
7+
-- | This module contains benchmarks for Peras Object diffusion decision logic
8+
-- as implemented by the by the function
9+
-- 'Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.Decision.makeDecision'
10+
module Main (main) where
11+
12+
import Control.DeepSeq (NFData (..))
13+
import Control.Exception (evaluate)
14+
import Data.Hashable (Hashable)
15+
import GHC.Generics (Generic)
16+
import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.Decision qualified as OD
17+
import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.TestUtils qualified as OD
18+
import Test.QuickCheck (Arbitrary (..), generate)
19+
import Test.QuickCheck.Gen (vectorOf)
20+
import Test.Tasty.Bench
21+
22+
-- TODO: We will probably want to use the actual types used in vote/cert diffusion,
23+
-- instead of placeholders.
24+
newtype DummyPeerAddr = DummyPeerAddr Int
25+
deriving (Eq, Ord, Generic, NFData)
26+
27+
instance Arbitrary DummyPeerAddr where
28+
arbitrary = DummyPeerAddr <$> arbitrary
29+
30+
newtype DummyObjectId = DummyObjectId Int
31+
deriving (Eq, Ord, Generic, Hashable, NFData)
32+
33+
instance Arbitrary DummyObjectId where
34+
arbitrary = DummyObjectId <$> arbitrary
35+
36+
data DummyObject = DummyObject
37+
{ doId :: DummyObjectId
38+
, doPayload :: ()
39+
}
40+
deriving (Eq, Ord, Generic, Hashable, NFData)
41+
42+
instance Arbitrary DummyObject where
43+
arbitrary = DummyObject <$> arbitrary <*> arbitrary
44+
45+
-- TODO: We should probably use specific policies that are well suited to the
46+
-- number of peers and objects.
47+
48+
main :: IO ()
49+
main =
50+
defaultMain
51+
[ bgroup
52+
"ouroboros-consensus:ObjectDiffusion"
53+
[ bgroup
54+
"VoteDiffusion"
55+
[ env
56+
(genToNF $ vectorOf 1_000 $ OD.genDecisionContext 10 50 doId Nothing)
57+
( \contexts ->
58+
bench "makeDecisions: 1000 decisions with (10 pairs, 50 objects) each" $
59+
nf (fmap makeVoteDiffusionDecisions) contexts
60+
)
61+
, env
62+
(genToNF $ vectorOf 1_000 $ OD.genDecisionContext 100 500 doId Nothing)
63+
( \contexts ->
64+
bench "makeDecisions: 1000 decisions with (100 pairs, 500 objects) each" $
65+
nf (fmap makeVoteDiffusionDecisions) contexts
66+
)
67+
, env
68+
(genToNF $ vectorOf 1_000 $ OD.genDecisionContext 1_000 5_000 doId Nothing)
69+
( \contexts ->
70+
bench "makeDecisions: 1000 decisions with (1000 pairs, 5000 objects) each" $
71+
nf (fmap makeVoteDiffusionDecisions) contexts
72+
)
73+
]
74+
, bgroup "CertDiffusion" []
75+
]
76+
]
77+
where
78+
genToNF gen = do
79+
x <- generate gen
80+
evaluate $ rnf x
81+
pure $! x
82+
83+
makeVoteDiffusionDecisions decisionContext =
84+
OD.makeDecisions @DummyPeerAddr @DummyObjectId @DummyObject decisionContext

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ library
197197
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.Decision
198198
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.Registry
199199
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.State
200+
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.TestUtils
200201
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound.V2.Types
201202
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API
202203
Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.PerasCert
@@ -323,6 +324,7 @@ library
323324

324325
build-depends:
325326
FailT ^>=0.1.2,
327+
QuickCheck,
326328
aeson,
327329
base >=4.14 && <4.22,
328330
base-deriving-via,
@@ -935,6 +937,20 @@ benchmark PerasCertDB-bench
935937
tasty-bench,
936938
unstable-consensus-testlib,
937939

940+
benchmark ObjectDiffusion-bench
941+
import: common-bench
942+
type: exitcode-stdio-1.0
943+
hs-source-dirs: bench/ObjectDiffusion-bench
944+
main-is: Main.hs
945+
other-modules:
946+
build-depends:
947+
QuickCheck,
948+
base,
949+
deepseq,
950+
hashable,
951+
ouroboros-consensus,
952+
tasty-bench,
953+
938954
test-suite doctest
939955
import: common-test
940956
main-is: doctest.hs

0 commit comments

Comments
 (0)