class SiegeSiege::Runner

Attributes

conf[RW]
urls[RW]

Public Class Methods

new(raw_configuration = {}) click to toggle source
# File lib/siege_siege/runner.rb, line 5
def initialize(raw_configuration = {})
  @rc_file = Tempfile.open
  @urls_file = Tempfile.open
  @command = nil

  Configuration.new(
    {
      concurrent: 1,
      time: 10,
      reps: 1
    }.merge!(raw_configuration).merge!(
      verbose: true,
      rc: @rc_file.path,
      csv: true,
      display_id: false,
      quiet: false,
      follow_location: false,
      timestamp: false,
      file: raw_configuration[:url] ? nil : @urls_file.path
    )
  ).tap do |conf|
    File.write(@rc_file, conf.rc)
    File.write(@urls_file, conf.urls.map(&:to_siege_url).join("\n"))
    @command = "siege #{conf.options}"
    @urls = conf.urls
  end
end

Public Instance Methods

run() click to toggle source
# File lib/siege_siege/runner.rb, line 33
def run
  puts "\e[32m#{@command}\e[0m"
  _, stdout, stderr = Open3.popen3(@command)

  indicate

  out = stdout.read
  err = stderr.read

  indicate_end

  Result.new(@command, @urls, out, err)
ensure
  indicate_end
end

Private Instance Methods

indicate() click to toggle source
# File lib/siege_siege/runner.rb, line 52
def indicate
  @indicator = Thread.start do
    chars = %w[| / - \\]
    i = 0
    loop do
      print "\e[31m#{chars[i % chars.length]}\e[0m"
      sleep 0.1
      i += 1
      print "\b"
    end
  end
end
indicate_end() click to toggle source
# File lib/siege_siege/runner.rb, line 65
def indicate_end
  return unless @indicator
  Thread.kill(@indicator)
  print "\b"
  @indicator = nil
end