Class: Gonzui::PerformanceMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/gonzui/monitor.rb

Constant Summary collapse

@@performance_counters =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerformanceMonitor

Returns a new instance of PerformanceMonitor.



16
17
18
19
20
# File 'lib/gonzui/monitor.rb', line 16

def initialize
  @start_time = Time.now
  @elapsed_time = nil
  @finished_p = false
end

Class Method Details

.[](label) ⇒ Object



38
39
40
# File 'lib/gonzui/monitor.rb', line 38

def self.[] (label)
  @@performance_counters[label]
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/gonzui/monitor.rb', line 42

def empty?
  @@performance_counters.empty?
end

#format(primary_label, *labels) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gonzui/monitor.rb', line 93

def format(primary_label, *labels)
  finish unless @finished_p

  label = build_label(*primary_label)
  primary_pc = Gonzui::PerformanceMonitor[label]
  return "" if primary_pc.nil?

  summary = primary_pc.summary(label, 1, @elapsed_time)
  labels.each {|label|
    label = build_label(*label)
    pc = Gonzui::PerformanceMonitor[label]
    next if pc.nil?
    summary  << pc.summary(label, 2, @elapsed_time)
    primary_pc.exclude(pc)
  }
  summary << primary_pc.rest_summary(:other, 2, @elapsed_time)
  summary << "\n"
  return summary
end

#headingObject



89
90
91
# File 'lib/gonzui/monitor.rb', line 89

def heading
  return Gonzui::PerformanceCounter.heading
end

#profile(m, name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gonzui/monitor.rb', line 46

def profile(m, name)
  label = build_label(m, name)
  if !@@performance_counters[label]
    @@performance_counters[label] = PerformanceCounter.new
    case m
    when Class
      eval <<"End"
      class #{m}
        alias orig_#{name} #{name}
        @@pc_#{name} = Gonzui::PerformanceMonitor["#{label}"]
        # redefine with profiler
        def #{name}(*args, &block)
          @@pc_#{name}.enter
          begin
            orig_#{name}(*args, &block)
          ensure
            @@pc_#{name}.leave
          end
        end
      end
End
    when Module
      eval <<"End"
      module #{m}
        alias orig_#{name} #{name}
        module_function :orig_#{name}
        # redefine with profiler
        def #{name}(*args, &block)
          pc = Gonzui::PerformanceMonitor["#{label}"]
          pc.enter
          begin
            orig_#{name}(*args, &block)
          ensure
            pc.leave
          end
        end
        module_function :#{name}
      end
End
    end
  end
end