Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created September 8, 2009 01:17
Show Gist options
  • Select an option

  • Save rbranson/182650 to your computer and use it in GitHub Desktop.

Select an option

Save rbranson/182650 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# benshee.rb: A multi-connection HTTP request generator
# (C) 2009 Rick Branson -- actually it's public domain.
#
require "rubygems"
require "eventmachine"
require "uri"
# MonkeyPatch EM to let us hit that instance variable
EventMachine::Protocols::HttpClient.class_eval do
attr_reader :start_time
end
if ARGV.size < 3
puts "benshee.rb [url] [clients] [hits]"
exit
end
$url, $clients, $hits = ARGV[0], ARGV[1].to_i, ARGV[2].to_i
raise "Clients must be at least 1" if $clients < 1
raise "Hits must be at least 1" if $hits < 1
$uri = URI.parse($url)
$initiated = 0
$completed = 0
$in_flight = 0
$times = []
$responses = {}
started_at = Time.now.to_f
begin
EM.run do
# added for the linux/bsd cats.
EM.epoll
EM.kqueue
# we're doing this because this is "asynchronously" recursive
fun = Proc.new do
h = EM::Protocols::HttpClient.request(
:host => $uri.host,
:port => $uri.port,
:request => $uri.path
)
$in_flight += 1
$initiated += 1
print "."
STDOUT.flush
h.callback do |r|
$in_flight -= 1
$completed += 1
$responses[r[:status]] ||= 0
$responses[r[:status]] += 1
$times.push(Time.now.to_f - h.start_time.to_f)
if $completed >= $hits
EM.stop_event_loop
elsif $initiated < $hits
# call the enclosing proc again, completing our "asynchronous" recursion loop
fun.call
end
end
end
while $in_flight < $clients and $initiated < $hits
fun.call
end
end
end
total_time = Time.now.to_f - started_at
responses_s = $responses.map { |k, v| "#{k}: #{v}" }.join(", ")
puts ""
puts "Requests:\t#{$hits}"
puts "Time:\t\t#{total_time}s"
puts "Responses:\t#{responses_s}"
puts "Rate:\t\t#{$hits / total_time} request/sec"
puts ""
puts "Min: #{$times.min}s"
puts "Max: #{$times.max}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment