package octez-shell-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file encoding_benchmarks_helpers.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020 Nomadic Labs. <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

let ns = Namespace.make Shell_namespace.ns "encoding"

let fv s = Free_variable.of_namespace (ns s)

(* Helpers for encodings of fixed-size values (hence constant-time) *)
module Shared_constant_time = struct
  type config = unit

  let config_encoding = Data_encoding.unit

  let default_config = ()

  type workload = unit

  let workload_encoding = Data_encoding.unit

  let workload_to_vector () = Sparse_vec.String.of_list [("encoding", 1.)]
end

module Shared_linear = struct
  type config = unit

  let config_encoding = Data_encoding.unit

  let default_config = ()

  type workload = {bytes : int}

  let workload_encoding =
    let open Data_encoding in
    conv (fun {bytes} -> bytes) (fun bytes -> {bytes}) int31

  let workload_to_vector {bytes} =
    Sparse_vec.String.of_list [("bytes", float_of_int bytes)]
end

module Make (Info : sig
  val file : string

  val purpose : Benchmark.purpose
end) =
struct
  (* Generic function to cook benchmarks for fixed-size encodings *)
  let fixed_size_shared :
      ?check:(unit -> unit) ->
      name:string ->
      generator:'a Base_samplers.sampler ->
      make_bench:
        ((unit -> 'a) -> unit -> unit Tezos_benchmark.Generator.benchmark) ->
      unit ->
      Benchmark.simple_with_num =
   fun ?(check = fun () -> ()) ~name ~generator ~make_bench () ->
    let free_variable = fv (Format.asprintf "%s_const" name) in
    let model =
      Model.make
        ~conv:(fun () -> ())
        (Model.unknown_const1 ~name:(ns name) ~const:free_variable)
    in
    let module Bench : Benchmark.Simple_with_num = struct
      let name = ns name

      let info = Format.asprintf "Benchmarking %a" Namespace.pp name

      let module_filename = Info.file

      let purpose = Info.purpose

      let tags = ["encoding"]

      include Shared_constant_time

      let create_benchmarks ~rng_state ~bench_num () =
        check () ;
        let generator () = generator rng_state in
        List.repeat bench_num (make_bench generator)

      let model = model

      let group = Benchmark.Group "encoding"
    end in
    (module Bench : Benchmark.Simple_with_num)

  (* Generic function to cook benchmarks for linear-time encodings *)
  (* Setting [purpose] will overwrite the definition from [Info]. *)
  let linear_shared ?(check = fun () -> ()) ?purpose ~name ?(intercept = false)
      ~generator ~make_bench () =
    let const = fv (Format.asprintf "%s_const" name) in
    let coeff = fv (Format.asprintf "%s_coeff" name) in
    let model =
      Model.make
        ~conv:(fun {Shared_linear.bytes} -> (bytes, ()))
        (Model.affine ~name:(ns name) ~intercept:const ~coeff)
    in
    let module Bench : Benchmark.Simple_with_num = struct
      let name =
        if intercept then Namespace.make ns name "intercept" else ns name

      let info =
        Format.asprintf
          "Benchmarking %a%s"
          Namespace.pp
          name
          (if intercept then " (intercept case)" else "")

      let module_filename = Info.file

      let purpose = Option.value purpose ~default:Info.purpose

      let tags = ["encoding"]

      include Shared_linear

      let create_benchmarks ~rng_state ~bench_num () =
        check () ;
        let generator () = generator rng_state in
        List.repeat bench_num (make_bench generator)

      let model = model

      let group = Benchmark.Group "encoding"
    end in
    (module Bench : Benchmark.Simple_with_num)

  (* Generic function to cook benchmarks for nlogn-time encodings *)
  let nsqrtn_shared_with_intercept ~name ~generator ~make_bench
      ~generator_intercept ~make_bench_intercept =
    let const = fv (Format.asprintf "%s_const" name) in
    let coeff = fv (Format.asprintf "%s_coeff" name) in
    let model =
      Model.make
        ~conv:(fun {Shared_linear.bytes} -> (bytes, ()))
        (Model.nsqrtn_const ~name:(ns name) ~intercept:const ~coeff)
    in
    let module Bench : Benchmark.Simple = struct
      let name = ns name

      let info = Format.asprintf "Benchmarking %a" Namespace.pp name

      let module_filename = Info.file

      let purpose = Info.purpose

      let tags = ["encoding"]

      include Shared_linear

      let create_benchmark ~rng_state () =
        let generator () = generator rng_state in
        make_bench generator

      let model = model

      let group = Benchmark.Group "encoding"
    end in
    let module Bench_intercept : Benchmark.Simple = struct
      let name = (Namespace.make ns name) "intercept"

      let info =
        Format.asprintf "Benchmarking %a (intercept case)" Namespace.pp name

      let module_filename = Info.file

      let purpose = Info.purpose

      let tags = ["encoding"]

      include Shared_linear

      let create_benchmark ~rng_state () =
        let generator () = generator_intercept rng_state in
        make_bench_intercept generator

      let model = model

      let group = Benchmark.Group "encoding"
    end in
    ( (module Bench : Benchmark.Simple),
      (module Bench_intercept : Benchmark.Simple) )

  let make_encode_fixed_size :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      encoding:a Data_encoding.t ->
      generator:(Random.State.t -> a) ->
      unit ->
      Benchmark.simple_with_num =
   fun ?check ~name ~encoding ~generator () ->
    fixed_size_shared
      ?check
      ~name
      ~generator
      ~make_bench:(fun generator () ->
        let generated = generator () in
        let closure () =
          ignore (Data_encoding.Binary.to_bytes_exn encoding generated)
        in
        Generator.Plain {workload = (); closure})
      ()

  let make_encode_variable_size :
      type a.
      ?check:(unit -> unit) ->
      ?purpose:Tezos_benchmark.Benchmark.purpose ->
      name:string ->
      encoding:a Data_encoding.t ->
      generator:(Random.State.t -> a * Shared_linear.workload) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ?purpose ~name ~encoding ~generator () ->
    linear_shared
      ?check
      ?purpose
      ~name
      ~generator
      ~make_bench:(fun generator () ->
        let generated, workload = generator () in
        let closure () =
          ignore (Data_encoding.Binary.to_bytes_exn encoding generated)
        in
        Generator.Plain {workload; closure})
      ()

  let make_decode_fixed_size :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      encoding:a Data_encoding.t ->
      generator:(Random.State.t -> a) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~encoding ~generator ->
    fixed_size_shared ?check ~name ~generator ~make_bench:(fun generator () ->
        let generated = generator () in
        let encoded = Data_encoding.Binary.to_bytes_exn encoding generated in
        let closure () =
          ignore (Data_encoding.Binary.of_bytes_exn encoding encoded)
        in
        Generator.Plain {workload = (); closure})

  let make_decode_variable_size :
      type a.
      ?check:(unit -> unit) ->
      ?purpose:Tezos_benchmark.Benchmark.purpose ->
      name:string ->
      encoding:a Data_encoding.t ->
      ?intercept:bool ->
      generator:(Random.State.t -> a * Shared_linear.workload) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ?purpose ~name ~encoding ?(intercept = false) ~generator ->
    linear_shared
      ?check
      ?purpose
      ~name
      ~intercept
      ~generator
      ~make_bench:(fun generator () ->
        let generated, workload = generator () in
        let encoded = Data_encoding.Binary.to_bytes_exn encoding generated in
        let closure () =
          ignore (Data_encoding.Binary.of_bytes_exn encoding encoded)
        in
        Generator.Plain {workload; closure})

  (* Generic functions to cook benchmarks for b58check conversions (used for
     typechecking in readable mode in the protocol...) and byte conversions. *)
  let make_encode_fixed_size_to_string :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_string:(a -> string) ->
      generator:(Random.State.t -> a) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_string ~generator ->
    fixed_size_shared ?check ~name ~generator ~make_bench:(fun generator () ->
        let generated = generator () in
        let closure () = ignore (to_string generated) in
        Generator.Plain {workload = (); closure})

  (* Exactly the sample implem' as above.*)
  let make_encode_fixed_size_to_bytes :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_bytes:(a -> bytes) ->
      generator:(Random.State.t -> a) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_bytes ~generator ->
    fixed_size_shared ?check ~name ~generator ~make_bench:(fun generator () ->
        let generated = generator () in
        let closure () = ignore (to_bytes generated) in
        Generator.Plain {workload = (); closure})

  let make_encode_variable_size_to_string :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_string:(a -> string) ->
      generator:(Random.State.t -> a * Shared_linear.workload) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_string ~generator () ->
    linear_shared
      ?check
      ~name
      ~generator
      ~make_bench:(fun generator () ->
        let generated, workload = generator () in
        let closure () = ignore (to_string generated) in
        Generator.Plain {workload; closure})
      ()

  let make_decode_fixed_size_from_string :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_string:(a -> string) ->
      from_string:(string -> a) ->
      generator:(Random.State.t -> a) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_string ~from_string ~generator ->
    fixed_size_shared ?check ~name ~generator ~make_bench:(fun generator () ->
        let generated = generator () in
        let string = to_string generated in
        let closure () = ignore (from_string string) in
        Generator.Plain {workload = (); closure})

  let make_decode_fixed_size_from_bytes :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_bytes:(a -> bytes) ->
      from_bytes:(bytes -> a) ->
      generator:(Random.State.t -> a) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_bytes ~from_bytes ~generator ->
    fixed_size_shared ?check ~name ~generator ~make_bench:(fun generator () ->
        let generated = generator () in
        let bytes = to_bytes generated in
        let closure () = ignore (from_bytes bytes) in
        Generator.Plain {workload = (); closure})

  let make_decode_variable_size_from_string :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_string:(a -> string) ->
      from_string:(string -> a) ->
      generator:(Random.State.t -> a * Shared_linear.workload) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_string ~from_string ~generator () ->
    linear_shared
      ?check
      ~name
      ~generator
      ~make_bench:(fun generator () ->
        let generated, workload = generator () in
        let string = to_string generated in
        let closure () = ignore (from_string string) in
        Generator.Plain {workload; closure})
      ()

  let make_decode_variable_size_from_bytes :
      type a.
      ?check:(unit -> unit) ->
      name:string ->
      to_bytes:(a -> bytes) ->
      from_bytes:(bytes -> a) ->
      generator:(Random.State.t -> a * Shared_linear.workload) ->
      unit ->
      (module Benchmark.Simple_with_num) =
   fun ?check ~name ~to_bytes ~from_bytes ~generator () ->
    linear_shared
      ?check
      ~name
      ~generator
      ~make_bench:(fun generator () ->
        let generated, workload = generator () in
        let string = to_bytes generated in
        let closure () = ignore (from_bytes string) in
        Generator.Plain {workload; closure})
      ()
end
OCaml

Innovation. Community. Security.