Class: TRuby::CompilationProfiler
- Inherits:
-
Object
- Object
- TRuby::CompilationProfiler
- Defined in:
- lib/t_ruby/cache.rb
Overview
Compilation profiler
Instance Method Summary collapse
-
#initialize ⇒ CompilationProfiler
constructor
A new instance of CompilationProfiler.
- #profile(name, &block) ⇒ Object
- #report ⇒ Object
- #reset ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ CompilationProfiler
Returns a new instance of CompilationProfiler.
763 764 765 766 |
# File 'lib/t_ruby/cache.rb', line 763 def initialize @timings = {} @call_counts = {} end |
Instance Method Details
#profile(name, &block) ⇒ Object
768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/t_ruby/cache.rb', line 768 def profile(name, &block) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = block.call elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start @timings[name] ||= 0.0 @timings[name] += elapsed @call_counts[name] ||= 0 @call_counts[name] += 1 result end |
#report ⇒ Object
782 783 784 785 786 787 788 789 |
# File 'lib/t_ruby/cache.rb', line 782 def report puts "=== Compilation Profile ===" @timings.sort_by { |_, v| -v }.each do |name, time| calls = @call_counts[name] avg = time / calls puts "#{name}: #{format("%.3f", time)}s total, #{calls} calls, #{format("%.3f", avg * 1000)}ms avg" end end |
#reset ⇒ Object
791 792 793 794 |
# File 'lib/t_ruby/cache.rb', line 791 def reset @timings.clear @call_counts.clear end |
#to_h ⇒ Object
796 797 798 799 800 801 802 803 804 805 |
# File 'lib/t_ruby/cache.rb', line 796 def to_h @timings.map do |name, time| { name: name, total_time: time, call_count: @call_counts[name], avg_time: time / @call_counts[name], } end end |