Skip to content

Commit cc77129

Browse files
pingerr: Add cache warming before DNS tests
- Add warmup query before testing to prime DNS server cache - Use single domain (google.com by default) for all tests - Add -d/--domain CLI option to customize test domain - Remove unused TEST_DOMAINS array (15 domains) This measures cached DNS response time rather than recursive resolution time, giving results that correlate with actual network latency to the DNS server. Signed-off-by: Panchajanya1999 <kernel@panchajanya.dev>
1 parent 5f09fc3 commit cc77129

File tree

2 files changed

+50
-56
lines changed

2 files changed

+50
-56
lines changed

pingerr.sh

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ IPV6_ONLY=0
2626
SKIP_PING=0
2727
NO_COLOR=0
2828
TEST_COUNT=5
29+
TEST_DOMAIN="google.com"
2930

3031
# Help function
3132
show_help() {
@@ -38,6 +39,7 @@ Options:
3839
-4, --ipv4-only Test only IPv4 DNS servers
3940
-6, --ipv6-only Test only IPv6 DNS servers (requires IPv6 connectivity)
4041
-n, --count N Number of tests per server (default: 5)
42+
-d, --domain NAME Domain/IP to use for DNS queries (default: google.com)
4143
-q, --quick Quick mode (3 tests per server)
4244
--no-ping Skip ping correlation test
4345
--no-color Disable colored output
@@ -48,6 +50,7 @@ Examples:
4850
$(basename "$0") # Run full test (IPv4 + IPv6 if available)
4951
$(basename "$0") -4 # Test only IPv4 servers
5052
$(basename "$0") -q # Quick test with fewer iterations
53+
$(basename "$0") -d github.com # Use github.com for DNS queries
5154
$(basename "$0") --no-ping # Skip ping correlation analysis
5255
5356
EOF
@@ -84,6 +87,22 @@ while [ $# -gt 0 ]; do
8487
TEST_COUNT=3
8588
shift
8689
;;
90+
-d|--domain)
91+
if [ -n "$2" ]; then
92+
# Validate: must be a single domain/IP (no spaces)
93+
case "$2" in
94+
*" "*|*" "*)
95+
echo "Error: --domain requires a single domain or IP (no spaces)"
96+
exit 1
97+
;;
98+
esac
99+
TEST_DOMAIN=$2
100+
shift 2
101+
else
102+
echo "Error: --domain requires a domain name or IP address"
103+
exit 1
104+
fi
105+
;;
87106
--no-ping)
88107
SKIP_PING=1
89108
shift
@@ -129,25 +148,6 @@ else
129148
NC=$(printf '\033[0m') # No Color
130149
fi
131150

