summaryrefslogtreecommitdiff
path: root/ocaml/test/old/cache_solid.ml
blob: 7ad7df0ad47add1639a8a0dda0adfac2e088177d (plain)
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
(* Cache Solid Pill - Cue once and save marshalled OCaml noun
 *
 * This cues the solid pill once (slow) and saves the resulting
 * noun using OCaml's Marshal for fast loading later
 *)

open Nock_lib

let cache_solid env =
  Printf.printf "Caching solid pill...\n\n";

  Eio.Switch.run @@ fun _sw ->
  let fs = Eio.Stdenv.fs env in

  (* Load and cue solid pill *)
  Printf.printf "Step 1: Loading solid.pill (8.7 MB)...\n";
  let file_path = Eio.Path.(fs / "solid.pill") in
  let pill_bytes = Eio.Path.load file_path |> Bytes.of_string in
  Printf.printf "  Loaded %d bytes\n\n" (Bytes.length pill_bytes);

  Printf.printf "Step 2: Cuing (this will take several minutes)...\n";
  let start = Unix.gettimeofday () in
  let last_tick = ref start in
  let last_nouns = ref 0 in
  let last_depth = ref 0 in
  let last_peak = ref 0 in
  let bytes_seen = ref 0 in

  let total_bytes = Bytes.length pill_bytes in

  let progress ~nouns ~bits ~depth ~max_depth =
    last_nouns := nouns;
    last_depth := depth;
    last_peak := max_depth;
    bytes_seen := bits / 8;

    let now = Unix.gettimeofday () in
    if now -. !last_tick >= 1.0 then begin
      let mb = float_of_int !bytes_seen /. (1024. *. 1024.) in
      let pct = (float_of_int !bytes_seen /. float_of_int total_bytes) *. 100. in
      Printf.printf "  %.1fs | %.2f MiB (%.1f%%) | nouns=%d depth=%d/%d\n%!"
        (now -. start) mb pct !last_nouns !last_depth !last_peak;
      last_tick := now
    end
  in

  let pill =
    Serial.cue
      ~progress
      ~progress_interval:50_000
      pill_bytes
  in

  let elapsed = Unix.gettimeofday () -. start in
  Printf.printf
    "  ✓ Cued in %.1fs (%.2f MiB, nouns=%d, max depth=%d)\n\n"
    elapsed
    (float_of_int total_bytes /. (1024. *. 1024.))
    !last_nouns
    !last_peak;

  Printf.printf "Step 3: Marshalling noun to solid.noun...\n";
  let out_channel = open_out_bin "solid.noun" in
  Marshal.to_channel out_channel pill [];
  close_out out_channel;
  Printf.printf "  ✓ Saved to solid.noun\n\n";

  Printf.printf "Step 4: Testing reload speed...\n";
  let start = Unix.gettimeofday () in
  let in_channel = open_in_bin "solid.noun" in
  let _reloaded = (Marshal.from_channel in_channel : Noun.noun) in
  close_in in_channel;
  let elapsed = Unix.gettimeofday () -. start in
  Printf.printf "  ✓ Reloaded in %.4fs (much faster!)\n\n" elapsed;

  Printf.printf "Done! Use solid.noun for fast testing.\n"

let () = Eio_main.run cache_solid