Skip to content

Instantly share code, notes, and snippets.

@vigo vigo/stress_test.rb
Created Feb 9, 2012

Embed
What would you like to do?
Mini Url checker tool
#!/usr/bin/env ruby
# encoding: utf-8
# Uğur Özyılmazel, ugur@ozyilmazel.com @vigobronx, @ugurozyilmazel
require 'optparse'
require 'open-uri'
require 'uri'
$VERSION = 1.1
# terminal colors...
$RESET = "\e[0m" # reset
$BLACK = "\e[0;30m"
$RED = "\e[0;31m"
$GREEN = "\e[0;32m"
$BROWN = "\e[0;33m"
$BLUE = "\e[0;34m"
$PURPLE = "\e[0;35m"
$CYAN = "\e[0;36m"
$LIGHT_GREY = "\e[0;37m"
if $0 == __FILE__
options = {}
# defaults
options[:loop_count] = 100
options[:interval] = 5
opt_parser = OptionParser.new do |opt|
opt.banner = "Usage: #{$0} [URL] [OPTIONS]"
opt.separator ""
opt.separator "URL: Must be a valid url and must started with http://"
opt.separator ""
opt.separator "Options"
opt.separator "---------------------------------------------------------"
opt.separator ""
opt.on("-V","Version Information") do
puts "#{$VERSION}"
exit
end
opt.on("-l","--loop LOOP_COUNT","Iteration amount. Default is #{options[:loop_count]} times.") do |loop_count|
options[:loop_count] = loop_count
end
opt.on("-i","--interval SECONDS","Call url in every XX Seconds. Default is #{options[:interval]} seconds.") do |interval|
options[:interval] = interval
end
# opt.on("-u","--url URL","Website address") do |url|
# options[:url] = url
# end
opt.on("-n","--no-caching","Enable anti-caching. Add timestap at the end of url (http://example.com?1328790164)") do
options[:no_cache] = true
end
opt.on("-v","--verbose","Verbose mode") do
options[:verbose] = true
end
opt.on("-h","--help","help") do
puts opt_parser
puts ""
exit
end
end
opt_parser.parse!
url = ARGV[0]
if options[:no_cache] then url = "%s?%s" % [url, Time.now.to_i] end
loop_count = options[:loop_count].to_i
interval = options[:interval].to_i
total_seconds = []
error_count = 0
unless (url =~ URI::regexp).nil?
loop_count.times{ |i|
time_start = Time.now
print "Fetching..."
begin
fetched_url = open(url)
rescue SocketError
puts "#{$RED}Site is not responding#{$RESET}..."
error_count += 1
end
time_end = Time.now
time_diff = time_end - time_start
total_seconds.push(time_diff)
if options[:verbose] then print "Started at #{$CYAN}#{time_start.strftime('%H:%M:%S')}#{$RESET}, " end
print "#{i+1} completed. (#{$PURPLE}#{time_diff}#{$RESET} seconds)."
if options[:verbose] then print " Endend at #{$CYAN}#{time_end.strftime('%H:%M:%S')}#{$RESET}." end
print "\n"
if options[:verbose]
html = fetched_url.read.split("\n")
html.each_with_index { |text, index| html[index] = text.strip }
html.reject! { |item| item.empty? } # clear empty lines...
puts "Random line from grabbed html:"
puts "\t#{$BLUE}#{html[rand(html.size)]}#{$RESET}\n"
end
sleep(interval)
}
total_time = 0
total_seconds.each{ |t| total_time+= t }
puts ""
puts "#{$BROWN}#{url}#{$RESET} has been called #{$PURPLE}#{loop_count}#{$RESET} times in #{$PURPLE}#{interval}#{$RESET} seconds interval."
puts "Avarage response is #{$RED}#{total_time/(loop_count*1.0)}#{$RESET} seconds..."
if error_count > 0 then puts "Site did not responded #{$RED}#{error_count}#{$RESET} time(s)" end
puts ""
else
puts "Sorry, please enter valid url... Maybe http:// is missing"
end
end
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.