#!/usr/bin/env bash
# Builds the stores scripts/mem-bench.sh serves, once, into STORES (default
# /tmp/quarry-mem-bench/stores). Deterministic; skips a store that exists.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
S=${STORES:-/tmp/quarry-mem-bench/stores}
mkdir -p "$S"
build() { # name commits files branches tags
  local n=$1
  [ -f "$S/$n/.zit" ] && return 0
  local g; g=$(mktemp -d "$S/.gen.XXXXXX")
  ( cd "$g" && git init -q -b main . &&
    python3 "$here/mem-bench-gen.py" "$2" "$3" "$4" "$5" | git fast-import --quiet &&
    git checkout -q main )
  rm -rf "$S/$n"
  zit import-git "$g" "$S/$n" --quiet
  rm -rf "$g"
  echo "built $n: $2 commits"
}
build small 2000 300 8 20
build medium 8000 1200 25 80
build large 30000 3000 60 300

# The live forge holds dozens of stores, so the floor of idle connections
# matters as much as the heavy queries: serve copies of the small and
# medium stores under their own names.
copies() { # base count
  local i
  for i in $(seq 1 "$2"); do
    [ -d "$S/$1-$i" ] || cp -R "$S/$1" "$S/$1-$i"
  done
}
copies small ${SMALL_COPIES:-24}
copies medium ${MEDIUM_COPIES:-3}
