-
Notifications
You must be signed in to change notification settings - Fork 33
Peras chain weight microbenchmark #1583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,32 @@ | ||
| # Consensus benchmarks | ||
|
|
||
| We are in the process of adding component level microbenchmarks for Consensus. | ||
|
|
||
| We check for regressions in performance on CI. | ||
|
|
||
| ## Mempool Benchmark | ||
|
|
||
| We started with microbenchmarks for adding transactions to the mempool. The | ||
| mempool benchmarks can be run using the following command. | ||
|
|
||
| ```sh | ||
| cabal new-run ouroboros-consensus:mempool-bench | ||
| ``` | ||
|
|
||
| We check for regressions in performance on CI. We might publish benchmark results in this site shortly. | ||
| ## ChainSync Client Benchmark | ||
|
|
||
| To aid the refactoring of the ChainSync client, we added a benchmark for it in [PR#823](https://github.com/IntersectMBO/ouroboros-consensus/pull/823). The benchmark could be invoked as follows: | ||
|
|
||
| ```sh | ||
| cabal new-run ouroboros-consensus:ChainSync-client-bench -- 10 10 | ||
| ``` | ||
|
|
||
| ## PerasCertDB Benchmark | ||
|
|
||
| We have a microbenchmark for the boosted chain fragment weight calculation, which could be run as follows: | ||
|
|
||
| ```sh | ||
| cabal run ouroboros-consensus:PerasCertDB-bench -- +RTS -T -A32m -RTS | ||
| ``` | ||
|
|
||
| We request GHC runtime system statistics with `-T` to get a memory usage estimate, and also request a large nursery with `-A32m` to minimise garbage collection. See `tasty-bench` [documentation](https://github.com/Bodigrim/tasty-bench?tab=readme-ov-file#troubleshooting) for more tips. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| {-# LANGUAGE ImportQualifiedPost #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
|
|
||
| -- | This module contains benchmarks for Peras chain weight calculation as implemented by | ||
| -- the by the 'Ouroboros.Consensus.Storage.PerasCertDB.API.boostedWeightForFragment' | ||
| -- function. | ||
| -- | ||
| -- We benchmark the calculation on a static sequence of chain fragments of increasing | ||
| -- length, ranging from 0 to around 8640, with a sampling rate of 100. The chain fragments | ||
| -- are instantiated with 'TestBlock', and every 5 blocks there is a booster block with | ||
| -- weight 15. All parameters are set in 'benchmarkParams'. | ||
| module Main (main) where | ||
|
|
||
| import Data.List (iterate') | ||
| import Data.Map.Strict qualified as Map | ||
| import Numeric.Natural (Natural) | ||
| import Ouroboros.Consensus.Block (PerasWeight (PerasWeight), SlotNo (..)) | ||
| import Ouroboros.Consensus.Storage.PerasCertDB.API | ||
| ( PerasWeightSnapshot (..) | ||
| , boostedWeightForFragment | ||
| ) | ||
| import Ouroboros.Network.AnchoredFragment qualified as AF | ||
| import Test.Ouroboros.Storage.TestBlock (TestBlock (..), TestBody (..), TestHeader (..)) | ||
| import Test.Ouroboros.Storage.TestBlock qualified as TestBlock | ||
| import Test.Tasty.Bench | ||
|
|
||
| data BenchmarkParams = BenchmarkParams | ||
| { blockRate :: SlotNo | ||
| -- ^ How often the fragments will contain blocks, in slots | ||
| , fragmentLenghtSamplingRate :: Natural | ||
| -- ^ The rate of length increase for generate chain fragments | ||
| , fragmentMaxLenght :: Natural | ||
| -- ^ the maximum length of a fragment | ||
| , boostedBlockRate :: Natural | ||
| -- ^ How often boosted blocks occur, in blocks | ||
| , boostWeight :: PerasWeight | ||
| -- ^ The weight of the boost | ||
| } | ||
|
|
||
| benchmarkParams :: BenchmarkParams | ||
| benchmarkParams = | ||
| BenchmarkParams | ||
| { blockRate = 20 | ||
| , fragmentLenghtSamplingRate = 100 | ||
| , fragmentMaxLenght = 2160 + 3 * 2160 | ||
| , boostedBlockRate = 5 | ||
| , boostWeight = PerasWeight 15 | ||
| } | ||
|
|
||
| main :: IO () | ||
| main = | ||
| Test.Tasty.Bench.defaultMain $ map benchBoostedWeightForFragment inputs | ||
| where | ||
| -- NOTE: we do not use the 'env' combinator to set up the test data since | ||
| -- it requires 'NFData' for 'AF.AnchoredFragment'. While the necessary | ||
| -- instances could be provided, we do not think is necessary for this | ||
| -- benchmark, as the input data is rather small. | ||
| inputs :: [(Natural, (PerasWeightSnapshot TestBlock, AF.AnchoredFragment TestBlock))] | ||
| inputs = | ||
| getEveryN (fragmentLenghtSamplingRate benchmarkParams) $ | ||
| take (fromIntegral $ fragmentMaxLenght benchmarkParams) $ | ||
| zip [0 ..] $ | ||
| zip (map uniformWeightSnapshot fragments) fragments | ||
|
|
||
| benchBoostedWeightForFragment :: | ||
| (Natural, (PerasWeightSnapshot TestBlock, AF.AnchoredFragment TestBlock)) -> Benchmark | ||
| benchBoostedWeightForFragment (i, (weightSnapshot, fragment)) = | ||
| bench ("boostedWeightForFragment of length " <> show i) $ | ||
| whnf (boostedWeightForFragment weightSnapshot) fragment | ||
|
|
||
| -- | An infinite list of chain fragments | ||
| fragments :: [AF.AnchoredFragment TestBlock] | ||
| fragments = iterate' addSuccessorBlock genesisFragment | ||
| where | ||
| genesisFragment :: AF.AnchoredFragment TestBlock | ||
| genesisFragment = AF.Empty AF.AnchorGenesis | ||
|
|
||
| addSuccessorBlock :: AF.AnchoredFragment TestBlock -> AF.AnchoredFragment TestBlock | ||
| addSuccessorBlock = \case | ||
| AF.Empty _ -> (AF.Empty AF.AnchorGenesis) AF.:> (TestBlock.firstBlock 0 dummyBody) | ||
| (xs AF.:> x) -> | ||
| let nextBlockSlot = blockRate benchmarkParams + (thSlotNo . testHeader $ x) | ||
| in (xs AF.:> x) AF.:> TestBlock.mkNextBlock x nextBlockSlot dummyBody | ||
|
|
||
| dummyBody :: TestBody | ||
| dummyBody = TestBody{tbForkNo = 0, tbIsValid = True} | ||
|
|
||
| -- | Given a chain fragment, construct a weight snapshot where there's a boosted block every 90 slots | ||
| uniformWeightSnapshot :: AF.AnchoredFragment TestBlock -> PerasWeightSnapshot TestBlock | ||
| uniformWeightSnapshot fragment = | ||
| let pointsToBoost = | ||
| map snd | ||
| . getEveryN (boostedBlockRate benchmarkParams) | ||
| . zip [0 ..] | ||
| . map AF.blockPoint | ||
| . AF.toOldestFirst | ||
| $ fragment | ||
| weights = repeat (boostWeight benchmarkParams) | ||
| in PerasWeightSnapshot{getPerasWeightSnapshot = Map.fromList $ zip pointsToBoost weights} | ||
|
|
||
| getEveryN :: Natural -> [(Natural, a)] -> [(Natural, a)] | ||
| getEveryN n = filter (\(i, _) -> (i `mod` n) == 0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not to be too annoying leaving a review comment after the PR's been merged, but would you be able to fix the typo'd
lengthin bothfragmentLenghtSamplingRateandfragmentMaxLenght?