File: bench.ml

package info (click to toggle)
ocaml-asn1-combinators 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 232 kB
  • sloc: ml: 2,059; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,323 bytes parent folder | download
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
(* Copyright (c) 2014-2017 David Kaloper Meršinjak. All rights reserved.
   See LICENSE.md. *)

let measure f =
  let t1  = Sys.time () in
  let res = f () in
  let t2  = Sys.time () in
  Printf.printf "[time] %.03f s\n%!" (t2 -. t1) ;
  res

let time ?(iter=1) f =
  let rec go = function
    | 1 -> f ()
    | n -> ignore (f ()) ; go (pred n) in
  measure @@ fun () -> go iter

let read filename =
  let fd = Unix.(openfile filename [O_RDONLY] 0) in
  Fun.protect ~finally:(fun () -> Unix.close fd)
    (fun () ->
       let chunk_size = 2048 in
       let rec read acc =
         let buf = Bytes.create chunk_size in
         let r = Unix.read fd buf 0 chunk_size in
         if r = chunk_size then
           read (buf :: acc)
         else
           Bytes.sub buf 0 r :: acc
           |> List.rev
           |> List.map Bytes.unsafe_to_string
           |> String.concat ""
       in
       read [])

let bench_certs filename =
  let cs = read filename in
  let rec bench n cs =
    if String.length cs = 0 then n else
      match Asn.decode X509.cert_ber cs with
      | Ok (_, cs) -> bench (succ n) cs
      | Error e -> invalid_arg (Format.asprintf "%a" Asn.pp_error e) in
  time ~iter:1 @@ fun () ->
    let n = bench 0 cs in
    Printf.printf "parsed %d certs.\n%!" n

let _ = bench_certs "./rondom/certs.bin"