benchmarking

This commit is contained in:
Seungheon Oh 2022-05-24 15:20:09 -05:00
parent eda12644e5
commit 5c438f0062
No known key found for this signature in database
GPG key ID: 9B0E12D357369B66
3 changed files with 70 additions and 51 deletions

View file

@ -1,12 +1,24 @@
module Bench (Benchmark (..), benchmarkSize) where module Bench (Benchmark (..), benchmarkScript, specificationTreeToBenchmarks) where
import Codec.Serialise (serialise) import Codec.Serialise (serialise)
import Data.ByteString.Lazy qualified as LBS import Data.ByteString.Lazy qualified as LBS
import Data.ByteString.Short qualified as SBS import Data.ByteString.Short qualified as SBS
import Data.Set (Set) import Data.List (intercalate)
import Data.Set qualified as Set import Data.Maybe (fromJust)
import Data.Text (Text) import Data.Text (Text, pack)
import Plutus.V1.Ledger.Scripts qualified as Plutus import Plutus.V1.Ledger.Api (
ExBudget (ExBudget),
ExCPU,
ExMemory,
Script,
)
import Plutus.V1.Ledger.Api qualified as Plutus
import Spec.Spec (
Specification (Specification),
SpecificationExpectation (Success),
SpecificationTree (..),
)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -14,20 +26,35 @@ import Plutus.V1.Ledger.Scripts qualified as Plutus
data Benchmark = Benchmark data Benchmark = Benchmark
{ name :: Text { name :: Text
-- ^ Human readable name describing script. -- ^ Human readable name describing script.
, size :: Int , bCPUBudget :: ExCPU
-- ^ The on-chain execution cost of a script.
, bMemoryBudget :: ExMemory
-- ^ The on-chain memory budget of a script.
, bScriptSize :: Int
-- ^ The on-chain size of a script. -- ^ The on-chain size of a script.
} }
deriving stock (Show, Eq, Ord) deriving stock (Show, Eq, Ord)
-- | Create a benchmark containing only the size of the script. benchmarkScript :: String -> Script -> Benchmark
benchmarkSize :: Text -> Plutus.Script -> Set Benchmark benchmarkScript name script = Benchmark (pack name) cpu mem size
benchmarkSize name script = where
Set.singleton $ (ExBudget cpu mem) = evalScriptCounting . serialiseScriptShort $ script
Benchmark size = SBS.length . SBS.toShort . LBS.toStrict . serialise $ script
{ name = name
, size = scriptSize script
}
-- | Compute the size of a script on-chain. serialiseScriptShort :: Script -> SBS.ShortByteString
scriptSize :: Plutus.Script -> Int serialiseScriptShort = SBS.toShort . LBS.toStrict . serialise -- Using `flat` here breaks `evalScriptCounting`
scriptSize = SBS.length . SBS.toShort . LBS.toStrict . serialise evalScriptCounting :: Plutus.SerializedScript -> Plutus.ExBudget
evalScriptCounting script =
let costModel = fromJust Plutus.defaultCostModelParams
(_logout, e) = Plutus.evaluateScriptCounting Plutus.Verbose costModel script []
in case e of
Left evalError -> error ("Eval Error: " <> show evalError)
Right exbudget -> exbudget
specificationTreeToBenchmarks :: SpecificationTree -> [Benchmark]
specificationTreeToBenchmarks = go []
where
go names (Terminal ((Specification n ex s))) = case ex of
Success -> [benchmarkScript (intercalate "/" (names <> [n])) s]
_ -> []
go names (Group gn tree) = mconcat $ go (names <> [gn]) <$> tree

View file

@ -1,42 +1,33 @@
module Main (main) where module Main (main) where
import Agora.AuthorityToken (authorityTokenPolicy) import Bench (specificationTreeToBenchmarks)
import Agora.Effect.TreasuryWithdrawal (treasuryWithdrawalValidator) import Spec.AuthorityToken qualified as AuthorityToken
import Agora.Governor (Governor (..)) import Spec.Effect.GovernorMutation qualified as GovernorMutation
import Agora.Governor.Scripts (governorPolicy, governorValidator) import Spec.Effect.TreasuryWithdrawal qualified as TreasuryWithdrawal
import Agora.Proposal.Scripts (proposalPolicy, proposalValidator) import Spec.Governor qualified as Governor
import Agora.Stake.Scripts (stakePolicy, stakeValidator) import Spec.Proposal qualified as Proposal
import Agora.Treasury (treasuryValidator) import Spec.Spec (group)
import Bench import Spec.Stake qualified as Stake
import Data.Foldable (for_) import Spec.Treasury qualified as Treasury
import Plutus.V1.Ledger.Api (CurrencySymbol)
import Sample.Shared
import Prelude import Prelude
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
main :: IO () main :: IO ()
main = do main = do
let benchmarks = mapM_ print $
mconcat specificationTreeToBenchmarks $
[ -- GATs group
benchmarkSize "authorityTokenPolicy" $ compile $ authorityTokenPolicy authorityToken "Benchmark"
, -- Governor [ group
benchmarkSize "governorValidator" $ compile $ governorValidator governor "Effects"
, benchmarkSize "governorPolicy" $ compile $ governorPolicy governor [ group "Treasury Withdrawal Effect" TreasuryWithdrawal.specs
, -- Stake , group "Governor Mutation Effect" GovernorMutation.specs
benchmarkSize "stakeValidator" $ compile $ stakeValidator stake ]
, benchmarkSize "stakePolicy" $ compile $ stakePolicy governor.gtClassRef , group "Stake" Stake.specs
, -- Proposal , group "Proposal" Proposal.specs
benchmarkSize "proposalValidator" $ compile $ proposalValidator proposal , group "AuthorityToken" AuthorityToken.specs
, benchmarkSize "proposalPolicy" $ compile $ proposalPolicy govAssetClass , group "Treasury" Treasury.specs
, -- Treasury , group "AuthorityToken" AuthorityToken.specs
benchmarkSize "treasuryValidator" $ compile $ treasuryValidator gatCS , group "Governor" Governor.specs
, -- Effect validators ]
benchmarkSize "treasuryWithdrawalValidator" $ compile $ treasuryWithdrawalValidator gatCS
]
for_ benchmarks print
gatCS :: CurrencySymbol
gatCS = "73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049" -- arbitrary CS

View file

@ -217,6 +217,7 @@ benchmark agora-bench
build-depends: build-depends:
, agora , agora
, agora-sample , agora-sample
, agora-spec
executable agora-scripts executable agora-scripts
import: lang, deps, exe-opts import: lang, deps, exe-opts