Skip to content

Instantly share code, notes, and snippets.

@barbuza
Created February 1, 2010 08:20
Show Gist options
  • Save barbuza/291530 to your computer and use it in GitHub Desktop.
Save barbuza/291530 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'optparse'
require 'URI'
require 'Time'
require 'eventmachine'
@fails = 0
@requests = 0
@request_timings = []
@options = { :requests => 100, :concurrency => 10 }
@uri = URI.parse ARGV[-1]
OptionParser.new do |opts|
opts.banner = 'Usage: ruby ab.rb [OPTIONS] URI'
opts.on('-n requests', Integer,
'Number of requests to perform') { |n| @options[:requests] = n }
opts.on('-c concurrency', Integer,
'Number of multiple requests to make') { |c| @options[:concurrency] = c }
end.parse!
EventMachine.run do
def make_request
EventMachine.stop if @requests >= @options[:requests]
start_time = Time.now.to_f
http = EventMachine::Protocols::HttpClient.request(
:host => @uri.host, :port => @uri.port,
:request => @uri.path, :query_string => @uri.query
)
http.callback do |response|
http.unbind
@request_timings.push Time.now.to_f - start_time
@requests += 1
@fails += 1 if response[:status] != 200
make_request
end
end
@options[:concurrency].times do
make_request
end
end
average_time = @request_timings.inject { |a, b| a + b }
rps = (@options[:concurrency] / (average_time / @request_timings.length)).to_i
puts "#{@requests} requests, #{@fails} fails"
puts "#{rps} rps"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment