Skip to content

Instantly share code, notes, and snippets.

@araipiyo
Created Jun 1, 2014
Embed
What would you like to do?
measure speed of HTTP GET at any particular site.
require 'net/http'
require 'uri'
url = URI.parse(ARGV[0])
http = Net::HTTP.new(url.host, url.port)
path = url.path != '' ? url.path : '/'
path += url.query ? "?#{url.query}" : ""
secs = []
http.get(path) # drop first access
10.times { |i|
start = Time.now
result = http.get(path)
finish = Time.now
secs << finish - start
unless result.code == "200"
p "error_at #{i}"
p result.code
p result.body
exit
end
}
puts secs
sum = secs.inject(0,&:+)
avg = sum / 10
var = secs.map { |i| (i - avg) ** 2 }.inject(0,&:+) / 9
stddev = Math.sqrt(var)
puts "average: #{avg}"
puts "standard deviation: #{stddev}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment