dns-bench/main.sh

93 lines
1.9 KiB
Bash
Raw Normal View History

2024-12-20 10:15:17 +04:00
#!/bin/bash
IFS=$'\n'
set -eu -o pipefail
2024-12-20 19:01:38 +04:00
2024-12-20 10:15:17 +04:00
cleanup () {
echo
2024-12-20 19:01:38 +04:00
# if Unbound is not started, exit from function
[ -v unbound_pid ] || return
2024-12-20 10:15:17 +04:00
echo '==> Stopping Unbound'
2024-12-20 19:01:38 +04:00
kill -INT "$unbound_pid"
2024-12-20 10:15:17 +04:00
echo
}
trap cleanup EXIT
2024-12-20 19:01:38 +04:00
built_or_system () {
# args: [1]custom/build/path [2]program_name [3]build_script [4]package_name
if [ -e "$1" ]; then
echo "$2: using custom build at $1" >&2 # stderr; just a message to user
echo "$1" # stdout; captured into a variable with $(), see below
elif exe=$(which "$2"); then
echo "$exe" # stdout
else
echo " ** $2: not found neither built nor in-system" >&2 # stderr
echo " use script './$3' to automatically build it at the correct path" >&2
# if $4 is set, say about possible pkg name
[ -v 4 ] && \
echo " or install it from your package manager, usually pkg is named '$4'" >&2
return 1
fi
}
exe_dig="bind9/bin/dig/dig"
exe_drill="ldns/drill/drill"
exe_hr="hickory-dns/target/release/resolve"
2024-12-20 10:15:17 +04:00
echo
echo '==> Checking deps'
which unbound
which hyperfine
2024-12-20 19:01:38 +04:00
exe_dig=$(built_or_system "$exe_dig" dig "build_dig.sh" bind-utils)
exe_drill=$(built_or_system "$exe_drill" drill "build_drill.sh" ldns)
exe_hr=$(built_or_system "$exe_hr" resolve "build_hr.sh")
2024-12-20 10:15:17 +04:00
echo
echo '==> Starting Unbound'
unbound -c ./unbound.conf -dp 2>unbound.log &
unbound_pid=$!
sleep 2
2024-12-20 10:15:17 +04:00
2024-12-20 19:01:38 +04:00
run_bench () {
hyperfine -N -w 30 \
"$exe_dig -p 2253 $1 $2 @127.0.0.1" \
"$exe_drill -p 2253 $1 $2 @127.0.0.1" \
"$exe_hr -n 127.0.0.1:2253 -t $1 $2"
}
2024-12-20 10:15:17 +04:00
echo
echo '==> Benchmarks for A domain.tld.'
run_bench A domain.tld
2024-12-20 10:15:17 +04:00
echo
echo '==> Benchmarks for AAAA domain.tld.'
run_bench AAAA domain.tld
2024-12-20 10:15:17 +04:00
echo
echo '==> Benchmarks for MX domain.tld.'
run_bench MX domain.tld
2024-12-20 10:15:17 +04:00
echo
echo '==> Benchmarks for CNAME mail.domain.tld.'
run_bench A mail.domain.tld
2024-12-20 19:01:38 +04:00
2024-12-21 11:22:15 +04:00
echo
echo '==> Benchmarks for TXT txt.domain.tld.'
run_bench TXT txt.domain.tld
2024-12-21 11:22:15 +04:00
2024-12-20 19:01:38 +04:00
echo
echo '==> All done'