Class: ApiBomb::War

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ War

alias_method :path, :paths



21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/api_bomb.rb', line 17

def base_url
  @base_url
end

#durationObject (readonly)

Returns the value of attribute duration.



17
18
19
# File 'lib/api_bomb.rb', line 17

def duration
  @duration
end

#frontsObject (readonly)

Returns the value of attribute fronts.



17
18
19
# File 'lib/api_bomb.rb', line 17

def fronts
  @fronts
end

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/api_bomb.rb', line 17

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/api_bomb.rb', line 17

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



17
18
19
# File 'lib/api_bomb.rb', line 17

def paths
  @paths
end

#requestsObject (readonly)

Returns the value of attribute requests.



17
18
19
# File 'lib/api_bomb.rb', line 17

def requests
  @requests
end

Instance Method Details

#build_pathsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



73
74
75
76
77
# 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!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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