-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-test
executable file
·56 lines (42 loc) · 1.44 KB
/
load-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: example.rb [options]'
opts.separator ''
opts.on('-u', '--url URL', 'required, the URL to load test') do |url|
options[:url] = url
end
opts.on('-t', '--times TIMES', 'the number of times to curl the url') do |times|
options[:times] = times.to_i
end
opts.on('-s', '--sleep SLEEP', 'the time in seconds (can be fractional) to sleep between requests') do |sleep|
options[:sleep] = sleep.to_f
end
opts.on('-h', '--help', 'prints this help message') do
puts opts
exit
end
opts.on('-v', '--verbose', 'run in verbose mode') do |verbose|
options[:verbose] = verbose || false
end
end.parse!
raise OptionParser::MissingArgument, 'No URL specified to load test' unless options.key? :url || !options[:url].empty?
options[:times] = 1000 unless options.key? :times
options[:sleep] = 0.25 unless options.key? :sleep
puts 'Waiting for some time to start'
initial_wait = rand
sleep initial_wait
puts "Waited for #{format('%.4f', initial_wait)} seconds, starting load test..."
puts "\n"
if options[:verbose]
puts 'Running in verbose mode'
puts "Load testing #{options[:url]} #{options[:times]} times"
puts "Waiting #{options[:sleep]} seconds between each call"
puts "\n"
end
(0..options[:times] - 1).each do |i|
curl_result = `curl -s #{options[:url]}`
puts "Try #{i + 1} - #{curl_result}"
sleep options[:sleep]
end