Sha256: 5268b887f0dfa022555522981ca923d2af6bda1034fd0d525f9a412f72afaee3

Contents?: true

Size: 1.63 KB

Versions: 68

Compression:

Stored size: 1.63 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 Puppet::FileSystem.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

68 entries across 68 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-3.8.7 lib/puppet/rails/benchmark.rb
puppet-3.8.7-x86-mingw32 lib/puppet/rails/benchmark.rb
puppet-3.8.7-x64-mingw32 lib/puppet/rails/benchmark.rb
puppet-3.8.6 lib/puppet/rails/benchmark.rb
puppet-3.8.6-x86-mingw32 lib/puppet/rails/benchmark.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-3.8.6-x64-mingw32 lib/puppet/rails/benchmark.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-3.8.5 lib/puppet/rails/benchmark.rb
puppet-3.8.5-x86-mingw32 lib/puppet/rails/benchmark.rb
puppet-3.8.5-x64-mingw32 lib/puppet/rails/benchmark.rb
puppet-3.8.4 lib/puppet/rails/benchmark.rb
puppet-3.8.4-x86-mingw32 lib/puppet/rails/benchmark.rb
puppet-3.8.4-x64-mingw32 lib/puppet/rails/benchmark.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-retrospec-0.9.1 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-retrospec-0.9.0 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb
puppet-retrospec-0.8.1 vendor/gems/puppet-3.7.3/lib/puppet/rails/benchmark.rb