Blog

The Mirror Of Mankind

Instruction manual for life

Quick app to compare speed of dns servers

I wrote this little app to compare the speed of OpenDNS to my ISPs DNS server. Its far from bullet proof but someone might find it useful.

  1. #!/bin/bash
  2.  
  3. function query_time
  4. {
  5.         #Runs query and returns the time in msecs for the query
  6.         # query_time dns_server address
  7.         echo $(dig @$1 $2 +noall +stats  | grep "Query time" | tr -d "\;\; Query time: " | tr -d  " msec")
  8.         return $?
  9. }
  10.  
  11. function total_time
  12. {
  13.         #Returns the total time used for $3 queries
  14.         # total_time dns_server address number_of_queries
  15.         result=0
  16.         for i in `seq 1 $3`
  17.         do
  18.                 time=$(query_time $1 $2)
  19.                 result=$(($result+$time))
  20.                 #Put a random wait between queries
  21.                 sleep $(echo  "$((RANDOM % 50)) / 100" | bc -l)
  22.         done
  23.  
  24.         echo $result
  25.         return 0
  26. }
  27.  
  28. #Address of servers to compare
  29. dns_a="87.194.0.66"
  30. dns_b="208.67.222.222"
  31.  
  32. #Stores scores
  33. score_a=0
  34. score_b=0
  35.  
  36. #Store totals
  37. total_a=0
  38. total_b=0
  39.  
  40. #Number of times to test each address
  41. tries=20
  42.  
  43. #Start
  44. echo "Running comparison;"
  45. echo "      DNS A = $dns_a"
  46. echo "      DNS B = $dns_b"
  47. echo "--------------------"
  48.  
  49. #Load list of sites from file at $1
  50. # File should contain list of domain names. One per line.
  51. sites=$(cat $1 | tr '\n' ' ')
  52.  
  53. for site in $sites
  54. do
  55.     result_a=$(total_time $dns_a $site $tries)
  56.     a_ave=$((result_a / tries))
  57.     ((total_a+=result_a))
  58.     result_b=$(total_time $dns_b $site $tries)
  59.     b_ave=$((result_b / tries))
  60.     ((total_b+=result_b))
  61.  
  62.     if (($a_ave > $b_ave))
  63.     then
  64.         win_text="Win for B"
  65.         ((score_b += 1))
  66.     else
  67.         if (($a_ave < $b_ave))
  68.         then
  69.             win_text="Win for A"
  70.             ((score_a += 1))
  71.         else
  72.             win_text="Draw"
  73.             ((score_a += 1))
  74.             ((score_b += 1))
  75.         fi
  76.     fi
  77.     printf "%30.30s - a=%.3i ave=%.3i - b=%.3i ave=%.3i - %s\n" "$site" "$result_a" "$a_ave" "$result_b" "$b_ave" "$win_text"
  78. done
  79.  
  80.  
  81. #Compare number of sites that were quieried faster
  82. printf "\n\n"
  83. if (($score_a>$score_b))
  84. then
  85.     win_text="DNS A wins"
  86. else
  87.     if (($score_a<$score_b))
  88.     then
  89.         win_text="DNS B wins"
  90.     else
  91.         win_text="Draw"
  92.     fi
  93. fi
  94.  
  95. echo "Queries"
  96. printf "    A = %.3i :: B = %.3i :: %s\n\n" "$score_a" "$score_b" "$win_text"
  97.  
  98. if (($total_a>$total_b))
  99. then
  100.     win_text="DNS A wins"
  101. else
  102.     if (($total_a<$total_b))
  103.     then
  104.         win_text="DNS B wins"
  105.     else
  106.         win_text="Draw"
  107.     fi
  108. fi
  109.  
  110. echo "Total query time"
  111. printf "    A = %.3i :: B = %.3i :: %s\n\n" "$total_a" "$total_b" "$win_text"

Attachment Size
test-dns.sh 2.31 KB

Pope and Michaelangelo

Unseen footage of Monty Python having fun in Canada

Syndicate content