Files
EasyDNS/scripts/bench-vs-bind.sh
T
2026-11-22 14:21:54 +01:00

127 lines
6.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Compare EasyDNS and BIND 9 on the same machine with dnsperf (authoritative UDP).
#
# scripts/bench-vs-bind.sh [--cores N] [--duration 30] [--clients 200] [--records 10000]
# [--easydns PATH] [--named PATH] [--out DIR]
#
# Needs: dnsperf, named (bind9), taskset (util-linux), python3, a release build of
# EasyDNS (cargo build --release -p dns-cli) or an installed `easydns`.
# Both servers run on 127.0.0.1 with the same zone, pinned to the same CPU cores
# (0..N-1); dnsperf runs on the remaining cores. Nothing is installed or changed on the
# system: everything lives in a temporary directory. Results: <out>/bench-<date>.md
set -euo pipefail
CORES=$(( $(nproc) / 2 )); DURATION=30; CLIENTS=200; RECORDS=10000
EASYDNS="${EASYDNS:-}"; NAMED="${NAMED:-$(command -v named || true)}"
OUT="bench-results"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
while [ $# -gt 0 ]; do
case "$1" in
--cores) CORES=$2; shift 2;; --duration) DURATION=$2; shift 2;;
--clients) CLIENTS=$2; shift 2;; --records) RECORDS=$2; shift 2;;
--easydns) EASYDNS=$2; shift 2;; --named) NAMED=$2; shift 2;; --out) OUT=$2; shift 2;;
-h|--help) sed -n '2,12p' "$0"; exit 0;;
*) echo "unknown option $1" >&2; exit 2;;
esac
done
[ -n "$EASYDNS" ] || EASYDNS="$ROOT/target/release/easydns"
[ -x "$EASYDNS" ] || EASYDNS="$(command -v easydns || true)"
for t in dnsperf taskset python3; do command -v "$t" >/dev/null || { echo "missing: $t" >&2; exit 2; }; done
[ -n "$EASYDNS" ] && [ -x "$EASYDNS" ] || { echo "easydns binary not found (cargo build --release -p dns-cli)" >&2; exit 2; }
[ -n "$NAMED" ] && [ -x "$NAMED" ] || { echo "named not found (apt install bind9)" >&2; exit 2; }
TOTAL=$(nproc)
[ "$CORES" -ge 1 ] && [ "$CORES" -lt "$TOTAL" ] || { echo "--cores must be 1..$((TOTAL-1)) (dnsperf needs the rest)" >&2; exit 2; }
SRV_CPUS="0-$((CORES-1))"; GEN_CPUS="$CORES-$((TOTAL-1))"; GEN_THREADS=$(( TOTAL - CORES ))
W="$(mktemp -d /tmp/easydns-bench.XXXXXX)"; trap 'kill $(jobs -p) 2>/dev/null || true; rm -rf "$W"' EXIT
P_BIND=15301; P_EASY=15302
# --- zone and queries ------------------------------------------------------------
python3 - "$W" "$RECORDS" <<'PY'
import sys, random
w, n = sys.argv[1], int(sys.argv[2])
with open(f"{w}/example.com.zone", "w") as z:
z.write("$TTL 3600\n@ IN SOA ns1.example.com. hostmaster.example.com. 1 3600 600 1209600 300\n")
z.write("@ IN NS ns1.example.com.\nns1 IN A 127.0.0.1\n@ IN MX 10 mail\nmail IN A 192.0.2.25\n")
for i in range(n):
z.write(f"h{i} IN A 198.51.{(i>>8)&255}.{i&255}\n")
random.seed(1)
with open(f"{w}/queries.txt", "w") as q:
for _ in range(200000):
r = random.random()
if r < 0.80: q.write(f"h{random.randrange(n)}.example.com A\n") # existing names
elif r < 0.90: q.write("example.com MX\n")
else: q.write(f"nx{random.randrange(10**6)}.example.com A\n") # NXDOMAIN
PY
# --- BIND ----------------------------------------------------------------------------
mkdir -p "$W/bind"
cat > "$W/bind/named.conf" <<EOF
options {
directory "$W/bind"; pid-file "$W/bind/named.pid";
listen-on port $P_BIND { 127.0.0.1; }; listen-on-v6 { none; };
recursion no; minimal-responses yes; querylog no;
};
zone "example.com" { type primary; file "$W/example.com.zone"; };
EOF
# --- EasyDNS ---------------------------------------------------------------------
easy_conf() { # $1 = answer cache entries
cat > "$W/easydns.yaml" <<EOF
server: { mode: primary, listen: ["127.0.0.1:$P_EASY"], data_dir: $W/easy-data, udp_sockets: $CORES, max_inflight: 20000 }
logging: { level: warn }
rate_limit: { enabled: false }
api: { enabled: false }
metrics: { enabled: false }
cache: { answer_entries: $1 }
zones: [ { name: example.com, type: primary, file: $W/example.com.zone } ]
EOF
}
run_perf() { # $1 = port, $2 = label → prints "qps avg_ms"
taskset -c "$GEN_CPUS" dnsperf -s 127.0.0.1 -p "$1" -d "$W/queries.txt" -l "$DURATION" \
-c "$CLIENTS" -T "$GEN_THREADS" -q 1000 -Q 10000000 > "$W/$2.txt" 2>&1 || true
python3 - "$W/$2.txt" <<'PY'
import re, sys
t = open(sys.argv[1]).read()
qps = re.search(r"Queries per second:\s+([\d.]+)", t)
lat = re.search(r"Average Latency \(s\):\s+([\d.]+)", t)
lost = re.search(r"Queries lost:\s+(\d+) \(([\d.]+)%\)", t)
print(f"{float(qps.group(1)) if qps else 0:.0f} {float(lat.group(1))*1000 if lat else 0:.3f} {lost.group(2) if lost else '?'}")
PY
}
rss_kb() { grep VmRSS "/proc/$1/status" 2>/dev/null | awk '{print $2}'; }
declare -a ROWS
bench() { # $1 label, $2 port, $3 pid
sleep 2
taskset -c "$GEN_CPUS" dnsperf -s 127.0.0.1 -p "$2" -d "$W/queries.txt" -l 5 -c 20 -Q 10000 >/dev/null 2>&1 || true # warm-up
read -r qps lat lost < <(run_perf "$2" "$1")
ROWS+=("| $1 | $qps | $lat | $lost% | $(( $(rss_kb "$3") / 1024 )) MB |")
echo "$1: $qps QPS, avg ${lat} ms, lost $lost%"
}
echo ">> $(nproc) CPUs: servers on $SRV_CPUS, dnsperf on $GEN_CPUS ($GEN_THREADS threads), $DURATION s, $CLIENTS clients, $RECORDS records"
taskset -c "$SRV_CPUS" "$NAMED" -g -n "$CORES" -c "$W/bind/named.conf" >"$W/named.log" 2>&1 & BIND_PID=$!
bench "BIND $("$NAMED" -v | awk '{print $2}')" "$P_BIND" "$BIND_PID"; kill "$BIND_PID"; wait "$BIND_PID" 2>/dev/null || true
for entries in 20000 0; do
easy_conf "$entries"
taskset -c "$SRV_CPUS" "$EASYDNS" run --config "$W/easydns.yaml" >"$W/easy.log" 2>&1 & EASY_PID=$!
label="EasyDNS $("$EASYDNS" --version | awk '{print $2}')"; [ "$entries" = 0 ] && label="$label (answer cache off)"
bench "$label" "$P_EASY" "$EASY_PID"; kill "$EASY_PID"; wait "$EASY_PID" 2>/dev/null || true
done
mkdir -p "$OUT"; F="$OUT/bench-$(date +%Y%m%d-%H%M%S).md"
{
echo "# EasyDNS vs BIND – $(date -Iseconds)"
echo
echo "CPU: $(grep -m1 'model name' /proc/cpuinfo | cut -d: -f2- | xargs) · $(nproc) CPUs · server cores: $CORES ($SRV_CPUS)"
echo "Kernel: $(uname -r) · dnsperf: $GEN_THREADS threads, $CLIENTS clients, $DURATION s · zone: $RECORDS A records"
echo "Queries: 80 % existing A, 10 % MX, 10 % NXDOMAIN (random names)"
echo
echo "| Server | QPS | avg latency (ms) | lost | RSS |"
echo "|---|---|---|---|---|"
printf '%s\n' "${ROWS[@]}"
} > "$F"
echo ">> results: $F"; cat "$F"