class ApiBomb::War

Attributes

base_url[R]
duration[R]
fronts[R]
logger[R]
options[R]
paths[R]
requests[R]

Public Class Methods

new(opts = {}) click to toggle source

alias_method :path, :paths

# File lib/api_bomb.rb, line 21
def initialize(opts = {})
  @fronts = opts[:concurrent_users] || 2
  @duration = opts[:duration] || 10
  @paths = opts[:paths]
  @paths = (opts[:path] || '') if @paths.blank?
  @options = LambdaHash.new(opts[:options] || {})
  @base_url = opts[:base_url] || ''
  @logger = opts[:logger] || Logger.new(STDOUT)
  @requests = opts[:requests]
  build_paths
end

Public Instance Methods

build_paths() click to toggle source
# File lib/api_bomb.rb, line 33
def build_paths
  case @paths
  when String
    @paths = Path::Single.new(path: @paths)
  when Array
    tmp_paths = []
    @paths.each do |path|
      if path.is_a? Hash
        tmp_paths << Path::Single.new(path)
      elsif path.is_a? String
        tmp_paths << Path::Single.new(path: path)
      else
        raise 'Unknown path structure'
      end
    end
    @paths = Path::Sequence.new(tmp_paths)
  when Hash
    @paths = Path::Single.new(@paths)
  when Path::Single, Path::Sequence, Path::Weighted
  else
    raise 'Unknown path structure'
  end
end
dereference_path(path) click to toggle source
# File lib/api_bomb.rb, line 73
def dereference_path(path)
  return path.call if path.respond_to? :call
  return path[:path] if path.respond_to? :[] && path[:path] != nil
  path
end
start!() click to toggle source
# File lib/api_bomb.rb, line 57
def start!
  case paths
  when Path::Single
    @logger.info("#{paths.report(base_url)}, duration: #{duration} sec")
    start_attack!(paths)
  when Path::Sequence
    paths.each do |path|
      @logger.info("#{path.report(base_url)}, duration: #{duration} sec")
      start_attack!(path)
    end
  when Path::Weighted
    @logger.info(paths.report(base_url))
    start_attack!(paths)
  end
end
start_attack!(testing_paths) click to toggle source
# File lib/api_bomb.rb, line 79
def start_attack!(testing_paths)
  Commander.new(
    fronts: fronts,
    duration: duration,
    army: Army.new(
      fighters: Fighter.pool(
        size: 2*fronts, args: [
          paths: testing_paths, base_url: base_url, options: options
        ]
      )
    ),
    logger: logger,
    requests: requests
  ).start_attack!
end