<commit_before>open Lwt.Infix
module Store = Irmin_pack.KV (Irmin.Contents.String)

let info () = Irmin.Info.v ~date:0L ~author:"author" "commit message"

let times ~n ~init f =
  let rec go i k =
    if i = 0 then k init else go (i - 1) (fun r -> f i r >>= k)
  in
  go n Lwt.return

let run ~path ~ncommits =
  let config = Irmin_pack.config ~fresh:false (Fpath.to_string path) in
  let tree = Store.Tree.empty in
  Store.Repo.v config >>= Store.master >>= fun t ->
  times ~n:ncommits ~init:tree (fun i tree ->
      Store.Tree.add tree [ string_of_int i ] "contents" >>= fun tree ->
      Store.set_tree_exn t ~info [] tree >>= fun () -> Lwt.return tree )
  >>= fun _ -> Lwt_io.printl "ok"

let main ~ncommits =
  Bos.OS.Dir.with_tmp "irmin%s"
    (fun path () -> Lwt_main.run (run ~path ~ncommits))
    ()
  |> Rresult.R.failwith_error_msg

let () =
  match Sys.argv with
  | [| _ |] -> main ~ncommits:1000
  | [| _; ncommits_str |] ->
      let ncommits = int_of_string ncommits_str in
      main ~ncommits
  | _ -> assert false<commit_msg>irmin-pack: bench_pack show tree counters<commit_after>open Lwt.Infix
module Store = Irmin_pack.KV (Irmin.Contents.String)

let info () = Irmin.Info.v ~date:0L ~author:"author" "commit message"

let times ~n ~init f =
  let rec go i k =
    if i = 0 then k init else go (i - 1) (fun r -> f i r >>= k)
  in
  go n Lwt.return

let run ~path ~ncommits =
  let config = Irmin_pack.config ~fresh:false (Fpath.to_string path) in
  let tree = Store.Tree.empty in
  Store.Repo.v config >>= Store.master >>= fun t ->
  times ~n:ncommits ~init:tree (fun i tree ->
      Store.Tree.add tree [ string_of_int i ] "contents" >>= fun tree ->
      Store.set_tree_exn t ~info [] tree >>= fun () -> Lwt.return tree )
  >>= fun _ -> Lwt_io.printl "ok"

let main ~ncommits =
  Bos.OS.Dir.with_tmp "irmin%s"
    (fun path () -> Lwt_main.run (run ~path ~ncommits))
    ()
  |> Rresult.R.failwith_error_msg

let () =
  match Sys.argv with
  | [| _ |] -> main ~ncommits:1000
  | [| _; ncommits_str |] ->
      let ncommits = int_of_string ncommits_str in
      main ~ncommits
  | _ -> assert false

let () = Fmt.epr "tree counters:\n%a\n%!" Store.Tree.dump_counters ()
{'fraction_non_alphanumeric': 0.1478776814240073, 'fraction_numerical': 0.006389776357827476, 'mean_word_length': 2.9424460431654675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4209457', 'n_tokens_mistral': 869, 'n_tokens_neox': 862, 'n_words': 236}