class Nanoc::CLI::Commands::Compile::TimingRecorder
Records the time spent per filter and per item representation
Public Class Methods
enable_for?(command_runner)
click to toggle source
@see Listener#enable_for?
# File lib/nanoc/cli/commands/compile.rb, line 144 def self.enable_for?(command_runner) command_runner.options.fetch(:verbose, false) end
new(reps:)
click to toggle source
@param [Enumerable<Nanoc::Int::ItemRep>] reps
# File lib/nanoc/cli/commands/compile.rb, line 149 def initialize(reps)) @times = {} @reps = reps end
Public Instance Methods
start()
click to toggle source
@see Nanoc::CLI::Commands::Compile::Listener#start
# File lib/nanoc/cli/commands/compile.rb, line 156 def start Nanoc::Int::NotificationCenter.on(:filtering_started) do |_rep, filter_name| @times[filter_name] ||= [] @times[filter_name] << { start: Time.now } end Nanoc::Int::NotificationCenter.on(:filtering_ended) do |_rep, filter_name| @times[filter_name].last[:stop] = Time.now end end
stop()
click to toggle source
@see Nanoc::CLI::Commands::Compile::Listener#stop
Calls superclass method
Nanoc::CLI::Commands::Compile::Listener#stop
# File lib/nanoc/cli/commands/compile.rb, line 167 def stop print_profiling_feedback super end
Protected Instance Methods
durations_for_filter(filter_name)
click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 232 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
durations_per_filter()
click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 219 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
print_profiling_feedback()
click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 174 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
print_row(row, length)
click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 196 def print_row(row, length) # Extract data filter_name, samples = *row # Calculate stats count = samples.size min = samples.min tot = samples.reduce(0) { |a, e| a + e } 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