Class: WebBenchmark
- Inherits:
-
Object
- Object
- WebBenchmark
- Defined in:
- lib/web_benchmark.rb,
lib/results.rb,
lib/fetcher.rb,
lib/interpretor.rb
Overview
Perform a visitors benchmark on a web resource (be it app or site)
Drill down
-
Start X threads
-
Let each thread perform an average of Y requests (count -/+ rand(count))
-
Make sure the user waits < Z seconds before requesting the next page
This way you can mimic a number of visitors on your site and see what that does for
-
The load on your webserver (keep an eye on that!)
-
The speed per request
Assets
By default each visitor will request the assets (img, script, css) it encounters (unless seen before). The time it takes to load these assets is added to the request time
Defined Under Namespace
Classes: Fetcher, Interpretor, Results
Constant Summary
- VERSION =
'1.0.0'
Instance Attribute Summary (collapse)
-
- (Object) count
Returns the value of attribute count.
-
- (Object) include_assets
Returns the value of attribute include_assets.
-
- (Object) noisy
Returns the value of attribute noisy.
-
- (Object) start_point
Returns the value of attribute start_point.
-
- (Object) visitors
Returns the value of attribute visitors.
Instance Method Summary (collapse)
-
- (Object) benchmark(url)
how well does the resource perform.
-
- (Object) full_test(cool_down = 5)
Perform a full test - this will show you how things 'scale'.
-
- (WebBenchmark) initialize(start_point, count = 5, visitors = 2, sleep = 500)
constructor
A new instance of WebBenchmark.
- - (Object) shout(msg)
-
- (Object) start
start all #visitors.
Constructor Details
- (WebBenchmark) initialize(start_point, count = 5, visitors = 2, sleep = 500)
A new instance of WebBenchmark
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/web_benchmark.rb', line 27 def initialize(start_point, count=5, visitors=2, sleep=500) @start_point = start_point @visitors = visitors @count = count sleep *= 100 if sleep < 100 @sleep = sleep @noisy = false @include_assets = true end |
Instance Attribute Details
- (Object) count
Returns the value of attribute count
25 26 27 |
# File 'lib/web_benchmark.rb', line 25 def count @count end |
- (Object) include_assets
Returns the value of attribute include_assets
25 26 27 |
# File 'lib/web_benchmark.rb', line 25 def include_assets @include_assets end |
- (Object) noisy
Returns the value of attribute noisy
25 26 27 |
# File 'lib/web_benchmark.rb', line 25 def noisy @noisy end |
- (Object) start_point
Returns the value of attribute start_point
25 26 27 |
# File 'lib/web_benchmark.rb', line 25 def start_point @start_point end |
- (Object) visitors
Returns the value of attribute visitors
25 26 27 |
# File 'lib/web_benchmark.rb', line 25 def visitors @visitors end |
Instance Method Details
- (Object) benchmark(url)
how well does the resource perform.
The total time of the request is stored in WebBenchmark::Results
before the benchmark is started, a sleep time is introduced. This helps to spread out (and randomize) the #visitors
The requested page is analyzed and a next link is choosen if the visitor still has pages left to visit
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/web_benchmark.rb', line 122 def benchmark(url) me = Thread.current if @sleep != 0 slumber = ((rand(@sleep)+1)/100.0) shout "#{me[:name]}: sleep #{slumber}" Kernel::sleep(slumber) end shout "#{me[:name]}: #{url}" fetcher = Fetcher.new(url) res = fetcher.fetch if res == false shout "#{me[:name]}: error on #{url}" return end if @include_assets == true and !fetcher.body.nil? fetched = fetcher.fetch_assets(me[:assets_cache]) shout("Fetched assets: #{fetched.join(", ")}") me[:assets_cache] += fetched end r = Results.instance(url) r.record(fetcher.start, fetcher.stop, fetcher.result.status) links = [] if fetcher.body fetcher.body.css('a').each do |a| link = a[:href] if link =~ /^\// link = @start_point + link elsif link !~ /https?:/ begin base = url.gsub(/[^\/]+$/, '') link = base + link rescue end end links << link unless link !~ Regexp.new(@start_point) end end if me[:count] > 1 me[:count] -= 1 next_url = nil tries = 0 while next_url.nil? and tries < 10 next_url = links[rand(links.length-1)] begin URI.parse(next_url) rescue next_url = nil end tries += 1 end if next_url.nil? shout("Cannot find another link on #{url} #{fetcher.result.status}- restarting on start point") return benchmark(@start_point) end benchmark(next_url.gsub(/([^:])\/\//, '\1/')) end end |
- (Object) full_test(cool_down = 5)
Perform a full test - this will show you how things 'scale'
First, a base line is established by performing #count visits with only one #visitors. This would generate a sample that should serve as the 'nominal' operation of the resource
Then we let things cool down for some seconds and perform a test with only half the number of visitors requested
Cool down again and finaly go in full blast.
You will be handed an interpretor holding 3 result sets to play with
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/web_benchmark.rb', line 52 def full_test(cool_down=5) puts "Perfoming a full test" interpretor = Interpretor.new(false) visitors_before = self.visitors # first, establish a base line 1 client - 20 requests self.visitors = 1 self.start interpretor.sets << Results.get_all Results.clear puts "Letting the server cool down #{cool_down}s" sleep cool_down self.visitors = visitors_before / 2 self.start interpretor.sets << Results.get_all Results.clear puts "Letting the server cool down #{cool_down}s" sleep cool_down self.visitors = visitors_before self.start interpretor.sets << Results.get_all return interpretor end |
- (Object) shout(msg)
195 196 197 198 |
# File 'lib/web_benchmark.rb', line 195 def shout msg return if !@noisy $stderr.puts msg end |
- (Object) start
start all #visitors
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/web_benchmark.rb', line 87 def start threads = [] start = Time.now puts "Starting benchmark with #{@visitors} visitors for #{@count} pages..." @visitors.times { |i| threads << Thread.new(@start_point, @count) do |url, count| Thread.current[:count] = ( rand(2) == 1 ? count - rand(count) : count + rand(count) ) Thread.current[:name] = "[Visitor-#{i+1}]" Thread.current[:assets_cache] = [] benchmark(url) end } shout "Waiting for #{threads.count} threads" threads.collect(&:join) puts "Benchmark took: #{"%.2f" % (Time.now - start)}" return end |