Sha256: dd1021ca7bc12c3c58a3c7500444ebff564fc9142d6e06d1cb3eb18c64a154af

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'benchmark'

module RBM
  class Benchmarker
    DEFAULT_OPTIONS = {
      :times => 1
    }

    attr_reader :fragments, :options

    def initialize(fragments, options)
      @fragments, @options = fragments, DEFAULT_OPTIONS.merge(options)
    end

    def run
      width = fragments.map { |fragment| (fragment[:name] || "").length }.max
      Benchmark.bm(width) do |bm|
        fragments.each do |fragment|
          name = fragment[:name] || ""
          fragment_name = (fragment[:name] || (@unnamed_fragment ||= "fragment_0").succ!).gsub(/\s+/, "_")

          object = Object.new
          binding = object.send(:binding)

          bm.report(name) do
            # TODO: figure out how to not eval each loop but still provide a better stack trace

            eval options[:init], binding, "init", 1 if options[:init]
            eval fragment[:prerun], binding, "#{fragment_name}_prerun", 1 if fragment[:prerun]
            options[:times].times { eval fragment[:fragment], binding, fragment_name, 1 }
            eval fragment[:postrun], binding, "#{fragment_name}_postrun", 1 if fragment[:postrun]
            eval options[:cleanup], binding, "cleanup", 1 if options[:cleanup]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rbm-0.0.2 lib/rbm/benchmarker.rb