132-
# Test domains (popular sites for comprehensive testing)
133-
TEST_DOMAINS=(
134-
"google.com"
135-
"youtube.com"
136-
"facebook.com"
137-
"instagram.com"
138-
"chatgpt.com"
139-
"x.com"
140-
"whatsapp.com"
141-
"reddit.com"
142-
"wikipedia.org"
143-
"amazon.com"
144-
"tiktok.com"
145-
"pinterest.com"
146-
"cloudflare.com"
147-
"github.com"
148-
"netflix.com"
149-
)
150-
151151
# Check for IPv6 connectivity
152152
check_ipv6_connectivity() {
153153
echo -e "${CYAN}Checking IPv6 connectivity...${NC}"
@@ -568,7 +568,7 @@ echo ""
568568
echo -e "Total DNS Servers: ${total_servers} (IPv4: ${#DNS_NAMES_IPV4[@]}, IPv6: ${#DNS_NAMES_IPV6[@]})"
569569
echo -e "Tests per server: ${TEST_COUNT}"
570570
echo -e "Total tests to run: ${total_tests}"
571-
echo -e "Test domains: ${#TEST_DOMAINS[@]} popular websites"
571+
echo -e "Test domain: ${TEST_DOMAIN}"
572572
echo ""
573573
echo -e "${YELLOW}This will take a few minutes to complete...${NC}"
574574
echo ""
@@ -602,17 +602,16 @@ for i in "${!DNS_NAMES[@]}"; do
602602
# Progress indicator
603603
printf "[%3d/%3d] Testing %-35s (%s) ... \n" "$current" "$total" "$dns_name" "$dns_ip"
604604

605+
# Run a warmup query to prime the cache (prevents first-query bias)
606+
test_dns "$dns_ip" "$TEST_DOMAIN" >/dev/null
607+
605608
# Store results for this DNS server
606609
times=""
607610
failed=0
608611

609-
# Test multiple times with different domains
612+
# Test multiple times with the same domain (measures cached response time)
610613
for j in $(seq 1 $TEST_COUNT); do
611-
# Rotate through test domains
612-
domain_index=$(( (j - 1) % ${#TEST_DOMAINS[@]} ))
613-
domain="${TEST_DOMAINS[$domain_index]}"
614-
615-
response_time=$(test_dns "$dns_ip" "$domain")
614+
response_time=$(test_dns "$dns_ip" "$TEST_DOMAIN")
616615

617616
if [ "$response_time" == "0" ] || [ -z "$response_time" ]; then
618617
failed=$((failed + 1))

pingerr_ash.sh

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ IPV6_ONLY=0
2626
SKIP_PING=0
2727
NO_COLOR=0
2828
TEST_COUNT=5
29+
TEST_DOMAIN="google.com"
2930

3031
# Help function
3132
show_help() {
@@ -35,6 +36,7 @@ show_help() {
3536
printf ' -4, --ipv4-only Test only IPv4 DNS servers\n'
3637
printf ' -6, --ipv6-only Test only IPv6 DNS servers (requires IPv6 connectivity)\n'
3738
printf ' -n, --count N Number of tests per server (default: 5)\n'
39+
printf ' -d, --domain NAME Domain/IP to use for DNS queries (default: google.com)\n'
3840
printf ' -q, --quick Quick mode (3 tests per server)\n'
3941
printf ' --no-ping Skip ping correlation test\n'
4042
printf ' --no-color Disable colored output\n'
@@ -44,6 +46,7 @@ show_help() {
4446
printf ' %s # Run full test (IPv4 + IPv6 if available)\n' "$(basename "$0")"
4547
printf ' %s -4 # Test only IPv4 servers\n' "$(basename "$0")"
4648
printf ' %s -q # Quick test with fewer iterations\n' "$(basename "$0")"
49+
printf ' %s -d github.com # Use github.com for DNS queries\n' "$(basename "$0")"
4750
printf ' %s --no-ping # Skip ping correlation analysis\n\n' "$(basename "$0")"
4851
exit 0
4952
}
@@ -78,6 +81,22 @@ while [ $# -gt 0 ]; do
7881
TEST_COUNT=3
7982
shift
8083
;;
84+
-d|--domain)
85+
if [ -n "$2" ]; then
86+
# Validate: must be a single domain/IP (no spaces)
87+
case "$2" in
88+
*" "*|*" "*)
89+
printf 'Error: --domain requires a single domain or IP (no spaces)\n'
90+
exit 1
91+
;;
92+
esac
93+
TEST_DOMAIN=$2
94+
shift 2
95+
else
96+
printf 'Error: --domain requires a domain name or IP address\n'
97+
exit 1
98+
fi
99+
;;
81100
--no-ping)
82101
SKIP_PING=1
83102
shift
@@ -123,15 +142,6 @@ else
123142
NC=$(printf '\033[0m') # No Color
124143
fi
125144

126-
# Test domains (popular sites for comprehensive testing)
127-
TEST_DOMAINS="google.com youtube.com facebook.com instagram.com chatgpt.com x.com whatsapp.com reddit.com wikipedia.org amazon.com tiktok.com pinterest.com cloudflare.com github.com netflix.com"
128-
129-
# Convert to list for counting
130-
# shellcheck disable=SC2086
131-
# Note: $TEST_DOMAINS intentionally unquoted to split into array
132-
set -- $TEST_DOMAINS
133-
TEST_DOMAIN_COUNT=$#
134-
135145
# Check for IPv6 connectivity
136146
check_ipv6_connectivity() {
137147
printf '%bChecking IPv6 connectivity...%b\n' "${CYAN}" "${NC}"
@@ -355,20 +365,6 @@ count_dns_servers() {
355365
echo "$DNS_SERVERS" | grep -c -v "^$"
356366
}
357367

358-
# Function to get nth test domain
359-
get_test_domain() {
360-
local index=$1
361-
local count=0
362-
for domain in $TEST_DOMAINS; do
363-
count=$((count + 1))
364-
if [ $count -eq "$index" ]; then
365-
echo "$domain"
366-
return
367-
fi
368-
done
369-
echo "google.com" # fallback
370-
}
371-
372368
# Function to test DNS response time
373369
test_dns() {
374370
local dns_server=$1
@@ -485,7 +481,7 @@ echo ""
485481
printf 'Total DNS Servers: %s (IPv4: %s, IPv6: %s)\n' "${DNS_COUNT}" "${DNS_IPV4_COUNT}" "${DNS_IPV6_COUNT}"
486482
printf 'Tests per server: %s\n' "${TEST_COUNT}"
487483
printf 'Total tests to run: %s\n' "${total_tests}"
488-
printf 'Test domains: %s popular websites\n' "${TEST_DOMAIN_COUNT}"
484+
printf 'Test domain: %s\n' "${TEST_DOMAIN}"
489485
echo ""
490486
printf '%bThis will take a few minutes to complete...%s\n' "${YELLOW}" "${NC}"
491487
echo ""
@@ -525,17 +521,16 @@ echo "$DNS_SERVERS" | grep -v "^$" | while IFS='|' read -r dns_name dns_ip; do
525521
printf "[%3d/%3d] Testing %-35s (%s) ... \n" "$current" "$total" "$dns_name" "$dns_ip"
526522

527523
# Store results for this DNS server
524+
# Run a warmup query to prime the cache (prevents first-query bias)
525+
test_dns "$dns_ip" "$TEST_DOMAIN" >/dev/null
526+
528527
times=""
529528
failed=0
530529

531-
# Test multiple times with different domains
530+
# Test multiple times with the same domain (measures cached response time)
532531
i=1
533532
while [ $i -le $TEST_COUNT ]; do
534-
# Rotate through test domains
535-
domain_index=$(( (i - 1) % TEST_DOMAIN_COUNT + 1 ))
536-
domain=$(get_test_domain $domain_index)
537-
538-
response_time=$(test_dns "$dns_ip" "$domain")
533+
response_time=$(test_dns "$dns_ip" "$TEST_DOMAIN")
539534

540535
if [ "$response_time" = "0" ] || [ -z "$response_time" ]; then
541536
failed=$((failed + 1))

0 commit comments

Comments
 (0)