diff options
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r-- | lib/puppet/rails/benchmark.rb | 69 | ||||
-rw-r--r-- | lib/puppet/rails/host.rb | 75 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 2 |
3 files changed, 107 insertions, 39 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 diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb index 8f4c7375c..578974555 100644 --- a/lib/puppet/rails/host.rb +++ b/lib/puppet/rails/host.rb @@ -1,9 +1,12 @@ require 'puppet/rails/resource' require 'puppet/rails/fact_name' require 'puppet/rails/source_file' +require 'puppet/rails/benchmark' require 'puppet/util/rails/collection_merger' class Puppet::Rails::Host < ActiveRecord::Base + include Puppet::Rails::Benchmark + extend Puppet::Rails::Benchmark include Puppet::Util include Puppet::Util::CollectionMerger @@ -39,38 +42,42 @@ class Puppet::Rails::Host < ActiveRecord::Base args = {} host = nil - transaction do - #unless host = find_by_name(name) - seconds = Benchmark.realtime { - unless host = find_by_name(node.name) - host = new(:name => node.name) + railsmark "Stored node" do + transaction do + #unless host = find_by_name(name) + + sometimes_benchmark("Searched for host")do + unless host = find_by_name(node.name) + host = new(:name => node.name) + end + end + if ip = node.parameters["ipaddress"] + host.ip = ip end - } - Puppet.debug("Searched for host in %0.2f seconds" % seconds) - if ip = node.parameters["ipaddress"] - host.ip = ip - end - if env = node.environment - host.environment = env - end + if env = node.environment + host.environment = env + end - # Store the facts into the database. - host.merge_facts(node.parameters) + # Store the facts into the database. + host.merge_facts(node.parameters) - seconds = Benchmark.realtime { - host.merge_resources(resources) - } - Puppet.debug("Handled resources in %0.2f seconds" % seconds) + sometimes_benchmark("Handled resources") { + host.merge_resources(resources) + } - host.last_compile = Time.now + host.last_compile = Time.now + + sometimes_benchmark("Saved host") { + host.save + } + end - seconds = Benchmark.realtime { - host.save - } - Puppet.debug("Saved host in %0.2f seconds" % seconds) end + # This only runs if time debugging is enabled. + write_benchmarks + return host end @@ -139,20 +146,17 @@ class Puppet::Rails::Host < ActiveRecord::Base # Set our resources. def merge_resources(list) resources_by_id = nil - seconds = Benchmark.realtime { + sometimes_benchmark("Searched for resources") { resources_by_id = find_resources() } - Puppet.debug("Searched for resources in %0.2f seconds" % seconds) - seconds = Benchmark.realtime { + sometimes_benchmark("Searched for resource params and tags") { find_resources_parameters_tags(resources_by_id) } if id - Puppet.debug("Searched for resource params and tags in %0.2f seconds" % seconds) - seconds = Benchmark.realtime { + sometimes_benchmark("Performed resource comparison") { compare_to_catalog(resources_by_id, list) } - Puppet.debug("Resource comparison took %0.2f seconds" % seconds) end def find_resources @@ -179,24 +183,21 @@ class Puppet::Rails::Host < ActiveRecord::Base end resources = nil - seconds = Benchmark.realtime { + sometimes_benchmark("Resource removal") { resources = remove_unneeded_resources(compiled, existing) } - Puppet.debug("Resource removal took %0.2f seconds" % seconds) # Now for all resources in the catalog but not in the db, we're pretty easy. additions = nil - seconds = Benchmark.realtime { + sometimes_benchmark("Resource merger") { additions = perform_resource_merger(compiled, resources) } - Puppet.debug("Resource merger took %0.2f seconds" % seconds) - seconds = Benchmark.realtime { + sometimes_benchmark("Resource addition") { additions.each do |resource| build_rails_resource_from_parser_resource(resource) end } - Puppet.debug("Resource addition took %0.2f seconds" % seconds) end def add_new_resources(additions) @@ -271,7 +272,7 @@ class Puppet::Rails::Host < ActiveRecord::Base end # We need to use 'destroy' here, not 'delete', so that all # dependent objects get removed, too. - Puppet::Rails::Resource.destroy(*deletions) unless deletions.empty? + Puppet::Rails::Resource.destroy(deletions) unless deletions.empty? return resources end diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index d91bb8209..ef1317871 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -108,7 +108,6 @@ class Puppet::Rails::Resource < ActiveRecord::Base db_params = {} deletions = [] - #Puppet::Rails::ParamValue.find_all_params_from_resource(self).each do |value| @params_hash.each do |value| # First remove any parameters our catalog resource doesn't have at all. deletions << value['id'] and next unless catalog_params.include?(value['name']) @@ -149,7 +148,6 @@ class Puppet::Rails::Resource < ActiveRecord::Base in_db = [] deletions = [] resource_tags = resource.tags - #Puppet::Rails::ResourceTag.find_all_tags_from_resource(self).each do |tag| @tags_hash.each do |tag| deletions << tag['id'] and next unless resource_tags.include?(tag['name']) in_db << tag['name'] |