Class: RWB::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rwb.rb,
lib/rwb/report.rb,
lib/rwb/warmup.rb,
lib/rwb/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, max_runs = 100, max_threads = 10) ⇒ Runner

Returns a new instance of Runner.



61
62
63
64
65
66
67
68
69
70
# File 'lib/rwb.rb', line 61

def initialize(urls, max_runs=100, max_threads=10)
  @urls = urls
  @max_runs = max_runs
  @max_threads = max_threads
  @results_mean = nil
  @results_std_dev = nil
  @sla_levels = [0.5, 0.9]
  @http = Net::HTTP
  @new_results = RunResults.new
end

Instance Attribute Details

#sla_levelsObject

Returns the value of attribute sla_levels.



59
60
61
# File 'lib/rwb.rb', line 59

def sla_levels
  @sla_levels
end

Instance Method Details

#add_proxy(proxy_addr, proxy_port = 80, proxy_user = nil, proxy_pass = nil) ⇒ Object



72
73
74
75
76
# File 'lib/rwb.rb', line 72

def add_proxy(proxy_addr, proxy_port = 80, proxy_user = nil, 
              proxy_pass = nil)
  @http = Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user,
                           proxy_pass)
end

#adjust_scale(scale, max) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/rwb/results.rb', line 110

def adjust_scale(scale, max)
  if scale/2 > max
    scale = scale/2
    scale = adjust_scale(scale, max)
  end
  scale
end

#get_times(results, levels) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rwb/report.rb', line 70

def get_times(results, levels)
  times = Array.new
  times.push(make_milli(results[0]))
  levels.each do |num|
    times.push(make_milli(results[(results.length*num - 1).round]))
  end
  unless times.last == make_milli(results.last)
    times.push(make_milli(results.last))
  end
  times
end

#graph_quartiles_overallObject



39
40
41
42
# File 'lib/rwb/report.rb', line 39

def graph_quartiles_overall
  results = @new_results.times_by_url
  puts results_quartile(results.sort)
end

#graph_quartiles_urls(scale = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rwb/report.rb', line 31

def graph_quartiles_urls(scale = nil)
  @urls.urls.keys.sort.each do |key|
    url = @urls.urls[key].to_base
    results = @new_results.times_by_url(url)
    puts "#{url}:\n\t" + results_quartile(results.sort, scale)
  end
end

#make_milli(num) ⇒ Object



82
83
84
# File 'lib/rwb/report.rb', line 82

def make_milli(num)
  (num * 1000).to_i
end


60
61
62
63
64
65
66
67
68
# File 'lib/rwb/report.rb', line 60

def print_times(results, levels)
  times = get_times(results, levels)
  puts  "\tShortest time:\t#{times.shift} msecs"
  levels.each_with_index do |num, index|
    percent = num.to_f * 100.0
    puts "\t#{percent}%ile time:\t#{times[index]} msecs"
  end
  puts  "\tLongest time:\t#{times[-1]} msecs"
end

#rand_warmup(num_requests) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rwb/warmup.rb', line 18

def rand_warmup(num_requests)
  $stderr.puts "warming up with #{num_requests} requests"
  checkpoint = num_requests/10
  for run in 1..num_requests
    if run % checkpoint == 0
      $stderr.print "#{run} "
    end
    url = URI.parse(@urls.get_url.to_url)
    @http.start(url.host, url.port) do |http|
      http.request( Net::HTTP::Get.new(url) )
    end
  end
  $stderr.puts
end

#report_by_time(sla_levels = @sla_levels, granularity = 0.2) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rwb/report.rb', line 44

def report_by_time(sla_levels = @sla_levels, granularity = 0.2)
  start = 0.0
  stop = granularity
  results = @new_results.times_by_url
  puts "Results by time:"
  while stop <= 1.0
    first = (results.length * start).to_i
    last = (results.length * stop).to_i
    these_results = results.slice(first..last)
    puts "results for requests #{first} - #{last}"
    print_times(these_results.sort, sla_levels)
    start = stop
    stop += granularity
  end
end

#report_full(sla_levels = @sla_levels) ⇒ Object



4
5
6
7
8
9
# File 'lib/rwb/report.rb', line 4

def report_full(sla_levels = @sla_levels)
  report_header
  report_overall(sla_levels)
  report_urls(sla_levels)
  report_by_time(sla_levels)
end

#report_headerObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/rwb/report.rb', line 86

def report_header
print <<EOF
Concurrency Level:       #{@max_threads}
Total Requests:          #{@max_runs}
Total time for testing:  #{@total_time} secs
Requests per second:     #{@max_runs/@total_time}
Mean time per request:   #{results_mean}  msecs
Standard deviation:      #{results_std_dev}
EOF
end

#report_overall(sla_levels = @sla_levels) ⇒ Object



11
12
13
14
15
16
# File 'lib/rwb/report.rb', line 11

def report_overall(sla_levels = @sla_levels)
  results = @new_results.times_by_url
  
  puts "Overall results:"
  print_times(results.sort, sla_levels)
