Class: Benchmark::HTTP::Command::Hammer
- Inherits:
-
Samovar::Command
- Object
- Samovar::Command
- Benchmark::HTTP::Command::Hammer
- Defined in:
- lib/benchmark/http/command/hammer.rb
Instance Method Summary collapse
- #alpn_protocols ⇒ Object
- #call ⇒ Object
- #measure_performance(concurrency, count, endpoint, request_path) ⇒ Object
- #run(url) ⇒ Object
Instance Method Details
#alpn_protocols ⇒ Object
72 73 74 |
# File 'lib/benchmark/http/command/hammer.rb', line 72 def alpn_protocols @options[:alpn_protocols]&.split(',') end |
#call ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/benchmark/http/command/hammer.rb', line 89 def call while true @urls.each do |url| run(url) end if @interval sleep(@interval) else break end end end |
#measure_performance(concurrency, count, endpoint, request_path) ⇒ Object
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 |
# File 'lib/benchmark/http/command/hammer.rb', line 31 def measure_performance(concurrency, count, endpoint, request_path) Console.logger.info(self) {"I am running #{concurrency} asynchronous tasks that will each make #{count} sequential requests..."} statistics = Statistics.new(concurrency) task = Async::Task.current running = true progress_task = task.async do |child| while true child.sleep(1) Console.logger.info(self, statistics) end end concurrency.times.map do task.async do client = Async::HTTP::Client.new(endpoint, protocol: endpoint.protocol) count.times do |i| statistics.measure do response = client.get(request_path).tap(&:finish) end end client.close end end.each(&:wait) progress_task&.stop Console.logger.info(self) do |buffer| buffer.puts "I made #{statistics.count} requests in #{Seconds[statistics.sequential_duration]}. The per-request latency was #{Seconds[statistics.latency]}. That's #{statistics.per_second} asynchronous requests/second." buffer.puts "\t Variance: #{Seconds[statistics.variance]}" buffer.puts "\tStandard Deviation: #{Seconds[statistics.standard_deviation]}" buffer.puts "\t Standard Error: #{Seconds[statistics.standard_error]}" buffer.puts statistics end return statistics end |
#run(url) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/benchmark/http/command/hammer.rb', line 76 def run(url) endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: self.alpn_protocols) request_path = endpoint.url.request_uri Console.logger.info(self) {"I am going to benchmark #{url}..."} Sync do |task| statistics = [] base = measure_performance(@options[:concurrency], @options[:count], endpoint, request_path) end end |