class TimingAttack::Enumerator

Attributes

grouper[R]
inputs[R]

Public Class Methods

new(inputs: [], options: {}) click to toggle source
Calls superclass method TimingAttack::Attacker::new
# File lib/timing_attack/enumerator.rb, line 5
def initialize(inputs: [], options: {})
  @inputs = inputs
  raise ArgumentError.new("Need at least 2 inputs") if inputs.count < 2
  super(options: options)
  @attacks = inputs.map { |input| TestCase.new(input: input, options: @options) }
end

Public Instance Methods

run!() click to toggle source
Calls superclass method TimingAttack::Attacker#run!
# File lib/timing_attack/enumerator.rb, line 12
def run!
 super
 puts report
end

Private Instance Methods

attack!() click to toggle source
# File lib/timing_attack/enumerator.rb, line 37
def attack!
  hydra = Typhoeus::Hydra.new(max_concurrency: concurrency)
  iterations.times do
    attacks.each do |attack|
      req = attack.generate_hydra_request!
      req.on_complete do |response|
        output.increment
      end
      hydra.queue req
    end
  end
  hydra.run
  attacks.each(&:process!)
end
bar_format() click to toggle source
# File lib/timing_attack/enumerator.rb, line 70
def bar_format
  @bar_format ||= "%t (%E) |%B|"
end
default_options() click to toggle source
Calls superclass method TimingAttack::Attacker#default_options
# File lib/timing_attack/enumerator.rb, line 79
def default_options
  super.merge(
    width: inputs.dup.map(&:length).push(30).sort.last,
  ).freeze
end
null_bar() click to toggle source
# File lib/timing_attack/enumerator.rb, line 74
def null_bar
  @null_bar_klass ||= Struct.new('NullProgressBar', :increment)
  @null_bar ||= @null_bar_klass.new
end
output() click to toggle source
# File lib/timing_attack/enumerator.rb, line 62
def output
  return null_bar unless verbose?
  @output ||= ProgressBar.create(title: "  Attacking".ljust(15),
                                     total: iterations * attacks.length,
                                     format: bar_format
                                    )
end
report() click to toggle source
# File lib/timing_attack/enumerator.rb, line 21
def report
  ret = ''
  hsh = grouper.serialize
  if hsh[:spike_delta] < threshold
    ret << "\n* Spike delta of #{sprintf('%.4f', hsh[:spike_delta])} is less than #{sprintf('%.4f', threshold)} * \n\n"
  end
  [:short, :long].each do |sym|
    ret << "#{sym.to_s.capitalize} tests:\n"
    hsh.fetch(sym).each do |input, time|
      ret << "  #{input.ljust(width)}"
      ret << sprintf('%.4f', time) << "\n"
    end
  end
  ret
end