forked from Liqwid-Labs/agora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBench.hs
77 lines (67 loc) · 2.35 KB
/
Bench.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-# LANGUAGE RecordWildCards #-}
module Bench (Benchmark (..), benchmarkScript, specificationTreeToBenchmarks) where
import Codec.Serialise (serialise)
import Data.ByteString.Lazy qualified as LBS
import Data.ByteString.Short qualified as SBS
import Data.Csv (DefaultOrdered, ToNamedRecord, header, headerOrder, namedRecord, toNamedRecord, (.=))
import Data.List (intercalate)
import Data.Text (Text, pack)
import Plutarch.Evaluate (evalScript)
import PlutusLedgerApi.V2 (
ExBudget (ExBudget),
ExCPU (..),
ExMemory (..),
Script,
)
import Prettyprinter (Pretty (pretty), indent, vsep)
import Test.Specification (
Specification (Specification),
SpecificationExpectation (Success),
SpecificationTree (..),
)
--------------------------------------------------------------------------------
-- | Represents the benchmark of a plutus script.
data Benchmark = Benchmark
{ name :: Text
-- ^ Human readable name describing script.
, cpuBudget :: ExCPU
-- ^ The on-chain execution cost of a script.
, memoryBudget :: ExMemory
-- ^ The on-chain memory budget of a script.
, scriptSize :: Int
-- ^ The on-chain size of a script.
}
deriving stock (Show, Eq, Ord, Generic)
instance Pretty Benchmark where
pretty (Benchmark name (ExCPU (toInteger -> cpu)) (ExMemory (toInteger -> mem)) size) =
vsep
[ pretty name
, indent 4 $
vsep
[ "CPU: " <> pretty cpu
, "MEM: " <> pretty mem
, "SIZE: " <> pretty size
]
]
instance ToNamedRecord Benchmark where
toNamedRecord (Benchmark {..}) =
namedRecord
[ "name" .= name
, "cpu" .= cpuBudget
, "mem" .= memoryBudget
, "size" .= scriptSize
]
instance DefaultOrdered Benchmark where
headerOrder _ = header ["name", "cpu", "mem", "size"]
benchmarkScript :: String -> Script -> Benchmark
benchmarkScript name script = Benchmark (pack name) cpu mem size
where
(_res, ExBudget cpu mem, _traces) = evalScript script
size = SBS.length . SBS.toShort . LBS.toStrict . serialise $ script
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