#!/usr/bin/env bash # Time ZIP creation with Peel's engine at 1, 4, 8, 12 and all cores, against macOS's # own `ditto -c -k` (the engine behind Finder's Compress) and `bsdtar --format zip`, on corpora generated # here from fixed seeds (nothing downloaded). Prints a Markdown table. # Used by scripts/perf-gate.sh for the create speed gate (#798). # # Usage: scripts/bench-create.sh [runs] (default 5; the table shows the median) # scripts/bench-create.sh --sizes no timing: print the size of the ZIP each tool writes, # so the times can be read against equal-ish output # Each run times every column once, rotating which goes first, after one # untimed round (the same method as scripts/bench.sh). # Env: PEEL=/path/to/peel (default: dist/Peel.app's engine, else target/release/peel) # BENCH_DIR=/path (default /private/tmp/peel-bench-create; corpora are kept # there between runs, never inside the repository) # BENCH_THREADS="1 4 8 12" (thread counts to time besides "all cores") # BENCH_REAL=0 (skip the optional real corpus, Rust's std docs) # # Thread counts come from PEEL_CREATE_THREADS (create.rs, ZIP only; 1 is the # single-threaded writer). A count above this machine's core count prints "—" # (not tested), never a number. Any failed Peel run, failed `unzip -t`, or # archive that differs between thread counts prints FAIL and exits 1: the # output of every thread count must be byte for byte the one-thread output. # # Corpora (rows): # html-docs 2,500 text files of 1 KB to 600 KB, 125 MB (the shape of Rust's std docs) # mixed 26 MB of source-like text with a few incompressible "images" # many-small 20,000 files of 1 KB (file-system bound, shows where cores stop helping) # rust-std-docs the real docs from the active rustup toolchain, when installed (optional) set -euo pipefail SIZES=0; [[ "${1:-}" == --sizes ]] && SIZES=1 RUNS="${1:-5}"; [[ "$SIZES" == 1 ]] && RUNS=5 ROOT="$(cd "$(dirname "$0")/.." && pwd)" PEEL="${PEEL:-}" if [[ -z "$PEEL" ]]; then for c in "$ROOT/dist/Peel.app/Contents/Resources/peel" "$ROOT/target/release/peel"; do [[ -x "$c" ]] && PEEL="$c" && break done fi [[ -x "${PEEL:-}" ]] || { echo "No peel engine; build first (cargo build --release -p peel-cli) or set PEEL=" >&2; exit 2; } BENCH_DIR="${BENCH_DIR:-/private/tmp/peel-bench-create}" THREADS=(${BENCH_THREADS:-1 4 8 12}) NCPU="$(sysctl -n hw.ncpu)" SRC="$BENCH_DIR/src"; OUT="$BENCH_DIR/out"; TF="$BENCH_DIR/time.txt" mkdir -p "$BENCH_DIR" trap 'rm -rf "$OUT" "$TF"' EXIT # ── Corpora ───────────────────────────────────────────────────────────────── MARK="$SRC/.built-v1" if [[ ! -f "$MARK" ]]; then echo "Building corpora in ${SRC}…" >&2 rm -rf "$SRC"; mkdir -p "$SRC" python3 - "$SRC" <<'PY' import os, random, sys root = sys.argv[1] rnd = random.Random(20261130) # A 4 MB pool of HTML-ish text; files are slices of it, so deflate finds the # repetition a real documentation tree has (about 6:1 at the default level). vocab = ["".join(rnd.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(rnd.randint(2, 11))) for _ in range(3000)] weights = [1.0 / (i + 1) for i in range(len(vocab))] tags = ["div", "span", "a", "code", "pre", "p", "li", "td"] parts, n = [], 0 while n < 4_000_000: w = " ".join(rnd.choices(vocab, weights, k=rnd.randint(4, 20))) t = rnd.choice(tags) s = f'<{t} class="{rnd.choice(vocab)}">{w}\n' parts.append(s); n += len(s) pool = "".join(parts).encode() def text(size): off = rnd.randrange(0, len(pool) - size) return pool[off:off + size] def put(path, data): os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "wb") as f: f.write(data) # html-docs: 2,500 files, sizes log-uniform 1 KB..600 KB scaled to about 125 MB. sizes = [int(1000 * (600 ** rnd.random())) for _ in range(2500)] k = 125_000_000 / sum(sizes) for i, s in enumerate(sizes): put(f"{root}/html-docs/std/m{i % 40:02d}/p{i:05d}.html", text(max(200, min(int(s * k), 3_000_000)))) # mixed: source-like text plus incompressible blobs standing in for images. for i in range(500): put(f"{root}/mixed/src/d{i % 25:02d}/f{i:04d}.rs", text(rnd.randint(8_000, 32_000))) for i in range(150): put(f"{root}/mixed/site/d{i % 10:02d}/g{i:04d}.html", text(rnd.randint(20_000, 60_000))) for i in range(12): put(f"{root}/mixed/img/i{i:02d}.jpg", rnd.randbytes(800_000)) # many-small: 20,000 files of 1 KB. for i in range(20_000): put(f"{root}/many-small/d{i % 50:02d}/f{i:05d}.txt", text(1000)) PY touch "$MARK" fi CORPORA=(html-docs mixed many-small) if [[ "${BENCH_REAL:-1}" == 1 ]]; then DOCS="$(rustc --print sysroot 2>/dev/null)/share/doc/rust/html/std" if [[ -d "$DOCS" ]]; then rm -f "$SRC/rust-std-docs"; ln -s "$DOCS" "$SRC/rust-std-docs"; CORPORA+=(rust-std-docs); fi fi # ── Timing ────────────────────────────────────────────────────────────────── median() { sort -n | awk '{a[NR]=$1} END {print a[int((NR+1)/2)]}'; } # run_one : creates $OUT/x.zip once; prints seconds, or n/a on failure. run_one() { local t="$1" cmd rm -rf "$OUT"; mkdir -p "$OUT" case "$t" in ditto) cmd=(/usr/bin/ditto -c -k --keepParent "$SRC/$c" "$OUT/x.zip") ;; bsdtar) cmd=(/usr/bin/tar --format zip -cf "$OUT/x.zip" -C "$SRC/$c/" .) ;; all) cmd=(env PEEL_CREATE_THREADS="$NCPU" "$PEEL" create -q "$OUT/x.zip" "$SRC/$c/") ;; *) cmd=(env PEEL_CREATE_THREADS="$t" "$PEEL" create -q "$OUT/x.zip" "$SRC/$c/") ;; esac /usr/bin/time -p -o "$TF" "${cmd[@]}" >/dev/null 2>&1 || { echo "n/a"; return; } awk '/^real/ {print $2}' "$TF" } summary() { case " $* " in *" n/a "*) echo "FAIL" ;; *) printf '%s\n' "$@" | median ;; esac } cell() { if [[ "$1" == FAIL ]]; then echo "FAIL"; elif [[ "$1" == "—" ]]; then echo "—"; else echo "$1 s"; fi; } if [[ "$SIZES" == 1 ]]; then echo "| Corpus | Input | Peel | \`ditto -c -k\` | bsdtar (\`tar --format zip\`) |" echo "| --- | --- | --- | --- | --- |" for c in "${CORPORA[@]}"; do rm -rf "$OUT"; mkdir -p "$OUT" PEEL_CREATE_THREADS="$NCPU" "$PEEL" create -q "$OUT/peel.zip" "$SRC/$c/" >/dev/null 2>&1 || { echo "FAIL: peel create $c" >&2; exit 1; } /usr/bin/ditto -c -k --keepParent "$SRC/$c" "$OUT/ditto.zip" || exit 1 /usr/bin/tar --format zip -cf "$OUT/bsdtar.zip" -C "$SRC/$c/" . || exit 1 mb() { awk -v b="$(stat -f %z "$1")" 'BEGIN { printf "%.1f MB", b / 1000000 }'; } inb=$(find -L "$SRC/$c" -type f -exec stat -f %z {} + | awk '{s += $1} END { printf "%.1f MB", s / 1000000 }') echo "| $c | $inb | $(mb "$OUT/peel.zip") | $(mb "$OUT/ditto.zip") | $(mb "$OUT/bsdtar.zip") |" done echo echo "_Sizes in decimal megabytes (1 MB = 1,000,000 bytes), default level, $(date +%Y-%m-%d)._" exit 0 fi tools=() for n in "${THREADS[@]}"; do [[ "$n" -le "$NCPU" ]] && tools+=("$n"); done tools+=(all ditto bsdtar) fail=0 echo "| Corpus | Size | 1 core | 4 cores | 8 cores | 12 cores | all $NCPU cores | \`ditto -c -k\` (Finder's Compress) | Output | bsdtar (\`tar --format zip\`) |" echo "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |" for c in "${CORPORA[@]}"; do # Exactness: one-thread output is the reference; every other count must match it. verdict="identical, \`unzip -t\` ok" PEEL_CREATE_THREADS=1 "$PEEL" create -q "$BENCH_DIR/ref.zip" "$SRC/$c/" >/dev/null 2>&1 || verdict="FAIL (create)" /usr/bin/unzip -tq "$BENCH_DIR/ref.zip" >/dev/null 2>&1 || verdict="FAIL (unzip -t)" for n in "${tools[@]}"; do [[ "$n" == ditto || "$n" == bsdtar ]] && continue cnt="$n"; [[ "$n" == all ]] && cnt="$NCPU" PEEL_CREATE_THREADS="$cnt" "$PEEL" create -q "$BENCH_DIR/chk.zip" "$SRC/$c/" >/dev/null 2>&1 \ && cmp -s "$BENCH_DIR/ref.zip" "$BENCH_DIR/chk.zip" || verdict="FAIL (threads $cnt differ)" done rm -f "$BENCH_DIR/ref.zip" "$BENCH_DIR/chk.zip" [[ "$verdict" == FAIL* ]] && fail=1 # bash 3.2 (macOS) has no associative arrays: results are indexed by position in "tools". runs=(); for t in "${tools[@]}"; do runs+=(""); done for run in $(seq 0 "$RUNS"); do # run 0 is the untimed round k=$((run % ${#tools[@]})) for off in $(seq 0 $((${#tools[@]} - 1))); do i=$(((k + off) % ${#tools[@]})) s=$(run_one "${tools[$i]}") [[ "$run" == 0 ]] && continue runs[$i]+="$s " done done med=(); for i in "${!tools[@]}"; do m=$(summary ${runs[$i]}); med+=("$m"); [[ "$m" == FAIL ]] && fail=1; done get() { local i; for i in "${!tools[@]}"; do [[ "${tools[$i]}" == "$1" ]] && { echo "${med[$i]}"; return; }; done; echo "—"; } sz=$(du -shL "$SRC/$c" | awk '{print $1}') files=$(find -L "$SRC/$c" -type f | wc -l | tr -d ' ') echo "| $c ($files files) | $sz | $(cell "$(get 1)") | $(cell "$(get 4)") | $(cell "$(get 8)") | $(cell "$(get 12)") | $(cell "$(get all)") | $(cell "$(get ditto)") | $verdict | $(cell "$(get bsdtar)") |" done echo echo "_$(sysctl -n machdep.cpu.brand_string), $NCPU cores, macOS $(sw_vers -productVersion), median of $RUNS interleaved runs, $(date +%Y-%m-%d), load average $(sysctl -n vm.loadavg | tr -d '{}' | awk '{print $1}') at the end._" exit "$fail"