Commits

Sebastien Mondet committed e023446

test: add module `Benchmark`

Comments (0)

Files changed (1)

test/sosa_test.ml

   @  List.init 20 (fun i -> random_ascii_string (i * 4 + 1))
   @  List.init 20 (fun i -> random_utf8_string (i * 4 + 1))
 
+module Benchmark = struct
+
+  let benchmarks_table = ref []
+
+  let add ~implementation ~experiment ~result =
+    match List.Assoc.find !benchmarks_table implementation with
+    | Some exps ->
+       exps := (experiment, result) :: !exps
+    | None ->
+       benchmarks_table := (implementation, ref [experiment, result]) :: !benchmarks_table
+
+  let to_string () =
+    let experiments =
+      List.map !benchmarks_table (fun (_, l) ->
+          List.map !l (fun (e, _) -> e))
+      |> List.concat |> List.dedup in
+    let first_row =
+      "Implementation" :: experiments
+    in
+    let row_widths =
+      List.map first_row (fun s -> ref (String.length s)) in
+    say "row widths: %s" (String.concat ~sep:", " (List.map row_widths (fun r -> sprintf "%d" !r)));
+    let other_rows =
+      List.map !benchmarks_table (fun (impl, l) ->
+          let w = List.nth_exn row_widths 0 in
+          w := max !w (String.length impl);
+          impl :: List.mapi experiments (fun i exp ->
+              let res = List.Assoc.find_exn !l exp in
+               let w = List.nth_exn row_widths (i  + 1) in
+              say "w: %d, i: %d lgth: %d" !w i (String.length res);
+              w := max !w (String.length res);
+              res)) in
+    let row_to_string row =
+      row
+      |> List.mapi ~f:(fun i c ->
+          say "%d %s %d %d" i c !(List.nth_exn row_widths i) (String.length c);
+          sprintf "%s%s" c
+            (String.make (1 + !(List.nth_exn row_widths i) - String.length c) ' '))
+      |> String.concat ~sep:"| "
+    in
+    sprintf "%s\n%s\n"
+      (first_row |> row_to_string)
+      (other_rows
+       |> List.map ~f:row_to_string
+       |> String.concat ~sep:"\n")
+
+end
+
 let do_basic_test (module Test : TEST_STRING) =
   let open Test in
   say "### Test %S" test_name;
         end)
   end);
   utf8_specific_test ();
+  say "## Benchmarks\n\n%s\n" (Benchmark.to_string ());
   exit !return_code
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.