Sha256: 0628cbbccfb7896e1daf4db4c53d587675afacd40e436a35398482325661dd5a

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

require 'benchmark'

require 'benchmark/perf/execution_time'
require 'benchmark/perf/iteration'
require 'benchmark/perf/version'

module Benchmark
  module Perf
    # Calculate arithemtic average of measurements
    #
    # @param [Array[Float]] measurements
    #
    # @return [Float]
    #   the average of given measurements
    #
    # @api public
    def average(measurements)
      return 0 if measurements.empty?
      measurements.reduce(&:+).to_f / measurements.size
    end

    # Calculate standard deviation
    #
    # @param [Array[Float]] measurements
    #
    # @api public
    def std_dev(measurements)
      return 0 if measurements.empty?
      average = average(measurements)
      Math.sqrt(
        measurements.reduce(0) do |sum, x|
          sum + (x - average)**2
        end.to_f / (measurements.size - 1)
      )
    end

    # Run given work and gather time statistics
    #
    # @param [Float] threshold
    #
    # @return [Boolean]
    #
    # @api public
    def assert_perform_under(threshold, options = {}, &work)
      bench = ExecutionTime.new(options)
      actual, _ = bench.run(&work)
      actual <= threshold
    end

    # Assert work is performed within expected iterations per second
    #
    # @param [Integer] iterations
    #
    # @return [Boolean]
    #
    # @api public
    def assert_perform_ips(iterations, options = {}, &work)
      bench = Iteration.new(options)
      mean, stddev, _ = bench.run(&work)
      iterations <= (mean + 3 * stddev)
    end

    extend Benchmark::Perf
  end # Perf
end # Benchmark

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
benchmark-perf-0.1.1 lib/benchmark/perf.rb
benchmark-perf-0.1.0 lib/benchmark/perf.rb