Skip to content

Commit

Permalink
Bench: Address decoding
Browse files Browse the repository at this point in the history
The code decode function is `decodeAddrStateAllowLeftoverT` and we bench
this via `decodeAddrEither`.

Run this with `cabal bench addr`.
  • Loading branch information
erikd committed Aug 25, 2023
1 parent 49c0698 commit 6f7e9cd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
47 changes: 47 additions & 0 deletions libs/cardano-ledger-core/bench/Addr.hs
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.Crypto (StandardCrypto)
import Cardano.Ledger.Address (Addr, decodeAddrEither, serialiseAddr)

import Control.Monad (replicateM)

import Criterion (Benchmark, env, bench, 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
23 changes: 18 additions & 5 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,26 @@ benchmark umap

build-depends:
base,
-- aeson >=2,
cardano-ledger-core,
cardano-ledger-core:{testlib},
containers,
criterion,
-- data-default-class,
-- deepseq,
-- microlens,
QuickCheck,
-- random

benchmark addr
type: exitcode-stdio-1.0
main-is: Addr.hs
hs-source-dirs: bench
default-language: Haskell2010
ghc-options:
-Wall -Wcompat -Wincomplete-record-updates
-Wincomplete-uni-patterns -Wredundant-constraints -Wunused-packages
-threaded -rtsopts -O2

build-depends:
base,
bytestring,
cardano-ledger-core,
cardano-ledger-core:{testlib},
criterion,
QuickCheck,

0 comments on commit 6f7e9cd

Please sign in to comment.