Class: ClassProfiler::Benchmark
- Includes:
- Singleton
- Defined in:
- lib/class_profiler/benchmark.rb
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#initialize(options = {}) ⇒ Benchmark
constructor
A new instance of Benchmark.
- #report(total_label = nil) ⇒ Object
- #reset! ⇒ Object
- #start(label, &block) ⇒ Object
- #start_and_report(label = 'Total Time', &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Benchmark
Returns a new instance of Benchmark.
6 7 8 9 10 |
# File 'lib/class_profiler/benchmark.rb', line 6 def initialize( = {}) @options = @sum_hash = {} @active_labels = [] end |
Instance Method Details
#active? ⇒ Boolean
54 55 56 |
# File 'lib/class_profiler/benchmark.rb', line 54 def active? active_labels.any? end |
#report(total_label = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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! ⇒ Object
50 51 52 |
# File 'lib/class_profiler/benchmark.rb', line 50 def reset! self.sum_hash = {} end |
#start(label, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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) ⇒ Object
28 29 30 31 32 |
# File 'lib/class_profiler/benchmark.rb', line 28 def start_and_report(label = 'Total Time', &block) bench!(label, &block) report!(label) end |