Skip to content

Instantly share code, notes, and snippets.

@znorris
Forked from mcrumm/Gemfile
Last active April 14, 2016 22:24
Show Gist options
  • Save znorris/f933c3c7a5e5705af872 to your computer and use it in GitHub Desktop.
Save znorris/f933c3c7a5e5705af872 to your computer and use it in GitHub Desktop.
Sloth, gentle load testing

Sloth: Gentle load testing

Install

$ bundle install
$ chmod +x ./sloth.rb
$ ./sloth.rb <SIEGE_FILE> <AUTH_USER> <AUTH_PASS> <TRANSACTIONS_PER_SECOND> <RUNTIME_IN_SECONDS>
source 'https://rubygems.org'
gem 'httparty'
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'httparty'
file = ARGV[0] # Siege File Path
user = ARGV[1] # HTTP Basic User
pass = ARGV[2] # HTTP Basic Pass
tps = ARGV[3].to_f # Transactions per second
runtime = ARGV[4].to_i # Runtime, in seconds
lines = [] # Lines for siege file
tts = 1.0 / tps # Calculate time to sleep once
ttc = 0 # total transactions counter
# Add lines from siege files into lines array.
File.open(file) do |f|
f.each_line do |line|
lines.push line
end
end
now = Time.now
end_time = now + runtime
threads = [] # threads for POSTs
# Loop until we pass the end_time.
while Time.now < end_time do
# Get a random line from the siege file.
line = lines.sample
data = line.split(' ')
# Generate the POST data
options = { body: data[2], basic_auth: { username: user, password: pass } }
# Each POST goes it its own thread so we don't affect timing.
threads << Thread.new do
response = HTTParty.post(data[0], options)
puts Time.now.to_s << ' ' << response.code.to_s << ' ' << response.message
end
# Total Transactions Counter
ttc += 1
# check how long we are sleeping for
# b = Process.clock_gettime(Process::CLOCK_MONOTONIC); sleep (tts); p Process::clock_gettime(Process::CLOCK_MONOTONIC)-b
sleep(tts)
end
puts ttc.to_s # Total transactions, sanity check
threads.each{|t| t.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment