#!/usr/bin/env bash
# Synthetic patch tree: 20,000 files, 2 GiB total (100 KiB each, random bytes), in 200 dirs.
# Times OPG's V1 hash (find | sha256sum), its V2 hash (Python, two inventories + per-file
# stat checks), and OPG's own read-only benchmark tool. Three runs each.
set -u
T=/tmp/bench/39472050; mkdir -p "$T"
python3 - "$T" <<'PY'
import os, sys
root = sys.argv[1]
for d in range(200):
    p = os.path.join(root, "files", "d%03d" % d); os.makedirs(p, exist_ok=True)
    for f in range(100):
        with open(os.path.join(p, "f%03d.bin" % f), "wb") as fh: fh.write(os.urandom(100 * 1024))
PY
echo "tree: $(find $T -type f | wc -l) files, $(du -sh $T | cut -f1)"
source /opt/opg/project/lib/opg_core.sh; opg_log() { :; }
for i in 1 2 3; do
  s=$(date +%s%N); h1=$(INTEGRITY_CHECK_TIMEOUT_SECONDS=1800 opg_tree_hash "$T"); e1=$(( ($(date +%s%N)-s)/1000000 ))
  s=$(date +%s%N); h2=$(python3 /lab/v2hash.py "$T"); e2=$(( ($(date +%s%N)-s)/1000000 ))
  printf 'run %s  V1 %6s ms  V2 %6s ms\n' "$i" "$e1" "$e2"
done
echo "-- OPG tools/opg_hash_benchmark.sh"
bash /opt/opg/tools/opg_hash_benchmark.sh "$T" synthetic-2g | grep -E 'files=|duration_ms'
