Sha256: d5a1fdbbfb88ad190ed268b3f7d3edd2e69bfcb5e826aa5508b2c5fa3fe47f84

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Rodimus

  class Benchmark
    include Observing

    attr_reader :stats

    def on_notify(subject, event_type)
      case event_type
      when :before_run
        initialize_stats
      when :after_run
        finalize_stats(subject)
      when :before_row
        before_row
      when :after_row
        after_row
      end
    end

    private

    def after_row
      row_run_time = (Time.now.to_f - @start_time).round(4)
      stats[:total] = (stats[:total] + row_run_time).round(4)
      stats[:min] = row_run_time if stats[:min] > row_run_time
      stats[:max] = row_run_time if stats[:max] < row_run_time
    end

    def before_row
      stats[:count] += 1
      @start_time = Time.now.to_f
    end

    def finalize_stats(subject)
      if stats[:count] > 0
        stats[:average] = (stats[:total] / stats[:count]).round(4)
      end

      Rodimus.logger.info "#{subject} benchmarks: #{stats}"
    end

    def initialize_stats
      @stats = {count: 0, total: 0, min: 1, max: 0, average: 0}
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rodimus-1.0.0 lib/rodimus/benchmark.rb