Skip to content

Instantly share code, notes, and snippets.

@cdgraff
Forked from vvuksan/gist:992206
Created October 31, 2013 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdgraff/7258882 to your computer and use it in GitHub Desktop.
Save cdgraff/7258882 to your computer and use it in GitHub Desktop.
# Based on https://github.com/ripienaar/mcollective-plugins/blob/master/agent/urltest/urltest.rb
require 'net/http'
require 'socket'
req_url = "http://www.google.com"
url = URI.parse(req_url)
times = {}
if url.scheme == "http"
times["beforedns"] = Time.now
name = TCPSocket.gethostbyname(url.host)
times["afterdns"] = Time.now
times["beforeopen"] = Time.now
socket = TCPSocket.open(url.host, url.port)
times["afteropen"] = Time.now
socket.print("GET #{url.request_uri} HTTP/1.1\r\nHost: #{url.host}\r\nUser-Agent: Webtester\r\nAccept: */*\r\nConnection: close\r\n\r\n")
times["afterrequest"] = Time.now
response = Array.new
while line = socket.gets
times["firstline"] = Time.now unless times.include?("firstline")
response << line
end
socket.close
times["end"] = Time.now
lookuptime = times["afterdns"] - times["beforedns"]
connectime = times["afteropen"] - times["beforeopen"]
prexfertime = times["firstline"] - times["afteropen"]
startxfer = times["firstline"] - times["afterrequest"]
bytesfetched = response.join.length
totaltime = times["end"] - times["beforedns"]
puts "DNS Lookup Time = #{lookuptime}"
puts "Time to connect = #{connectime}"
puts "Time to send request = #{prexfertime}"
puts "Time to fetch response = #{startxfer}"
puts "Total time = #{totaltime}"
else
reply.fail "Unsupported url scheme: #{url.scheme}"
return
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment