diff --git a/benches/benchmark.sh b/benches/benchmark.sh new file mode 100755 index 0000000..9f8a625 --- /dev/null +++ b/benches/benchmark.sh @@ -0,0 +1,270 @@ +#!/usr/bin/env bash +# spell-checker:ignore timeformat tempdir ldd passwd pwck +# +# Benchmark script for shadow-rs vs GNU shadow-utils. +# +# Compares performance, binary size, and shared library dependencies +# between shadow-rs (Rust) and GNU shadow-utils (C) implementations. +# +# Usage: +# docker compose run --rm debian bash benches/benchmark.sh +# +# Requirements: +# - Must run inside Docker (needs both GNU shadow-utils and shadow-rs) +# - Must run as root (passwd -S and pwck require root or shadow access) +# +# The script builds shadow-rs in release mode, locates GNU shadow-utils +# binaries, and runs comparative benchmarks. + +set -euo pipefail + +# --------------------------------------------------------------------------- +# Configuration +# --------------------------------------------------------------------------- + +PASSWD_STATUS_ITERS=1000 +PWCK_ITERS=100 + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + +log() { printf '\033[1;34m==> %s\033[0m\n' "$*"; } +warn() { printf '\033[1;33mWARN: %s\033[0m\n' "$*"; } +err() { printf '\033[1;31mERROR: %s\033[0m\n' "$*"; exit 1; } + +# Print a separator line. +separator() { printf '%.0s-' {1..72}; printf '\n'; } + +# Format bytes as human-readable. +human_size() { + local bytes=$1 + if [ "$bytes" -ge 1048576 ]; then + printf '%.1f MiB' "$(echo "$bytes / 1048576" | bc -l)" + elif [ "$bytes" -ge 1024 ]; then + printf '%.1f KiB' "$(echo "$bytes / 1024" | bc -l)" + else + printf '%d B' "$bytes" + fi +} + +# Time a command N times, report wall-clock total in seconds. +# Usage: bench_command