Sha256: 00551ea52ddc22117f00dee1d7bae488cfd4bc2cc006064e073e239fdc6511ee

Contents?: true

Size: 1.62 KB

Versions: 51

Compression:

Stored size: 1.62 KB

Contents

require 'benchmark'
require 'yaml'

module Puppet::Rails::Benchmark
  $benchmarks = {:accumulated => {}}

  def time_debug?
    Puppet::Rails::TIME_DEBUG
  end

  def railsmark(message)
    result = nil
    seconds = Benchmark.realtime { result = yield }
    Puppet.debug(message + " in %0.2f seconds" % seconds)

    $benchmarks[message] = seconds if time_debug?
    result
  end

  def debug_benchmark(message)
    return yield unless Puppet::Rails::TIME_DEBUG

    railsmark(message) { yield }
  end

  # Collect partial benchmarks to be logged when they're
  # all done.
  #   These are always low-level debugging so we only
  # print them if time_debug is enabled.
  def accumulate_benchmark(message, label)
    return yield unless time_debug?

    $benchmarks[:accumulated][message] ||= Hash.new(0)
    $benchmarks[:accumulated][message][label] += Benchmark.realtime { yield }
  end

  # Log the accumulated marks.
  def log_accumulated_marks(message)
    return unless time_debug?

    return if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty?

    $benchmarks[:accumulated][message].each do |label, value|
      Puppet.debug(message + ("(#{label})") + (" in %0.2f seconds" % value))
    end
  end

  def write_benchmarks
    return unless time_debug?

    branch = %x{git branch}.split("\n").find { |l| l =~ /^\*/ }.sub("* ", '')

    file = "/tmp/time_debugging.yaml"

    if FileTest.exist?(file)
      data = YAML.load_file(file)
    else
      data = {}
    end
    data[branch] = $benchmarks
    Puppet::Util.replace_file(file, 0644) { |f| f.print YAML.dump(data) }
  end
end

Version data entries

51 entries across 51 versions & 3 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/rails/benchmark.rb
puppet-parse-0.1.3 lib/vendor/puppet/rails/benchmark.rb
puppet-parse-0.1.2 lib/vendor/puppet/rails/benchmark.rb
puppet-parse-0.1.1 lib/vendor/puppet/rails/benchmark.rb
puppet-2.7.26 lib/puppet/rails/benchmark.rb
puppet-2.7.25 lib/puppet/rails/benchmark.rb
puppet-2.7.24 lib/puppet/rails/benchmark.rb
puppet-3.3.2 lib/puppet/rails/benchmark.rb
puppet-3.3.1 lib/puppet/rails/benchmark.rb
puppet-3.3.1.rc3 lib/puppet/rails/benchmark.rb
puppet-3.3.1.rc2 lib/puppet/rails/benchmark.rb
puppet-3.3.1.rc1 lib/puppet/rails/benchmark.rb
puppet-3.3.0 lib/puppet/rails/benchmark.rb
puppet-3.3.0.rc3 lib/puppet/rails/benchmark.rb
puppet-3.3.0.rc2 lib/puppet/rails/benchmark.rb
puppet-3.2.4 lib/puppet/rails/benchmark.rb
puppet-2.7.23 lib/puppet/rails/benchmark.rb
puppet-3.2.3 lib/puppet/rails/benchmark.rb
puppet-3.2.3.rc1 lib/puppet/rails/benchmark.rb
puppet-3.2.2 lib/puppet/rails/benchmark.rb