Created
August 7, 2012 16:41
-
-
Save jelder/3287153 to your computer and use it in GitHub Desktop.
Ping a URL and report 95th percentile timings
This file contains hidden or 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
gem 'smart_colored', :require => 'smart_colored/extend' | |
gem 'rest-client' |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bundler' | |
Bundler.setup | |
Bundler.require | |
require 'pp' | |
interval = 1.0 | |
url = ARGV.pop | |
@format = "%.4f" | |
@timings = [] | |
def stats | |
STDERR.puts <<-EOF | |
95%: #{ @format % (@timings.sort[((@timings.count * 0.95).ceil)-1]) } | |
Avg: #{ @format % (@timings.inject(:+) / @timings.count) } | |
EOF | |
end | |
Signal.trap("INT") do | |
stats | |
exit | |
end | |
Thread.new do | |
loop do | |
sleep 30 | |
@timings = @timings.pop(1000) | |
stats | |
end | |
end | |
loop do | |
start = Time.now | |
response = RestClient.get(url){|response, request, result| response } | |
latency = Time.now - start | |
@timings << latency | |
graph = " ".inverse * Math.log(latency * 100) | |
code = case response.code.to_i | |
when 0 | |
graph = graph.red | |
response.code.to_s.red.on_white | |
when 100..400 | |
response.code.to_s.green_bold | |
when 400..500 | |
graph = graph.blue | |
response.code.to_s.blue_bold | |
when 500...600 | |
graph = graph.red | |
response.code.to_s.red.on_white | |
else | |
response.code.to_s.green_bold | |
end | |
puts [ @format % latency, code, graph ].join " " | |
if interval > latency | |
sleep interval - latency | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment