Skip to content

Instantly share code, notes, and snippets.

@stulentsev
Created October 23, 2012 06:46
Show Gist options
  • Save stulentsev/3937284 to your computer and use it in GitHub Desktop.
Save stulentsev/3937284 to your computer and use it in GitHub Desktop.
DNS resolver / speed checker for reg.ru
# Example output:
#
# Resolved IP addresses for www.reg.ru:
#     31.31.204.42
#     31.31.204.21
#     31.31.205.41
#
# Loading from 31.31.204.42 took 0.370469 seconds
# Loading from 31.31.204.21 took 1.037444 seconds
# Loading from 31.31.205.41 took 1.38692 seconds
#
# IP address 31.31.204.42 was the fastest (0.370469 secs)
#! /usr/bin/env ruby
require 'resolv'
require 'net/http'
require 'benchmark'
host = 'www.reg.ru'
ipaddrs = Resolv.getaddresses(host)
puts "Resolved IP addresses for #{host}:"
ipaddrs.each do |ip|
puts " #{ip}"
end
puts ''
load_times = {}
ipaddrs.each do |ip|
uri = URI("http://#{ip}")
req = Net::HTTP::Get.new(uri.request_uri)
req['Host'] = host
# proxy_addr = 'localhost'
# proxy_port = 8888
#
load_times[ip] = Benchmark.realtime do
# Net::HTTP::Proxy(proxy_addr, proxy_port).start(uri.hostname, uri.port) {|http|
Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
end
puts "Loading from #{ip} took #{load_times[ip]} seconds"
end
puts ''
inv = load_times.invert
fastest_time = inv.keys.min
fastest_ip = inv[fastest_time]
puts "IP address #{fastest_ip} was the fastest (#{fastest_time} secs)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment