Sha256: 95533d0f4f6d41c096b1d89b5a1e37fc2d5a9ecfc1ac1e4ec704529c965caa1f

Contents?: true

Size: 1.27 KB

Versions: 30

Compression:

Stored size: 1.27 KB

Contents

require 'benchmark'

module Ramaze
  module Helper

    # Little helper to give you a hand when benching parts of actions
    module Bench

      # Will first run an empty loop to determine the overhead it imposes, then
      # goes on to yield your block +iterations+ times.
      #
      # The last yielded return value will be returned upon completion of the
      # benchmark and the result of the benchmark itself will be sent to
      # Log.info
      #
      # Example:
      #
      #   class MainController < Ramaze::Controller
      #     def index
      #       @users = bench{ User.all }
      #       @tags = bench{ Article.tags }
      #     end
      #   end
      #
      # This will show something like following in your log:
      # [..] INFO   Bench ./start.rb:3:in `index': 0.121163845062256
      # [..] INFO   Bench ./start.rb:4:in `index': 2.234987235098341
      #
      # So now we know that the Article.tags call takes the most time and
      # should be improved.
      def bench(iterations = 1)
        result = nil
        from = caller[0]
        delta = Benchmark.realtime{ iterations.times{ nil }}
        taken = Benchmark.realtime{ iterations.times{ result = yield }}
        Log.info "Bench #{from}: #{taken - delta}"
        return result
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 4 rubygems

Version Path
Pistos-ramaze-2009.04.08 lib/ramaze/helper/bench.rb
Pistos-ramaze-2009.06.12 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.04.01 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.04.08 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.04.18 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.04.22 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.04 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.05.08 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.05 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.06.04 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.06.12 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.06 lib/ramaze/helper/bench.rb
manveru-ramaze-2009.07 lib/ramaze/helper/bench.rb
rjspotter-ramaze-2009.06.29 lib/ramaze/helper/bench.rb
rjspotter-ramaze-2009.06.31 lib/ramaze/helper/bench.rb
ramaze-2011.07.25 lib/ramaze/helper/bench.rb
ramaze-2011.01.30 lib/ramaze/helper/bench.rb
ramaze-2011.01 lib/ramaze/helper/bench.rb
ramaze-2010.06.18 lib/ramaze/helper/bench.rb
ramaze-2010.04.04 lib/ramaze/helper/bench.rb