class ClassProfiler::Benchmark
Attributes
active_labels[RW]
options[RW]
sum_hash[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/class_profiler/benchmark.rb, line 6 def initialize(options = {}) @options = options @sum_hash = {} @active_labels = [] end
Public Instance Methods
active?()
click to toggle source
# File lib/class_profiler/benchmark.rb, line 54 def active? active_labels.any? end
report(total_label = nil)
click to toggle source
# File lib/class_profiler/benchmark.rb, line 34 def report(total_label = nil) printf "######### Performance Report #########\n" if sum_hash[total_label] total_time = sum_hash[total_label][:sum].round(5) puts total_time sum_hash.sort_by{|label, values| values[:sum]}.to_h.each{|label, values| printf "%-150s %s (%s)\n", "#{label} (total time):", values[:sum].round(5), "#{((values[:sum]/ total_time) * 100).round(1)}%" printf "%-150s %s\n", "#{label} (number of calls):", values[:num] printf "%-150s %s\n\n", "#{label} (average time):", (values[:sum]/values[:num]).round(5) } end printf "\n######### (most time consuming method is at the bottom) #########" reset! end
reset!()
click to toggle source
# File lib/class_profiler/benchmark.rb, line 50 def reset! self.sum_hash = {} end
start(label, &block)
click to toggle source
# File lib/class_profiler/benchmark.rb, line 12 def start(label, &block) append_active_label(label) value = nil time = ::Benchmark.measure { value = block.call }.real sum_hash[label] = {num: 0, sum: 0} if sum_hash[label].nil? sum_hash[label][:num] += 1 sum_hash[label][:sum] += time.round(5) remove_active_label(label) return value end
start_and_report(label = 'Total Time', &block)
click to toggle source
# File lib/class_profiler/benchmark.rb, line 28 def start_and_report(label = 'Total Time', &block) bench!(label, &block) report!(label) end
Private Instance Methods
append_active_label(label)
click to toggle source
# File lib/class_profiler/benchmark.rb, line 60 def append_active_label(label) active_labels << label end
remove_active_label(label)
click to toggle source
# File lib/class_profiler/benchmark.rb, line 64 def remove_active_label(label) self.active_labels = active_labels.select{|i| i != label} end