end

#report_urls(sla_levels = @sla_levels) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rwb/report.rb', line 18

def report_urls(sla_levels = @sla_levels)
  @urls.urls.keys.sort.each do |key|
    url = @urls.urls[key].to_base
    results = @new_results.times_by_url(url)
    puts "Results for #{url}:"
    if results.length > 0
      print_times(results.sort, sla_levels)
    else
      puts "no results for url"
    end
  end
end

#results_meanObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/rwb/results.rb', line 84

def results_mean
  if @results_mean
    return @results_mean
  end
  @results_mean = @new_results.times_by_url.inject(0) do |sum, time|
    sum += time
  end
  @results_mean = @results_mean/@new_results.times_by_url.length
  make_milli(@results_mean)
end

#results_quartile(times, scale = nil) ⇒ Object



118
119
120
121
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
# File 'lib/rwb/results.rb', line 118

def results_quartile(times, scale = nil)
  times = times.map { |t| make_milli(t) }
  size = results_mean + results_std_dev * 2
  min = times[0]
  first  = times[(times.length*0.25).to_int - 1]
  second = times[(times.length*0.5).to_int - 1]
  third  = times[(times.length*0.75).to_int - 1]
  max = times[-1]
  
  len = max.to_s.length
  unless scale
    scale = (max/((10**len).to_f)).ceil * (10**len)
    scale = adjust_scale(scale, max)
  end
  step = scale/50
  step = 1 if step == 0
  scale = 50 if scale < 50
  line = Array.new
  char = ' '
  for i in 0..49 do
    case i
    when max/step
      line[i] = ']'
    when third/step
      line[i] = '|'
      char = ' '
    when second/step
      line[i] = '+'
    when first/step
      line[i] = '|'
      char = '-'
    when min/step
      line[i] = '['
    when 0
      line[i] = ':'
    when 49
      line[i] = ':'
    else
      line[i] = char
    end
  end
  return '0' + line.join('')  + scale.to_s
end

#results_std_devObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rwb/results.rb', line 95

def results_std_dev
  if @results_std_dev
    return @results_std_dev
  end
  unless @results_mean
    results_mean
  end
  @results_std_dev = @new_results.times_by_url.inject(0) do |std_dev, time|
    std_dev += (time - @results_mean) ** 2
  end
  @results_std_dev = 
    Math.sqrt(@results_std_dev/@new_results.times_by_url.length)
  make_milli(@results_std_dev)
end

#runObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rwb.rb', line 78

def run
  threads = []
  @steps = []
  0.step(@max_runs, @max_runs/10) {|i| @steps.push(i) }
  @requests = (1..@max_runs).to_a
  total_start_time = Time.now
  for t in 1..@max_threads
    threads << Thread.new { run_thread }
  end
  threads.each { |th| th.join }
  total_stop_time = Time.now
  @total_time = total_stop_time - total_start_time
end

#run_test(id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rwb.rb', line 101

def run_test(id)
  request = @urls.get_url
#      url = URI.parse request.to_url
  base = request.to_base
  time = nil
  response_code = nil
  
#      @http.start(url.host, url.port) do |http|
#        start_time = Time.now
#        response = http.request( Net::HTTP::Get.new(url) )
#        stop_time = Time.now
#        time = stop_time - start_time
#        response_code = response.code
#      end

  start_time = Time.now
  begin
    open(request.to_url) do |page|
      stop_time = Time.now
      time = stop_time - start_time
      response_code = page.status
    end
  rescue
    stop_time = Time.now
    time = stop_time - start_time
    response_code = 404
  end
  
  
  result = Result.new(id, Time.now, base, time, response_code)
  
  @new_results.add_result(result)
end

#run_threadObject



92
93
94
95
96
97
98
99
# File 'lib/rwb.rb', line 92

def run_thread
  while req_num = @requests.shift
    run_test(req_num-1)
    if @steps.include?(req_num)
      puts "completed #{req_num} runs"
    end
  end
end

#spec_warmup(urls, num_runs = 1) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rwb/warmup.rb', line 33

def spec_warmup(urls, num_runs=1)
  $stderr.puts "warming up with #{num_runs} runs"
  for run in 1..num_runs
    $stderr.print "#{run} "
    urls.each do |url|
      url = URI.parse(url)
      @http.start(url.host, url.port) do |http|
        http.request( Net::HTTP::Get.new(url) )
      end
    end
  end
  $stderr.puts
end

#warmup(num_runs = 1) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rwb/warmup.rb', line 4

def warmup(num_runs = 1)
  $stderr.puts "warming up with #{num_runs} runs"
  for run in 1..num_runs
    $stderr.print "#{run} "
    @urls.urls.values.each do |url|
      url = URI.parse(url.to_url)
      @http.start(url.host, url.port) do |http|
        http.request( Net::HTTP::Get.new(url) )
      end
    end
  end
  $stderr.puts
end