Skip to content

Commit c36cd64

Browse files
authored
mempool-bench: move more pre-calculation into withResource (#1227)
PR #1175 makes a change that alters the cost of the size of the pre-generated txs. This PR pre-emptively removes that cost from the benchmarked section of code.
2 parents 19b2ba8 + 671001c commit c36cd64

File tree

2 files changed

+27
-23
lines changed
  • ouroboros-consensus

2 files changed

+27
-23
lines changed

ouroboros-consensus/bench/mempool-bench/Main.hs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,35 @@ main = withStdTerminalHandles $ do
5454
where
5555
benchAddNTxs n =
5656
withResource
57-
(pure $!! mkNTryAddTxs n)
57+
(pure $!!
58+
let cmds = mkNTryAddTxs n
59+
sz = sum $ map TestBlock.txSize $ getCmdsTxs cmds
60+
in (cmds, Mempool.ByteSize sz)
61+
)
5862
(\_ -> pure ())
59-
(\getTxs -> do
63+
(\getCmds -> do
6064
bgroup (show n <> " transactions") [
6165
bench "setup mempool" $ whnfIO $ do
62-
txs <- getTxs
63-
openMempoolWithCapacityFor txs
66+
(_cmds, capacity) <- getCmds
67+
openMempoolWithCapacity capacity
6468
, bench "setup mempool + benchmark" $ whnfIO $ do
65-
txs <- getTxs
66-
mempool <- openMempoolWithCapacityFor txs
67-
run mempool txs
69+
(cmds, capacity) <- getCmds
70+
mempool <- openMempoolWithCapacity capacity
71+
run mempool cmds
6872
, testCase "test" $ do
69-
txs <- getTxs
70-
mempool <- openMempoolWithCapacityFor txs
71-
testAddTxs mempool txs
72-
, testCase "txs length" $ do
73-
txs <- getTxs
74-
length txs @?= n
73+
(cmds, capacity) <- getCmds
74+
mempool <- openMempoolWithCapacity capacity
75+
testAddCmds mempool cmds
76+
, testCase "cmds length" $ do
77+
(cmds, _capacity) <- getCmds
78+
length cmds @?= n
7579
]
7680
)
7781
where
78-
testAddTxs mempool txs = do
79-
run mempool txs
82+
testAddCmds mempool cmds = do
83+
run mempool cmds
8084
mempoolTxs <- Mocked.getTxs mempool
81-
mempoolTxs @?= getCmdsTxs txs
85+
mempoolTxs @?= getCmdsTxs cmds
8286

8387
parseBenchmarkResults csvFilePath = do
8488
csvData <- BL.readFile csvFilePath
@@ -129,18 +133,17 @@ main = withStdTerminalHandles $ do
129133
Adding TestBlock transactions to a mempool
130134
-------------------------------------------------------------------------------}
131135

132-
openMempoolWithCapacityFor :: [MempoolCmd TestBlock] -> IO (MockedMempool IO TestBlock)
133-
openMempoolWithCapacityFor cmds =
134-
Mocked.openMockedMempool capacityRequiredByCmds
136+
openMempoolWithCapacity :: Mempool.ByteSize -> IO (MockedMempool IO TestBlock)
137+
openMempoolWithCapacity capacity =
138+
Mocked.openMockedMempool (Mempool.mkCapacityBytesOverride
139+
(Mempool.unByteSize capacity)
140+
)
135141
Tracer.nullTracer
136142
TestBlock.txSize
137143
Mocked.MempoolAndModelParams {
138144
Mocked.immpInitialState = TestBlock.initialLedgerState
139145
, Mocked.immpLedgerConfig = TestBlock.sampleLedgerConfig
140146
}
141-
where
142-
capacityRequiredByCmds = Mempool.mkCapacityBytesOverride totalTxsSize
143-
where totalTxsSize = sum $ fmap TestBlock.txSize $ getCmdsTxs cmds
144147

145148
mkNTryAddTxs :: Int -> [MempoolCmd TestBlock.TestBlock]
146149
mkNTryAddTxs 0 = []

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Ouroboros.Consensus.Mempool.Capacity (
2323
, TxLimits (..)
2424
) where
2525

26+
import Cardano.Prelude (NFData)
2627
import Data.Measure (BoundedMeasure, Measure)
2728
import Data.Word (Word32)
2829
import NoThunks.Class
@@ -123,5 +124,5 @@ class BoundedMeasure (TxMeasure blk) => TxLimits blk where
123124

124125
newtype ByteSize = ByteSize { unByteSize :: Word32 }
125126
deriving stock (Show)
126-
deriving newtype (Eq, Ord)
127+
deriving newtype (Eq, NFData, Ord)
127128
deriving newtype (BoundedMeasure, Measure)

0 commit comments

Comments
 (0)