Class: ZenProfiler

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/zenprofile.rb

Constant Summary collapse

@@start =
@@stack = @@map = nil

Class Method Summary collapse

Class Method Details



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zenprofile.rb', line 23

def self.print_profile(f)
  stop_profile
  total = self.instance.time_now - @@start
  if total == 0 then total = 0.01 end
  @@map["#toplevel"][1] = total
  data = @@map.values
  data.sort!{|a,b| b[2] <=> a[2]} # TODO: change to sort_by
  sum = 0

  f.printf "  %%   cumulative   self              self     total\n"           
  f.printf " time   seconds   seconds    calls  ms/call  ms/call  name\n"
  for d in data
    sum += d[2]
    klass = d[3].first
    meth  = d[3].last.to_s

    signature = if klass.nil?
                  meth
                elsif klass.kind_of?(Class)
                  klass.to_s.sub(/#<\S+:(\S+)>/, '\\1') + "#" + meth
                else
                  klass.class.name + "." + meth
                end

    f.printf "%6.2f %8.2f  %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
    f.printf "%8.2f %8.2f  %s\n", d[2]*1000/d[0], d[1]*1000/d[0], signature
  end
end

.start_profileObject



12
13
14
15
16
17
# File 'lib/zenprofile.rb', line 12

def self.start_profile
  @@start = self.instance.time_now
  @@stack = [[0, 0, [nil, :toplevel]], [0, 0, [nil, :dummy]]]
  @@map = {"#toplevel" => [1, 0.0, 0.0, [nil, "#toplevel"]]}
  self.instance.add_event_hook
end

.stop_profileObject



19
20
21
# File 'lib/zenprofile.rb', line 19

def self.stop_profile
  self.instance.remove_event_hook
end