Skip to content

Instantly share code, notes, and snippets.

@jschoolcraft
Forked from masaki/gist:329030
Created July 20, 2011 10:13
Show Gist options
  • Save jschoolcraft/1094715 to your computer and use it in GitHub Desktop.
Save jschoolcraft/1094715 to your computer and use it in GitHub Desktop.
OO-ish ab (apache bench)
#!/usr/bin/ruby
require 'stringio'
module AB
class Response
class << self
def parse_string(string)
new.tap {|res| res.parse_string(string) }
end
end
def initialize
@params = {}
end
def keys
@params.keys
end
def has_key?(key)
keys.include? key.to_sym
end
def method_missing(name, *args)
if has_key? name
@params[name.to_sym]
end
end
def parse_string(string)
stream = StringIO.new(string)
1..3.times { stream.gets } # skip copyrights
while line = stream.gets
line.chomp!
next if line.empty?
if /^Benchmarking/ =~ line
1 until (tmp = stream.gets).chomp.empty? # skip benchmarking
elsif /^Connection Times/ =~ line
break # TODO: connect/processing/waiting/total
else
key, value = line.split(/\s*:\s*/)
@params[key.downcase.tr_s(' -', '_').to_sym] = value
end
end
end
end
end
if __FILE__ == $0
res = AB::Response.parse_string(`ab http://localhost/`)
p "keys: #{res.keys.join(', ')}"
p "Server Software: #{res.server_software}"
p "Server Hostname: #{res.server_hostname}"
p "Total transferred: #{res.total_transferred}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment