-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3683 from input-output-hk/erikd/umap-size-bench
Add two new bench mark programs
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,47 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
import Cardano.Ledger.Address (Addr, decodeAddrEither, serialiseAddr) | ||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
|
||
import Control.Monad (replicateM) | ||
|
||
import Criterion (Benchmark, bench, env, nf) | ||
import Criterion.Main (defaultMain) | ||
|
||
import Data.ByteString.Char8 (ByteString) | ||
import Data.Either (lefts) | ||
|
||
import Test.Cardano.Ledger.Core.Arbitrary () | ||
import Test.QuickCheck (arbitrary, generate) | ||
|
||
main :: IO () | ||
main = do | ||
defaultMain $ | ||
map (\c -> env (generateAddrAsBytestring c) decodeAddrBench) (map (* 500) [1 .. 2]) | ||
where | ||
decodeAddrBench :: [ByteString] -> Benchmark | ||
decodeAddrBench xs = | ||
bench ("decodeAddr (" ++ show (length xs) ++ ")") (nf tryDecodeAddr xs) | ||
|
||
-- ------------------------------------------------------------------------------------------------- | ||
|
||
generateAddrAsBytestring :: Int -> IO [ByteString] | ||
generateAddrAsBytestring count = | ||
replicateM count (serialiseAddr <$> genAddr) | ||
where | ||
genAddr :: IO (Addr StandardCrypto) | ||
genAddr = generate arbitrary | ||
|
||
tryDecodeAddr :: [ByteString] -> () | ||
tryDecodeAddr xs = | ||
case lefts $ map decode xs of | ||
[] -> () | ||
ys -> error $ "tryDecodeAddr: " ++ show ys | ||
where | ||
decode :: ByteString -> Either String (Addr StandardCrypto) | ||
decode = decodeAddrEither |
This file contains 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,56 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
import Cardano.Ledger.UMap (UMap) | ||
import qualified Cardano.Ledger.UMap as UMap | ||
|
||
import Control.Monad (replicateM) | ||
|
||
import Criterion (Benchmark, bench, env, nf) | ||
import Criterion.Main (defaultMain) | ||
|
||
import qualified Data.Map.Strict as Map | ||
|
||
import Test.Cardano.Ledger.Core.Arbitrary () | ||
import Test.QuickCheck (arbitrary, generate) | ||
|
||
main :: IO () | ||
main = do | ||
defaultMain $ | ||
map (\c -> env (generateUMap c) umapSizeBench) (map (* 10000) [1 .. 2]) | ||
where | ||
umapSizeBench :: UMap StandardCrypto -> Benchmark | ||
umapSizeBench umap = | ||
bench ("compositeSize (" ++ show (compositeSize umap) ++ ")") (nf compositeSize umap) | ||
|
||
-- ------------------------------------------------------------------------------------------------- | ||
|
||
compositeSize :: UMap StandardCrypto -> Int | ||
compositeSize umap = | ||
sum | ||
[ UMap.size (UMap.RewDepUView umap) | ||
, UMap.size (UMap.PtrUView umap) | ||
, UMap.size (UMap.SPoolUView umap) | ||
, UMap.size (UMap.DRepUView umap) | ||
] | ||
|
||
-- Generate a UView of exactly the size specified. | ||
generateUMap :: Int -> IO (UMap StandardCrypto) | ||
generateUMap size = | ||
generate $ do | ||
ptrs <- replicateM size arbitrary | ||
sPools <- replicateM size arbitrary | ||
dReps <- replicateM size arbitrary | ||
creds <- replicateM size arbitrary | ||
rdPairs <- replicateM size arbitrary | ||
pure $ | ||
UMap.unify | ||
(Map.fromList $ zip creds rdPairs) | ||
(Map.fromList $ zip ptrs creds) | ||
(Map.fromList $ zip creds sPools) | ||
(Map.fromList $ zip creds dReps) |
This file contains 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