Class: Siege

Inherits:
Object
  • Object
show all
Defined in:
lib/siege.rb,
lib/siege/results.rb,
lib/siege/temp_file.rb

Overview

TODO: Separate Gem?

Defined Under Namespace

Classes: Results, TempFile

Constant Summary collapse

DEFAULTS =
{
  duration:   '30s',
  users:      15,
  rc:         File.expand_path('../siege/data/siegerc', __FILE__)
}
OPTIONS =
{
  benchmark:  '-b',
  duration:   '-t',
  file:       '-f',
  header:     '-H',
  internet:   '-i',
  quiet:      '-q',
  user_agent: '-A',
  users:      '-c',
  rc:         '-R',
  delay:      '-d',
  verbose:    '-v'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, conf) ⇒ Siege

Returns a new instance of Siege.



26
27
28
29
30
# File 'lib/siege.rb', line 26

def initialize(urls, conf)
  @url_file = TempFile.with_content urls
  @conf     = DEFAULTS.merge conf
  @results  = Results.for executor
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



24
25
26
# File 'lib/siege.rb', line 24

def conf
  @conf
end

#resultsObject (readonly)

Returns the value of attribute results.



24
25
26
# File 'lib/siege.rb', line 24

def results
  @results
end

Instance Method Details

#cli_optsObject



45
46
47
48
49
50
51
52
# File 'lib/siege.rb', line 45

def cli_opts
  @conf.flat_map { |opt, val| OPTIONS[opt] + val.to_s }.
      join(' ').
      gsub(/(\-\w) true/, '\1').
      gsub(/\-\w false/, '') +
      url_opt +
      stdout_to_stdin
end

#executorObject



32
33
34
35
# File 'lib/siege.rb', line 32

def executor
  puts "Sieging with:\n\tsiege #{cli_opts} -f#{@url_file.name} 2>&1"
  `siege #{cli_opts}`
end

#stdout_to_stdinObject



41
42
43
# File 'lib/siege.rb', line 41

def stdout_to_stdin
  ' 2>&1'
end

#url_optObject



37
38
39
# File 'lib/siege.rb', line 37

def url_opt
  ' -f' + @url_file.name
end