Module: Puppet::Rails::Benchmark

Included in:
Host, Host, Resource
Defined in:
lib/puppet/rails/benchmark.rb

Instance Method Summary collapse

Instance Method Details

#accumulate_benchmark(message, label) ⇒ Object

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.



28
29
30
31
32
33
# File 'lib/puppet/rails/benchmark.rb', line 28

def accumulate_benchmark(message, label)
  return yield unless time_debug?

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

#debug_benchmark(message) ⇒ Object



18
19
20
21
22
# File 'lib/puppet/rails/benchmark.rb', line 18

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

  railsmark(message) { yield }
end

#log_accumulated_marks(message) ⇒ Object

Log the accumulated marks.



36
37
38
39
40
41
42
43
44
# File 'lib/puppet/rails/benchmark.rb', line 36

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

#railsmark(message) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/puppet/rails/benchmark.rb', line 9

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

#time_debug?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/puppet/rails/benchmark.rb', line 5

def time_debug?
  Puppet::Rails::TIME_DEBUG
end

#write_benchmarksObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/rails/benchmark.rb', line 46

def write_benchmarks
  return unless time_debug?

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

  file = "/tmp/time_debugging.yaml"

  require 'yaml'

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