diff options
author | Luke Kanies <luke@madstop.com> | 2009-04-08 18:26:36 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-04-22 14:39:36 +1000 |
commit | 042689e06bcee2882a2937b2caeab4529ae5c048 (patch) | |
tree | 498812b0648ce2008cae3a4398e27ed4736726ea /lib/puppet/rails/benchmark.rb | |
parent | 89d91392b9fb98cc14a194729bb35cb1d87a72e2 (diff) | |
download | puppet-042689e06bcee2882a2937b2caeab4529ae5c048.tar.gz puppet-042689e06bcee2882a2937b2caeab4529ae5c048.tar.xz puppet-042689e06bcee2882a2937b2caeab4529ae5c048.zip |
Adding a Rails-specific benchmarking module
This just slightly simplifies adding lots of time-debug
stuff in Rails.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/rails/benchmark.rb')
-rw-r--r-- | lib/puppet/rails/benchmark.rb | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/puppet/rails/benchmark.rb b/lib/puppet/rails/benchmark.rb new file mode 100644 index 000000000..aadacc243 --- /dev/null +++ b/lib/puppet/rails/benchmark.rb @@ -0,0 +1,69 @@ +require 'benchmark' +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 sometimes_benchmark(message) + unless Puppet::Rails::TIME_DEBUG + return yield + end + + 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) + unless time_debug? + return yield + end + + $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? + + if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty? + return + end + + $benchmarks[:accumulated][message].each do |label, value| + Puppet.debug(message + ("(%s)" % 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" + + require 'yaml' + + if FileTest.exist?(file) + data = YAML.load_file(file) + else + data = {} + end + data[branch] = $benchmarks + File.open(file, "w") { |f| f.print YAML.dump(data) } + end +end |