Created
September 3, 2008 04:45
-
-
Save radamant/8542 to your computer and use it in GitHub Desktop.
Simple Web Benchmarking
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
w = Webench.new "localhost", 3000 | |
# authenticate / create session | |
w.init_steps << Webench::Step.new do |http, cookie| | |
http.post('/account/login', 'login=username&password=password') | |
end | |
# main test steps | |
w.steps << Webench::Step.new do |http, cookie| | |
http.get('/articles/1',{'COOKIE' => cookie}) | |
end | |
w.run(5) |
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
Run 1 took 1.86858 second(s) | |
Run 2 took 3.04381 second(s) | |
Run 3 took 2.25652 second(s) | |
Run 4 took 3.13171 second(s) | |
Run 5 took 2.53058 second(s) | |
Average time: 2.56624 second(s) |
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
class Webench | |
attr_accessor :init_steps | |
attr_accessor :steps | |
def initialize host, port | |
@host = host | |
@port = port | |
@steps = [] | |
@init_steps = [] | |
end | |
class Step | |
def initialize &block | |
@block = block | |
end | |
def run http, cookie = nil | |
@block.call http, cookie | |
end | |
end | |
def run num_runs | |
marks = [] | |
res = Net::HTTP.start(@host, @port) do |http| | |
@init_steps.each do |step| | |
result = step.run http | |
# todo: this is a bit hokey, need a better way to set the auth cookie | |
@cookie = result.header["set-cookie"] if result.header["set-cookie"] | |
end | |
num_runs.times do | |
benchmark = Benchmark.realtime do | |
@steps.each do |step| | |
step.run http, @cookie | |
end | |
end | |
marks << benchmark | |
sleep 1/100 | |
end | |
print_report marks, num_runs | |
end | |
end | |
def print_report marks, num_runs | |
avg_time = 0 | |
marks.each_with_index do |benchmark, i| | |
avg_time = avg_time + benchmark | |
puts "Run #{i + 1} took " + sprintf("%.5f", benchmark) + " second(s)" | |
end | |
avg_time = avg_time / num_runs | |
puts "Average time: " + sprintf("%.5f", avg_time) + " second(s)" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment