Class: AbCrunch::AbRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/abcrunch/ab_runner.rb

Class Method Summary collapse

Class Method Details

.ab(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/abcrunch/ab_runner.rb', line 18

def self.ab(options)
  cmd = ab_command(options)
  result = nil

  Open3.popen3(cmd) do |stdin, stdout, stderr|
    err_lines = stderr.readlines
    if err_lines.length > 0
      cmd = cmd.cyan
      err = "AB command failed".red
      err_s = err_lines.reduce {|line, memo| memo += line}.red
      raise "#{err}\nCommand: #{cmd}\n#{err_s}"
    end
    result = AbCrunch::AbResult.new stdout.readlines.reduce {|line, memo| memo += line}, options
  end

  result
end

.ab_command(options) ⇒ Object



11
12
13
14
15
16
# File 'lib/abcrunch/ab_runner.rb', line 11

def self.ab_command(options)
  options = validate_options(options)
  url = AbCrunch::Page.get_url(options, true)
  AbCrunch::Logger.log(:info, "Calling ab on:  #{url}")
  "ab -c #{options[:concurrency]} -n #{options[:num_requests]} -k -H 'Accept-Encoding: gzip' #{url}"
end

.validate_options(options) ⇒ Object



6
7
8
9
# File 'lib/abcrunch/ab_runner.rb', line 6

def self.validate_options(options)
  raise "AB Options missing :url" unless options.has_key? :url
  AbCrunch::Config.ab_options.merge(options)
end