Skip to content

Instantly share code, notes, and snippets.

@pzb
Created August 2, 2018 05:47
Show Gist options
  • Save pzb/185b13bedbd248b55937ec2a883e552c to your computer and use it in GitHub Desktop.
Save pzb/185b13bedbd248b55937ec2a883e552c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
require 'net/http'
require 'uri'
require 'socket'
# First grab the reachability page and parse it
uri = URI.parse('http://ec2-reachability.amazonaws.com/')
r = Net::HTTP.get_response(uri)
doc = Nokogiri::HTML(r.body)
cur_region = nil
cur_ips = []
ips = {}
# A few heuristics are needed to get the data
# into a format we want
doc.css('table').each do |t|
t.css('tr').each do |row|
first = row.first_element_child
if first['class'] == 'region-heading'
if !cur_region.nil?
ips[cur_region] = cur_ips
end
cur_region = first.text
cur_ips = []
else
ip_elem = row.css('td')[2]
cur_ips << ip_elem.text unless ip_elem.nil?
end
end
end
def time_tcp(host, port = 80)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
s = TCPSocket.new(host, port)
stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
s.close
(stop - start)*1000
end
ips.each do |region, ip_list|
times = []
ip_list.each do |ip|
times << time_tcp(ip)
end
puts "#{region} average: #{(times.inject(:+)/times.length).round(2)} ms"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment