Created
March 11, 2010 10:44
-
-
Save masaki/329030 to your computer and use it in GitHub Desktop.
OO-ish ab (apache bench)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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