Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions dnstest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ command -v bc > /dev/null || { echo "bc was not found. Please install bc."; exit
{ command -v drill > /dev/null && dig=drill; } || { command -v dig > /dev/null && dig=dig; } || { echo "dig was not found. Please install dnsutils."; exit 1; }



# Local DNS resolvers
NAMESERVERS=`cat /etc/resolv.conf | grep ^nameserver | cut -d " " -f 2 | sed 's/\(.*\)/&#&/'`

# Upstream DNS resolvers
# Non-standard ports may be specified e.g. 127.0.0.1:5353#mydns
PROVIDERS="
1.1.1.1#cloudflare
4.2.2.1#level3
Expand All @@ -22,42 +24,65 @@ PROVIDERS="
8.26.56.26#comodo
"

# Domains to test. Duplicated domains are ok
DOMAINS2TEST="www.google.com amazon.com facebook.com www.youtube.com www.reddit.com wikipedia.org twitter.com gmail.com www.google.com whatsapp.com"
# Number of domains to test
NUM_DOMAINS2TEST=10

# Random domains to choose from
RANDOM_DOMAINS=(
`curl -sS https://raw.githubusercontent.com/opendns/public-domain-lists/master/opendns-top-domains.txt`
`curl -sS https://raw.githubusercontent.com/opendns/public-domain-lists/master/opendns-random-domains.txt`
)

heading="DOMAINS TO TEST: "; echo -n "$heading"
results_indent=$((${#heading} - 3))
results_tempfile=`mktemp`
domains2test=""
num_random_domains=${#RANDOM_DOMAINS[*]}

totaldomains=0
printf "%-18s" ""
for d in $DOMAINS2TEST; do
totaldomains=$((totaldomains + 1))
printf "%-8s" "test$totaldomains"
for ((i=1; i <= $NUM_DOMAINS2TEST; i++)); do
if [ $i -gt 1 ]; then
printf "%-${#heading}s" ""
fi

domain_id=`printf "%5s" "($i) "`; echo -n "$domain_id"
domain_heading=" $domain_id"
results_header="$results_header$domain_heading"
random_domain=${RANDOM_DOMAINS[$RANDOM % num_random_domains]}; echo $random_domain
domains2test="$domains2test $random_domain"
done
printf "%-8s" "Average"
echo ""

avg_heading=" AVERAGE"
results_header="$results_header$avg_heading"
printf "\n%-${results_indent}s" ""
echo "$results_header"

for p in $NAMESERVERS $PROVIDERS; do
pip=${p%%#*}
pname=${p##*#}
ftime=0

printf "%-18s" "$pname"
for d in $DOMAINS2TEST; do
ttime=`$dig +tries=1 +time=2 +stats @$pip $d |grep "Query time:" | cut -d : -f 2- | cut -d " " -f 2`
printf "%-${results_indent}s" "$pname" | tee -a $results_tempfile
for d in $domains2test; do
ttime=`$dig +tries=1 +time=2 +stats @${pip/:/" -p"} $d | grep "Query time:" | cut -d : -f 2- | cut -d " " -f 2`
if [ -z "$ttime" ]; then
#let's have time out be 1s = 1000ms
ttime=1000
elif [ "x$ttime" = "x0" ]; then
ttime=1
fi

printf "%-8s" "$ttime ms"
printf "%${#domain_heading}s" "$ttime ms" | tee -a $results_tempfile
ftime=$((ftime + ttime))
done
avg=`bc -lq <<< "scale=2; $ftime/$totaldomains"`
avg=`bc -lq <<< "scale=2; $ftime/$NUM_DOMAINS2TEST"`

echo " $avg"
printf "%${#avg_heading}s\n" $avg | tee -a $results_tempfile
done

printf "\n%-${results_indent}s" "SORTED BY AVG."
echo "$results_header"
sort -g -k$((2*$NUM_DOMAINS2TEST + 2)) $results_tempfile
rm $results_tempfile


exit 0;