Source file micheline_benchmarks.ml
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
open Benchmarks_shell
open Tezos_micheline
let ns = Namespace.make Shell_namespace.ns "micheline"
let fv s = Free_variable.of_namespace (ns s)
type width_function = depth:int -> int Base_samplers.sampler
type node = (int, unit) Micheline.node
type node_kind = Int_node | String_node | Bytes_node | Seq_node | Prim_node
let all_kinds = [|Int_node; String_node; Bytes_node; Seq_node; Prim_node|]
let sample_kind : node_kind Base_samplers.sampler =
fun rng_state ->
let i = Random.State.int rng_state (Array.length all_kinds) in
all_kinds.(i)
let sample_string _ = ""
let sample_bytes _ = Bytes.empty
let sample_z _ = Z.zero
let sample (w : width_function) rng_state =
let rec sample depth rng_state k =
match sample_kind rng_state with
| Int_node -> k (Micheline.Int (0, sample_z rng_state))
| String_node -> k (Micheline.String (0, sample_string rng_state))
| Bytes_node -> k (Micheline.Bytes (0, sample_bytes rng_state))
| Seq_node ->
let width = w ~depth rng_state in
sample_list
depth
width
[]
(fun terms -> k (Micheline.Seq (0, terms)))
rng_state
| Prim_node ->
let width = w ~depth rng_state in
sample_list
depth
width
[]
(fun terms -> k (Micheline.Prim (0, (), terms, [])))
rng_state
and sample_list depth width acc k rng_state =
if width < 0 then invalid_arg "sample_list: negative width"
else if width = 0 then k (List.rev acc)
else
sample (depth + 1) rng_state (fun x ->
sample_list depth (width - 1) (x :: acc) k rng_state)
in
sample 0 rng_state (fun x -> x)
let reasonable_width_function ~depth rng_state =
Base_samplers.(
sample_in_interval
~range:{min = 0; max = 20 / (Bits.numbits depth + 1)}
rng_state)
let sample = sample reasonable_width_function
type size = {nodes : int; bytes : int}
let int z = {nodes = 1; bytes = (Z.numbits z + 7) / 8}
let string s = {nodes = 1; bytes = String.length s}
let bytes b = {nodes = 1; bytes = Bytes.length b}
let node = {nodes = 1; bytes = 0}
let ( @+ ) x y = {nodes = x.nodes + y.nodes; bytes = x.bytes + y.bytes}
let micheline_size (n : node) =
let rec micheline_size n acc =
let open Micheline in
match n with
| Int (_, i) -> acc @+ int i
| String (_, s) -> acc @+ string s
| Bytes (_, b) -> acc @+ bytes b
| Seq (_, terms) ->
List.fold_left
(fun acc term -> micheline_size term acc)
(acc @+ node)
terms
| Prim (_, _, terms, _) ->
List.fold_left
(fun acc term -> micheline_size term acc)
(acc @+ node)
terms
in
micheline_size n {nodes = 0; bytes = 0}
module Micheline_strip_locations : Benchmark.Simple = struct
let name = ns "strip_locations_micheline"
let info = "Benchmarking Micheline.strip_locations"
let module_filename = __FILE__
let purpose = Benchmark.Generate_code "script_repr"
let tags = ["micheline"]
type config = unit
let config_encoding = Data_encoding.unit
let default_config = ()
type workload = size
let workload_encoding =
let open Data_encoding in
conv
(fun {nodes; bytes} -> (nodes, bytes))
(fun (nodes, bytes) -> {nodes; bytes})
(obj2 (req "nodes" int31) (req "bytes" int31))
let workload_to_vector {nodes; bytes} =
Sparse_vec.String.of_list
[("nodes", float_of_int nodes); ("bytes", float_of_int bytes)]
let model =
Model.(
make
~conv:(fun {nodes; bytes = _} -> (nodes, ()))
~model:(linear ~name ~coeff:(fv "nodes")))
let group = Benchmark.Standalone
let create_benchmark ~rng_state () =
let term = sample rng_state in
let size = micheline_size term in
let closure () = ignore (Micheline.strip_locations term) in
Generator.Plain {workload = size; closure}
end
let () = Registration.register_simple (module Micheline_strip_locations)