Created
May 25, 2011 23:03
-
-
Save vvuksan/992206 to your computer and use it in GitHub Desktop.
URL Metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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