Records the time spent per filter and per item representation
@see Listener#enable_for?
# File lib/nanoc/cli/commands/compile.rb, line 158 def self.enable_for?(command_runner) command_runner.options.fetch(:verbose, false) end
@option params [Array<Nanoc::ItemRep>] :reps The list of item representations in the site
# File lib/nanoc/cli/commands/compile.rb, line 163 def initialize(params = {}) @times = {} @reps = params.fetch(:reps) end
@see Listener#start
# File lib/nanoc/cli/commands/compile.rb, line 170 def start Nanoc::NotificationCenter.on(:filtering_started) do |rep, filter_name| @times[filter_name] ||= [] @times[filter_name] << { :start => Time.now } end Nanoc::NotificationCenter.on(:filtering_ended) do |rep, filter_name| @times[filter_name].last[:stop] = Time.now end end
@see Listener#stop
# File lib/nanoc/cli/commands/compile.rb, line 181 def stop print_profiling_feedback super end
# File lib/nanoc/cli/commands/compile.rb, line 246 def durations_for_filter(filter_name) result = [] @times[filter_name].each do |sample| if sample[:start] && sample[:stop] result << sample[:stop] - sample[:start] end end result end
# File lib/nanoc/cli/commands/compile.rb, line 233 def durations_per_filter @_durations_per_filter ||= begin result = {} @times.keys.each do |filter_name| durations = durations_for_filter(filter_name) if durations result[filter_name] = durations end end result end end
# File lib/nanoc/cli/commands/compile.rb, line 188 def print_profiling_feedback # Get max filter length max_filter_name_length = durations_per_filter.keys.map { |k| k.to_s.size }.max return if max_filter_name_length.nil? # Print warning if necessary if @reps.any? { |r| !r.compiled? } $stderr.puts $stderr.puts 'Warning: profiling information may not be accurate because ' + 'some items were not compiled.' end # Print header puts puts ' ' * max_filter_name_length + ' | count min avg max tot' puts '-' * max_filter_name_length + '-+-----------------------------------' durations_per_filter.to_a.sort_by { |r| r[1] }.each do |row| print_row(row, max_filter_name_length) end end
# File lib/nanoc/cli/commands/compile.rb, line 210 def print_row(row, length) # Extract data filter_name, samples = *row # Calculate stats count = samples.size min = samples.min tot = samples.reduce(0) { |memo, i| memo + i } avg = tot / count max = samples.max # Format stats count = format('%4d', count) min = format('%4.2f', min) avg = format('%4.2f', avg) max = format('%4.2f', max) tot = format('%5.2f', tot) # Output stats key = format("%#{length}s", filter_name) puts "#{key} | #{count} #{min}s #{avg}s #{max}s #{tot}s" end
Generated with the Darkfish Rdoc Generator 2.