Sha256: 503537a4d4c3639ecc839d037b21a959d9b042d3924b3c38ad7b4be69694bdc2

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

require 'benchmark'

module Ramaze
  module Helper
    ##
    # The Benchmark helper is a simple helper that can be used to benchmark
    # certain blocks of code by wrapping them in a block. This can be useful if
    # you want to see how long a certain query takes or how long it takes to
    # create a new user.
    #
    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.
      #
      # @param  [Fixnum] iterations The amount of iterations to run.
      # @return [Mixed]
      #
      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 # Bench
  end # Helper
end # Ramaze

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ramaze-2023.01.06 lib/ramaze/helper/bench.rb
ramaze-2012.12.08 lib/ramaze/helper/bench.rb
ramaze-2012.12.08b lib/ramaze/helper/bench.rb
ramaze-2012.04.14 lib/ramaze/helper/bench.rb
ramaze-2012.03.07 lib/ramaze/helper/bench.rb
ramaze-2011.12.28 lib/ramaze/helper/bench.rb
ramaze-2011.10.23 lib/ramaze/helper/bench.rb