Skip to content

Instantly share code, notes, and snippets.

@jelder
Created August 7, 2012 16:41
Show Gist options
  • Save jelder/3287153 to your computer and use it in GitHub Desktop.
Save jelder/3287153 to your computer and use it in GitHub Desktop.
Ping a URL and report 95th percentile timings
gem 'smart_colored', :require => 'smart_colored/extend'
gem 'rest-client'
#!/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