|
| 1 | +module Benchmarks.Values ( |
| 2 | + makeBenchmarks, |
| 3 | +) where |
| 4 | + |
| 5 | +import Prelude |
| 6 | + |
| 7 | +import Common |
| 8 | + |
| 9 | +import PlutusCore (DefaultFun (InsertCoin, LookupCoin, UnValueData, ValueContains, ValueData)) |
| 10 | +import PlutusCore.Value (Value) |
| 11 | +import PlutusCore.Value qualified as Value |
| 12 | + |
| 13 | +import Control.Monad.State (State) |
| 14 | +import Criterion.Main (Benchmark) |
| 15 | +import Data.ByteString (ByteString) |
| 16 | +import System.Random.Stateful (StageGenM, StatefulGen, StdGen, UniformRange (uniformRM), |
| 17 | + runStateGen_, uniformByteStringM) |
| 18 | + |
| 19 | +makeBenchmarks :: StdGen -> [Benchmark] |
| 20 | +makeBenchmarks gen = |
| 21 | + [ benchInsertCoin gen |
| 22 | + -- , benchUnionValue gen |
| 23 | + ] |
| 24 | + |
| 25 | +type PolicyId = ByteString |
| 26 | +type TokenName = ByteString |
| 27 | +type Amount = Integer |
| 28 | + |
| 29 | +-- | An insertCoin benchmark is a concrete set of arguments we apply to the |
| 30 | +-- InsertCoin builtin function to measure its runtime cost. |
| 31 | +data InsertCoinBenchmark = InsertCoinBenchmark |
| 32 | + { icPolicyId :: PolicyId |
| 33 | + , icTokenName :: TokenName |
| 34 | + , icAmount :: Amount |
| 35 | + , icValue :: Value |
| 36 | + } |
| 37 | + |
| 38 | +icToTuple :: InsertCoinBenchmark -> (PolicyId, TokenName, Amount, Value) |
| 39 | +icToTuple (InsertCoinBenchmark p t a v) = (p, t, a, v) |
| 40 | + |
| 41 | +benchInsertCoin :: StdGen -> Benchmark |
| 42 | +benchInsertCoin gen = |
| 43 | + createFourTermBuiltinBenchElementwiseWithWrappers |
| 44 | + (id, id, id, id) -- TODO: use proper wrappers |
| 45 | + InsertCoin |
| 46 | + [] |
| 47 | + (icToTuple <$> insertCoinBenchGen gen) |
| 48 | + |
| 49 | +-- | Generate a set of benchmarks for the InsertCoin builtin function. |
| 50 | +-- It includes the following scenarios: |
| 51 | +-- 1. Inserting into an empty Value. |
| 52 | +-- 2. Inserting a new TokenName into an existing PolicyId. |
| 53 | +-- 3. Inserting into an existing TokenName. |
| 54 | +-- 4. Inserting a new PolicyId. |
| 55 | +-- 5. Deleting a TokenName by inserting a 0 amount. |
| 56 | +-- 6. Deleting a PolicyId by inserting a 0 amount into its last TokenName. |
| 57 | +-- We're interested in the worst case performance, so we'll use the largest key values possible. |
| 58 | +insertCoinBenchGen |
| 59 | + :: StdGen |
| 60 | + -> [InsertCoinBenchmark] |
| 61 | +insertCoinBenchGen g = undefined |
| 62 | + |
| 63 | +uniformByteString :: StdGen -> ByteString |
| 64 | +uniformByteString gen = uniformByteString Value.maxKeyLen gen |
0 commit comments