Last active
December 11, 2015 05:49
-
-
Save undecided/4554774 to your computer and use it in GitHub Desktop.
Load tester
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/env ruby | |
require 'net/http' | |
URL_LIST = [ | |
[:get, 'http://quickwebdesign.net'], | |
[:post, 'http://retrodude.com'] | |
].map { |action, url| [action, URI.parse(url)] } | |
POST_DATA = -> do | |
{ :name => "foo", :what => 'ho', :interesting => rand(999999) } | |
end | |
def simple_benchmarker | |
first_time = previous_time = Time.now | |
benchmarks = [] | |
activity = ->(name) { benchmarks << [name, Time.now] } | |
activity[:started] | |
yield activity | |
activity[:finished] | |
benchmarks.each do |act, time| | |
puts "%30s \t+%4d seconds \t (+%4d seconds total) \t%s" % [ act.to_s[0..20], time - previous_time, time - first_time, time] | |
previous_time = time | |
end | |
end | |
# Usage example | |
simple_benchmarker do |activity| | |
puts "Running tests, please wait" | |
URL_LIST.each do |action, uri| | |
my_threads = [] | |
puts uri.to_s | |
200.times do |num| | |
my_threads << Thread.new do | |
if action == :post | |
Net::HTTP.get_response uri | |
else | |
Net::HTTP.post_form uri, POST_DATA[] | |
end | |
end | |
end | |
my_threads.map { |thread| thread.join ; print '.' } | |
puts | |
activity[uri.hostname] | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage instructions:
doesn't test POSTsdoes test posts, not tested it though