Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created April 16, 2012 18:08
Show Gist options
  • Save timuruski/2400422 to your computer and use it in GitHub Desktop.
Save timuruski/2400422 to your computer and use it in GitHub Desktop.
Use siege to benchmark specific routes in your app.
#!/usr/bin/env ruby
if ARGV.empty?
warn "Need a list of URLs to benchmark"
exit 1
end
class Siege
REQUESTS = 10
CONCURRENCY = 1
attr_reader :url
def initialize(url)
@url = url
end
def attack!
parse_results `siege -c#{CONCURRENCY} -r#{REQUESTS} \"#{url}\" 2>&1`
print_result
STDOUT.flush
end
# ** SIEGE 2.71
# ** Preparing 1 concurrent users for battle.
# The server is now under siege.. done.
#
# Transactions: 4 hits
# Availability: 100.00 %
# Elapsed time: 2.38 secs
# Data transferred: 0.02 MB
# Response time: 0.60 secs
# Transaction rate: 1.68 trans/sec
# Throughput: 0.01 MB/sec
# Concurrency: 1.00
# Successful transactions: 4
# Failed transactions: 0
# Longest transaction: 0.60
# Shortest transaction: 0.59
def parse_results(results)
@results = results.lines
@transactions = parse_value('Transactions').to_i
@average_response = parse_value('Response time').to_f
@longest_response = parse_value('Longest transaction').to_f
@shortest_response = parse_value('Shortest transaction').to_f
@successes = parse_value('Successful transactions').to_i
@failures = parse_value('Failed transactions').to_i
end
def parse_value(label)
@results.grep(%r{^#{label}:}).first.split(':')[1].strip
end
def print_result
puts "#{url},#{@average_response},#{@shortest_response},#{@longest_response},#{@transactions},#{@successes},#{@failures}"
end
end
# Chop a file into urls
def urls_to_array(text)
text.lines.reject { |line| line.start_with?('#') }.map(&:chomp)
end
# Print CSV header
puts %w[url avg min max reqs succ fail].join(',')
urls_to_array(ARGF).each { |url| Siege.new(url).attack! }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment