Sha256: 55e55a66ffd495acbed8fa3f4946676ee7f36ab0788cae42611b67d2e0491809

Contents?: true

Size: 1.62 KB

Versions: 20

Compression:

Stored size: 1.62 KB

Contents

module BenchmarkDriver
  # Repeat calling block and return desired result: "best", "worst" or "average".
  module Repeater
    VALID_TYPES = %w[best worst average]

    class << self
      # `block.call` can return multiple objects, but the first one is used for sort.
      # When `config.repeat_result == 'average'`, how to deal with rest objects is decided
      # by `:rest_on_average` option.
      def with_repeat(config:, larger_better:, rest_on_average: :first, &block)
        values = config.repeat_count.times.map { block.call }

        case config.repeat_result
        when 'best'
          best_result(values, larger_better)
        when 'worst'
          best_result(values, !larger_better)
        when 'average'
          average_result(values, rest_on_average)
        else
          raise "unexpected repeat_result #{config.repeat_result.inspect}"
        end
      end

      private

      def best_result(values, larger_better)
        values.sort_by do |value, *|
          larger_better ? value : -value
        end.last
      end

      def average_result(values, rest_on_average)
        unless values.first.is_a?(Array)
          return values.inject(&:+) / values.size.to_f
        end

        case rest_on_average
        when :first
          rest = values.first[1..-1]
          [values.map { |v| v[0] }.inject(&:+) / values.size.to_f, *rest]
        when :average
          values.first.size.times.map do |index|
            values.map { |v| v[index] }.inject(&:+) / values.first.size.to_f
          end
        else
          raise "unexpected rest_on_average #{rest_on_average.inspect}"
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
benchmark_driver_monotonic_raw-0.14.13 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.13 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.12 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.11 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.10 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.9 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.8 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.7 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.6 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.5 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.4 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.3 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.2 lib/benchmark_driver/repeater.rb
benchmark_driver-0.14.0 lib/benchmark_driver/repeater.rb
benchmark_driver-0.13.3 lib/benchmark_driver/repeater.rb
benchmark_driver-0.13.2 lib/benchmark_driver/repeater.rb
benchmark_driver-0.13.1 lib/benchmark_driver/repeater.rb
benchmark_driver-0.13.0 lib/benchmark_driver/repeater.rb
benchmark_driver-0.12.0 lib/benchmark_driver/repeater.rb
benchmark_driver-0.11.1 lib/benchmark_driver/repeater.rb