Module: LoadTest
- Defined in:
- lib/load_test.rb,
lib/load_test/version.rb
Overview
load test
Defined Under Namespace
Classes: Future, ResultSender, Stat, StatRow
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.output_stdout(stat, columns: [:name, :average, :median, :percentile_80, :percentile_90, :count, :error_count], decimal_place: 2) ⇒ Object
Output statistics to stdout.
-
.result_receiver(reader) ⇒ Object
Create an Enumerator that receives results from the child process.
-
.run(rpm:, limit_time: nil, limit_count: nil, process_size: nil, &block) ⇒ Object
Run a load test.
-
.run_custom(process_size: nil, concurrent: 1, repeat: 1, interval: 0, &block) ⇒ Object
It executes while finely controlling computational resources.
-
.stat(result) ⇒ Object
Statistics of load test results.
Class Method Details
.output_stdout(stat, columns: [:name, :average, :median, :percentile_80, :percentile_90, :count, :error_count], decimal_place: 2) ⇒ Object
Output statistics to stdout.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/load_test.rb', line 169 def output_stdout(stat, columns: [:name, :average, :median, :percentile_80, :percentile_90, :count, :error_count], decimal_place: 2) columns.map!(&:to_s) column_to_values = columns.to_h { [_1, [_1]] } stat.to_a.sort_by(&:name).each do |stat_row| columns.each do |column| if /percentile/.match?(column) percent = column.split("_")[1] column_to_values[column] << stat_row.percentile(Integer(percent)).round(decimal_place).to_s else raise "Column '#{column}' does not exist." unless stat_row.respond_to?(column) result = stat_row.send(column) if result.is_a?(Numeric) result = result.round(decimal_place) end column_to_values[column] << result.to_s end end end column_to_size = column_to_values.transform_values do |values| values.max_by(&:size).size end column_to_values = column_to_values.transform_values { _1[1..] } puts columns.map { _1.ljust(column_to_size[_1]) }.join(" | ") column_to_values.first[1].size.times do |index| puts columns.map { |column| column_to_values[column][index].rjust(column_to_size[column]) }.join(" | ") end end |
.result_receiver(reader) ⇒ Object
Create an Enumerator that receives results from the child process.
- reader
-
reader of IO.pipe
- return
-
Enumerator
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/load_test.rb', line 131 def result_receiver(reader) # :nodoc: Enumerator.new do |y| raise "This Enumerator can only be repeated once. hint: It may be better to use the Enumerator#to_a method to make it an array." if reader.closed? loop do break if reader.eof? result = Marshal.load(reader) raise result if result.is_a?(Exception) y << result end ensure reader.close end end |
.run(rpm:, limit_time: nil, limit_count: nil, process_size: nil, &block) ⇒ Object
Run a load test.
Note: Either limit_time or limit_count is required.
- rpm
-
The number of times to execute the block per minute.
- limit_time
-
After this number of seconds, the test ends.
- limit_count
-
The test ends when the block executes limit_count times.
- process_size
-
Number of processes to launch. Default is equal to the number of CPU cores.
- block {|result_sender| … }
-
This content is efficiently repeated in parallel. Argument is LoadTest::ResultSender.
- return
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 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 |
# File 'lib/load_test.rb', line 25 def run(rpm:, limit_time: nil, limit_count: nil, process_size: nil, &block) if limit_time.nil? && limit_count.nil? raise ArgumentError, "Either limit_time or limit_count is required." end process_size ||= Etc.nprocessors # request per second (per process) rps = (60 / rpm.to_f) * process_size # Number of requests per process. if limit_count base_request_count = limit_count.div(process_size) request_counts = Array.new(process_size, base_request_count) (limit_count % process_size).times { request_counts[_1] += 1 } else request_counts = Array.new(process_size, nil) end reader, writer = IO.pipe result_sender = LoadTest::ResultSender.new(writer) request_counts.each do |request_count| fork do reader.close error = nil catch do |finish| timers = Timers::Group.new timers.after(limit_time) { throw finish } if limit_time timers.every(rps) do block.call(result_sender) rescue => e error = e throw finish end if request_count request_count.times { timers.wait } else loop { timers.wait } end end raise error if error ensure writer.close end end writer.close Process.waitall result_receiver(reader) end |
.run_custom(process_size: nil, concurrent: 1, repeat: 1, interval: 0, &block) ⇒ Object
It executes while finely controlling computational resources.
- process_size
-
The number of times to do Process.fork.
- concurrent
-
The meaning is close to the number of threads. It’s strictly the number of Async.
- repeat
-
number of times to repeat. If nil is specified, it will loop infinitely.
- block {|result_sender| … }
-
This content is efficiently repeated in parallel. Argument is LoadTest::ResultSender.
- return
91 92 93 94 95 96 97 98 99 100 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 |
# File 'lib/load_test.rb', line 91 def run_custom(process_size: nil, concurrent: 1, repeat: 1, interval: 0, &block) process_size ||= Etc.nprocessors reader, writer = IO.pipe result_sender = LoadTest::ResultSender.new(writer) process_size.times do fork do reader.close Async do |task| concurrent.times do task.async do if repeat repeat.times do block.call(result_sender) sleep interval end else loop do block.call(result_sender) sleep interval end end end end end ensure writer.close end end writer.close Process.waitall result_receiver(reader) end |