navid/veins / master / xveins/bench/net_bench.sh
net_bench.sh
← xveins/bench · 16078 bytes · raw · download
#!/bin/sh
# Compare the C++ rewrite with the Spectre reference over the native protocol.
#
# Requirements: hyperfine, python3, openssl, a built C++ vn, and the Spectre vn
# in ../veins/vn. The script creates a throwaway dataset under bench/.net-bench,
# runs hyperfine, and writes BENCHES.md.
set -eu
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
ROOT=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
REPO=$(CDPATH= cd -- "$ROOT/.." && pwd)
VN_CPP=${VN_CPP:-$ROOT/build/vn}
VN_SPECTRE=${VN_SPECTRE:-$REPO/veins/vn}
HYPERFINE=${HYPERFINE:-hyperfine}
WORK=${WORK:-$ROOT/bench/.net-bench}
OUT=${OUT:-$ROOT/BENCHES.md}
RUNS=${RUNS:-10}
WHOAMI_RUNS=${WHOAMI_RUNS:-20}
SYS_PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
case "$WORK" in
"" | "/" | ".")
echo "refusing to use WORK='$WORK'" >&2
exit 1
;;
esac
CPP_DIR=$(CDPATH= cd -- "$(dirname -- "$VN_CPP")" && pwd)
SX_DIR=$(CDPATH= cd -- "$(dirname -- "$VN_SPECTRE")" && pwd)
VN_CPP="$CPP_DIR/$(basename -- "$VN_CPP")"
VN_SPECTRE="$SX_DIR/$(basename -- "$VN_SPECTRE")"
for tool in "$VN_CPP" "$VN_SPECTRE"; do
if [ ! -x "$tool" ]; then
echo "missing executable: $tool" >&2
echo "build the C++ rewrite first: cmake --build $ROOT/build" >&2
exit 1
fi
done
for tool in "$HYPERFINE" python3 openssl; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "required tool not found: $tool" >&2
exit 1
fi
done
rm -rf "$WORK"
mkdir -p "$WORK/config" "$WORK/bench_src" "$WORK/push_pristine" "$WORK/fakebin"
XDG_CONFIG_HOME="$WORK/config"
export XDG_CONFIG_HOME
CPP_RUN="env XDG_CONFIG_HOME='$XDG_CONFIG_HOME' PATH='$CPP_DIR:$SYS_PATH' '$VN_CPP'"
SX_RUN="env XDG_CONFIG_HOME='$XDG_CONFIG_HOME' PATH='$SX_DIR:$SYS_PATH' '$VN_SPECTRE'"
CPP_SSH_RUN="env XDG_CONFIG_HOME='$XDG_CONFIG_HOME' PATH='$WORK/fakebin:$CPP_DIR:$SYS_PATH' '$VN_CPP'"
SX_SSH_RUN="env XDG_CONFIG_HOME='$XDG_CONFIG_HOME' PATH='$WORK/fakebin:$SX_DIR:$SYS_PATH' '$VN_SPECTRE'"
run_cpp() {
env XDG_CONFIG_HOME="$XDG_CONFIG_HOME" PATH="$CPP_DIR:$SYS_PATH" "$VN_CPP" "$@"
}
run_sx() {
env XDG_CONFIG_HOME="$XDG_CONFIG_HOME" PATH="$SX_DIR:$SYS_PATH" "$VN_SPECTRE" "$@"
}
wait_port() {
python3 - "$1" <<'PY'
import socket, sys, time
port = int(sys.argv[1])
for _ in range(100):
sock = socket.socket()
sock.settimeout(0.1)
try:
sock.connect(("127.0.0.1", port))
sock.close()
sys.exit(0)
except OSError:
sock.close()
time.sleep(0.1)
sys.exit(1)
PY
}
generate_small_files() {
python3 - "$1" "$2" "$3" <<'PY'
import os, sys
root, start, stop = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
body = ("abcdefghijklmnopqrstuvwxyz" * 70)[:1800]
for d in range(start, stop):
folder = os.path.join(root, "d%02d" % d)
os.makedirs(folder, exist_ok=True)
for f in range(20):
path = os.path.join(folder, "f%02d.txt" % f)
with open(path, "w", encoding="utf-8") as handle:
handle.write("file %d-%d\n" % (d, f))
handle.write(body)
handle.write("\n")
PY
}
run_cpp key generate benchkey >"$WORK/key.txt"
PUB=$(awk '/public key:/ {print $3}' "$WORK/key.txt")
if [ -z "$PUB" ]; then
echo "could not generate the benchmark key" >&2
exit 1
fi
run_cpp auth use benchkey >/dev/null
grant_access() {
(cd "$1" && run_cpp access anonymous on &&
run_cpp access add -role write -key "$PUB" -name bench >/dev/null)
}
(
cd "$WORK/bench_src"
run_sx init . >/dev/null
generate_small_files . 0 12
python3 - <<'PY'
import os
with open("big_old.bin", "wb") as handle:
handle.write(os.urandom(2 * 1024 * 1024))
PY
run_sx go -m "base commit" >/dev/null
# Point oldbig at the real base commit, not the movable working-copy commit.
BASE_CHANGE=$(run_sx log | awk '/base commit/ {print $2}' | tail -1)
if [ -z "$BASE_CHANGE" ]; then
echo "could not resolve the base commit" >&2
exit 1
fi
run_sx bmk set oldbig -rev "$BASE_CHANGE" >/dev/null
rm -f big_old.bin
generate_small_files . 12 18
python3 - <<'PY'
import os
with open("big_head.bin", "wb") as handle:
handle.write(os.urandom(1 * 1024 * 1024))
PY
run_sx go -m "head commit" >/dev/null
run_sx bmk set main >/dev/null
)
grant_access "$WORK/bench_src"
# A separate small repository for push, so a push does not transfer the large
# dataset. The reference pushes all reachable objects, as does the rewrite.
(
cd "$WORK/push_pristine"
run_sx init . >/dev/null
generate_small_files . 0 1
run_sx go -m "push base" >/dev/null
run_sx bmk set main >/dev/null
)
grant_access "$WORK/push_pristine"
run_cpp clone "file://$WORK/bench_src" "$WORK/fetch_pristine_cpp" -blobs:all >/dev/null
run_sx clone "file://$WORK/bench_src" "$WORK/fetch_pristine_sx" -blobs:all >/dev/null
run_cpp clone "file://$WORK/bench_src" "$WORK/lazy_pristine_cpp" -blobs:none >/dev/null
run_sx clone "file://$WORK/bench_src" "$WORK/lazy_pristine_sx" -blobs:none >/dev/null
run_cpp clone "file://$WORK/push_pristine" "$WORK/push_client_cpp" >/dev/null
run_sx clone "file://$WORK/push_pristine" "$WORK/push_client_sx" >/dev/null
(
cd "$WORK/push_client_cpp"
printf 'bench push\n' >push.txt
run_cpp go -m "bench push" >/dev/null
run_cpp bmk set main >/dev/null
run_cpp remote set-url origin "file://$WORK/push_srv_cpp"
)
(
cd "$WORK/push_client_sx"
printf 'bench push\n' >push.txt
run_sx go -m "bench push" >/dev/null
run_sx bmk set main >/dev/null
run_sx remote set-url origin "file://$WORK/push_srv_sx"
)
cat >"$WORK/fakebin/ssh" <<'SH'
#!/bin/sh
while [ $# -gt 0 ]; do
case "$1" in
-p|-l) shift 2 ;;
-*) shift ;;
*) break ;;
esac
done
host=$1
shift
exec /bin/sh -c "$*"
SH
chmod +x "$WORK/fakebin/ssh"
cat >"$WORK/openssl.cnf" <<EOF
[req]
distinguished_name = dn
x509_extensions = v3_req
prompt = no
[dn]
CN = localhost
[v3_req]
subjectAltName = @alt
[alt]
DNS.1 = localhost
IP.1 = 127.0.0.1
EOF
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout "$WORK/key.pem" -out "$WORK/cert.pem" \
-days 2 -config "$WORK/openssl.cnf" >/dev/null 2>&1
PIN=$(openssl x509 -in "$WORK/cert.pem" -noout -fingerprint -sha256 |
sed 's/.*=//;s/://g' | tr 'A-F' 'a-f')
free_port() {
python3 - <<'PY'
import socket
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
print(sock.getsockname()[1])
sock.close()
PY
}
PORT_CPP=$(free_port)
PORT_SX=$(free_port)
while [ "$PORT_SX" = "$PORT_CPP" ]; do PORT_SX=$(free_port); done
CPP_PID=""
SX_PID=""
cleanup() {
if [ -n "$CPP_PID" ]; then kill "$CPP_PID" 2>/dev/null || true; fi
if [ -n "$SX_PID" ]; then kill "$SX_PID" 2>/dev/null || true; fi
if [ -n "$CPP_PID" ]; then wait "$CPP_PID" 2>/dev/null || true; fi
if [ -n "$SX_PID" ]; then wait "$SX_PID" 2>/dev/null || true; fi
}
trap cleanup EXIT INT TERM
"$VN_CPP" serve -listen "127.0.0.1:$PORT_CPP" -root "$WORK" \
-tls-cert "$WORK/cert.pem" -tls-key "$WORK/key.pem" \
>"$WORK/cpp-server.log" 2>&1 &
CPP_PID=$!
"$VN_SPECTRE" serve -listen "127.0.0.1:$PORT_SX" -root "$WORK" \
-tls-cert "$WORK/cert.pem" -tls-key "$WORK/key.pem" \
>"$WORK/sx-server.log" 2>&1 &
SX_PID=$!
wait_port "$PORT_CPP"
wait_port "$PORT_SX"
# A tiny repository used only for whoami over TLS.
mkdir -p "$WORK/whoami_repo"
(
cd "$WORK/whoami_repo"
run_cpp init . >/dev/null
run_cpp remote add tls_cpp "veins://localhost:$PORT_CPP/bench_src" \
-key:benchkey -pin:"$PIN" >/dev/null
run_cpp remote add tls_sx "veins://localhost:$PORT_SX/bench_src" \
-key:benchkey -pin:"$PIN" >/dev/null
)
write_prepare() {
path="$WORK/$1"
shift
{
echo '#!/bin/sh'
printf '%s\n' "$@"
} >"$path"
chmod +x "$path"
}
write_prepare prepare_clone.sh \
"rm -rf '$WORK/clone_cpp' '$WORK/clone_sx'"
write_prepare prepare_fetch.sh \
"rm -rf '$WORK/fetch_cpp' '$WORK/fetch_sx'" \
"cp -R '$WORK/fetch_pristine_cpp' '$WORK/fetch_cpp'" \
"cp -R '$WORK/fetch_pristine_sx' '$WORK/fetch_sx'"
write_prepare prepare_lazy.sh \
"rm -rf '$WORK/lazy_cpp' '$WORK/lazy_sx'" \
"cp -R '$WORK/lazy_pristine_cpp' '$WORK/lazy_cpp'" \
"cp -R '$WORK/lazy_pristine_sx' '$WORK/lazy_sx'"
write_prepare prepare_push.sh \
"rm -rf '$WORK/push_srv_cpp' '$WORK/push_srv_sx'" \
"cp -R '$WORK/push_pristine' '$WORK/push_srv_cpp'" \
"cp -R '$WORK/push_pristine' '$WORK/push_srv_sx'"
write_prepare prepare_ssh.sh \
"rm -rf '$WORK/clone_ssh_cpp' '$WORK/clone_ssh_sx'"
write_prepare prepare_tls.sh \
"rm -rf '$WORK/clone_tls_cpp' '$WORK/clone_tls_sx'"
write_prepare prepare_noop.sh \
"exit 0"
run_pair() {
name=$1
prepare=$2
runs=$3
warmup=$4
cpp_cmd=$5
sx_cmd=$6
echo
echo "== $name =="
"$HYPERFINE" --style basic --warmup "$warmup" --runs "$runs" \
--prepare "$prepare" \
--export-markdown "$WORK/$name.md" \
--export-json "$WORK/$name.json" \
-n "C++" "$cpp_cmd" \
-n "Spectre" "$sx_cmd"
}
SSH_URL="ssh://localhost/$WORK/bench_src"
TLS_CPP_URL="veins://localhost:$PORT_CPP/bench_src"
TLS_SX_URL="veins://localhost:$PORT_SX/bench_src"
run_pair clone-file "$WORK/prepare_clone.sh" "$RUNS" 1 \
"cd '$WORK' && $CPP_RUN clone 'file://$WORK/bench_src' '$WORK/clone_cpp'" \
"cd '$WORK' && $SX_RUN clone 'file://$WORK/bench_src' '$WORK/clone_sx'"
run_pair fetch-file "$WORK/prepare_fetch.sh" "$RUNS" 1 \
"cd '$WORK/fetch_cpp' && $CPP_RUN fetch origin" \
"cd '$WORK/fetch_sx' && $SX_RUN fetch origin"
run_pair lazy-checkout-file "$WORK/prepare_lazy.sh" "$RUNS" 1 \
"cd '$WORK/lazy_cpp' && $CPP_RUN co oldbig" \
"cd '$WORK/lazy_sx' && $SX_RUN co oldbig"
run_pair push-file "$WORK/prepare_push.sh" "$RUNS" 1 \
"cd '$WORK/push_client_cpp' && $CPP_RUN push origin main" \
"cd '$WORK/push_client_sx' && $SX_RUN push origin main"
run_pair whoami-file "$WORK/prepare_noop.sh" "$WHOAMI_RUNS" 2 \
"cd '$WORK' && $CPP_RUN whoami 'file://$WORK/bench_src'" \
"cd '$WORK' && $SX_RUN whoami 'file://$WORK/bench_src'"
run_pair clone-ssh "$WORK/prepare_ssh.sh" "$RUNS" 1 \
"cd '$WORK' && $CPP_SSH_RUN clone '$SSH_URL' '$WORK/clone_ssh_cpp'" \
"cd '$WORK' && $SX_SSH_RUN clone '$SSH_URL' '$WORK/clone_ssh_sx'"
run_pair clone-tls "$WORK/prepare_tls.sh" "$RUNS" 1 \
"cd '$WORK' && $CPP_RUN clone '$TLS_CPP_URL' '$WORK/clone_tls_cpp' -pin:'$PIN'" \
"cd '$WORK' && $SX_RUN clone '$TLS_SX_URL' '$WORK/clone_tls_sx' -pin:'$PIN'"
run_pair whoami-tls "$WORK/prepare_noop.sh" "$WHOAMI_RUNS" 2 \
"cd '$WORK/whoami_repo' && $CPP_RUN whoami tls_cpp" \
"cd '$WORK/whoami_repo' && $SX_RUN whoami tls_sx"
cleanup
trap - 0 2 15
python3 - "$WORK" >"$WORK/summary.md" <<'PY'
import json, os, sys
work = sys.argv[1]
names = [
("clone-file", "Clone file://"),
("fetch-file", "Fetch file://"),
("lazy-checkout-file", "Lazy checkout file://"),
("push-file", "Push file://"),
("whoami-file", "Whoami file://"),
("clone-ssh", "Clone ssh://"),
("clone-tls", "Clone TLS 1.3"),
("whoami-tls", "Whoami TLS 1.3"),
]
print("| Benchmark | C++ mean | Spectre mean | Spectre / C++ |")
print("|:---|---:|---:|---:|")
for key, label in names:
with open(os.path.join(work, key + ".json"), encoding="utf-8") as handle:
data = json.load(handle)
means = {r["command"]: r["mean"] for r in data["results"]}
cpp = means["C++"] * 1000.0
sx = means["Spectre"] * 1000.0
ratio = sx / cpp if cpp else float("nan")
print(f"| {label} | {cpp:.1f} ms | {sx:.1f} ms | {ratio:.2f}x |")
PY
FILE_COUNT=$(find "$WORK/bench_src" -type f -not -path '*/.veins/*' | wc -l | tr -d ' ')
DISK_SIZE=$(du -sh "$WORK/bench_src" | awk '{print $1}')
HOST_TEXT=$(uname -sm)
CPU_TEXT=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || uname -m)
HYPERFINE_VERSION=$("$HYPERFINE" --version)
CPP_VERSION=$("$VN_CPP" version)
SX_VERSION=$("$VN_SPECTRE" version)
{
echo "# veins benchmarks"
echo
echo "Two reproducible benchmark sets are kept here: a local-core comparison that includes"
echo "the Rust subset, and the native network comparison between the C++ rewrite and Spectre."
echo
if [ -f "$ROOT/bench/core_bench.md" ]; then
echo "## Local core: Rust subset vs C++ vs Spectre"
echo
cat "$ROOT/bench/core_bench.md"
echo
fi
echo "## Networking: C++ rewrite vs Spectre"
echo
echo "Generated $(date -u '+%Y-%m-%d %H:%M:%S UTC') by \`bench/net_bench.sh\`."
echo
echo "The Rust subset is not included in this section because it does not implement a"
echo "network transport."
echo
echo "### Environment"
echo
echo '```'
echo "host: $HOST_TEXT"
echo "cpu: $CPU_TEXT"
echo "hyperfine: $HYPERFINE_VERSION"
echo "c++ vn: $CPP_VERSION"
echo "spectre vn: $SX_VERSION"
echo "runs: $RUNS (clone/fetch/push/checkout), $WHOAMI_RUNS (whoami)"
echo "warmup: 1 (clone/fetch/push/checkout), 2 (whoami)"
echo '```'
echo
echo "### Dataset"
echo
echo "- \`bench_src\`: $FILE_COUNT working files, $DISK_SIZE on disk including packs."
echo " - 360 small text files in 18 directories."
echo " - 2 MiB \`big_old.bin\` in the \`oldbig\` history."
echo " - 1 MiB \`big_head.bin\` in \`main\`."
echo "- \`push_pristine\`: a separate 20-file repository, so push does not transfer the large dataset."
echo
echo "### What each benchmark does"
echo
echo "| Benchmark | Operation |"
echo "|:---|:---|"
echo "| \`clone-file\` | Full clone over \`file://\`: handshake, refs, history, head chunks, checkout. Includes spawning one local server per run. |"
echo "| \`fetch-file\` | \`vn fetch origin\` with no new remote refs. Both clients start from a full \`-blobs:all\` clone. |"
echo "| \`lazy-checkout-file\` | \`vn co oldbig\` in a \`-blobs:none\` clone, fetching the 2 MiB old blob from the promisor over \`file://\`. |"
echo "| \`push-file\` | Push one new commit from a prepared client to a freshly reset copy of \`push_pristine\` over \`file://\`. |"
echo "| \`whoami-file\` | Connect, authenticate, \`WHOAMI\`, close over \`file://\`. |"
echo "| \`clone-ssh\` | Full clone over \`ssh://\`, using a local \`ssh\` shim that runs the remote \`vn serve -stdio\` command. |"
echo "| \`clone-tls\` | Full clone over TLS 1.3 with a pinned self-signed certificate. Uses long-running servers. |"
echo "| \`whoami-tls\` | Connect, authenticate, \`WHOAMI\`, close over TLS 1.3. |"
echo
echo "Lower is better. Hyperfine reports wall-clock time; the relative column is against the fastest command."
echo
echo "### Summary"
echo
cat "$WORK/summary.md"
echo
echo "A ratio above 1 means Spectre took longer than C++."
echo
echo "### Results"
echo
echo "#### Clone over file://"
cat "$WORK/clone-file.md"
echo
echo "#### Fetch over file://"
cat "$WORK/fetch-file.md"
echo
echo "#### Lazy checkout over file://"
cat "$WORK/lazy-checkout-file.md"
echo
echo "#### Push over file://"
cat "$WORK/push-file.md"
echo
echo "#### Whoami over file://"
cat "$WORK/whoami-file.md"
echo
echo "#### Clone over ssh://"
cat "$WORK/clone-ssh.md"
echo
echo "#### Clone over TLS 1.3"
cat "$WORK/clone-tls.md"
echo
echo "#### Whoami over TLS 1.3"
cat "$WORK/whoami-tls.md"
echo
echo "### Notes"
echo
echo "- Spectre clone destinations are absolute paths on macOS. The reference checkout hardcodes Linux's \`AT_FDCWD\` value (\`-100\`); with a relative destination it cannot open the working directory on macOS."
echo "- \`file://\` and \`ssh://\` include server process startup. \`clone-tls\` and \`whoami-tls\` use long-running servers, so they isolate protocol and TLS cost more closely."
echo "- These runs reproduce the reference's transfer behavior: fetch \`haves\` are operation heads (so metadata is re-sent), and push sends all objects reachable from the bookmark rather than using \`HAVE_QUERY\`."
echo "- Lazy checkout fetches one missing chunk per promisor connection in both implementations, so it is intentionally a worst-case/read-ahead-free measurement."
echo "- \`push-file\` is only a few milliseconds of real work; process startup noise can dominate. Read the min/max columns alongside the mean."
echo "- Treat differences below roughly 10 ms as noise; this is a single-machine run, not a controlled lab measurement."
echo
echo "### Reproduce"
echo
echo '```sh'
echo "cd veins-cpprewrite"
echo "cmake -S . -B build && cmake --build build -j"
echo "./bench/net_bench.sh"
echo '```'
} >"$OUT"
echo
echo "wrote $OUT"