class GemFootprintAnalyzer::AverageRunner

A class handling sampling and calculating basic statistical values from the set of runs.

Constants

AVERAGED_FIELDS
RUNS

Public Class Methods

new(runs = RUNS, &run_block) click to toggle source

@param runs [Integer] optional number of runs to perform @param run_block [proc] actual unit of work to be done runs times

# File lib/gem_footprint_analyzer/average_runner.rb, line 9
def initialize(runs = RUNS, &run_block)
  fail ArgumentError, 'runs must be > 0' unless runs > 0

  @run_block = run_block
  @runs = runs
end

Public Instance Methods

run() click to toggle source

@return [Array<Hash>] Array of hashes that now include average metrics in place of fields

present in {AVERAGED_FIELDS}. The rest of the columns is copied from the first sample.
# File lib/gem_footprint_analyzer/average_runner.rb, line 18
def run
  results = Array.new(@runs) { run_once }
  calculate_averages(results)
end

Private Instance Methods

calculate_average(values) click to toggle source
# File lib/gem_footprint_analyzer/average_runner.rb, line 45
def calculate_average(values)
  num = values.size
  sum = values.sum.to_f
  mean = sum / num

  stddev = Math.sqrt(values.sum { |v| (v - mean)**2 } / num).round(4)
  {mean: mean, stddev: stddev}
end
calculate_averages(results) click to toggle source

Take corresponding results array values and compare them

# File lib/gem_footprint_analyzer/average_runner.rb, line 30
def calculate_averages(results)
  Array.new(results.first.size) do |require_number|
    samples = results.map { |r| r[require_number] }
    first_sample = samples.first

    average = initialize_average_with_copied_fields(first_sample)
    AVERAGED_FIELDS.map do |field|
      next unless first_sample.key?(field)

      average[field] = calculate_average(samples.map { |s| s[field] })
    end
    average
  end
end
initialize_average_with_copied_fields(sample) click to toggle source
# File lib/gem_footprint_analyzer/average_runner.rb, line 54
def initialize_average_with_copied_fields(sample)
  average = {}
  (sample.keys - AVERAGED_FIELDS).each { |k| average[k] = sample[k] }
  average
end
run_once() click to toggle source
# File lib/gem_footprint_analyzer/average_runner.rb, line 25
def run_once
  @run_block.call
end