Skip to content

Instantly share code, notes, and snippets.

@undecided

undecided/gist:4554774

Last active Dec 11, 2015
Embed
What would you like to do?
Load tester
#!/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
@undecided

This comment has been minimized.

Copy link
Owner Author

@undecided undecided commented Jan 17, 2013

Usage instructions:

  1. Edit lines 5-6 to add URLs to test. Note that this currently doesn't test POSTs does test posts, not tested it though
  2. The value on line 36 may be tweaked - bear in mind there are limits to the number of connections you are allowed simultaneously. Feel free to add duplicate records in the array from point 1)
  3. Warm up your servers - the first time you run this script may take longer depending on server config / most recent caches
  4. Try it from different locations over different network connections, to ensure you aren't the victim of throttling etc
  5. This will not check whether the return was valid. Your server may have gone down and be returning 500 errors - we do not check for this (yet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.