lib/profile.rb


DEFINITIONS

This source file includes following functions.


   1  
   2  module Profiler__
   3    Times = if defined? Process.times then Process else Time end
   4    Start = Float(Times::times[0])
   5    top = "toplevel".intern
   6    Stack = [[0, 0, top]]
   7    MAP = {"#toplevel" => [1, 0, 0, "#toplevel"]}
   8  
   9    p = proc{|event, file, line, id, binding, klass|
  10      case event
  11      when "call", "c-call"
  12        now = Float(Times::times[0])
  13        Stack.push [now, 0.0, id]
  14      when "return", "c-return"
  15        now = Float(Times::times[0])
  16        tick = Stack.pop
  17        name = klass.to_s
  18        if name.nil? then name = '' end
  19        if klass.kind_of? Class
  20          name += "#"
  21        else
  22          name += "."
  23        end
  24        name += id.id2name
  25        data = MAP[name]
  26        unless data
  27          data = [0.0, 0.0, 0.0, name]
  28          MAP[name] = data
  29        end
  30        data[0] += 1
  31        cost = now - tick[0]
  32        data[1] += cost
  33        data[2] += cost - tick[1]
  34        Stack[-1][1] += cost
  35      end
  36    }
  37    END {
  38      set_trace_func nil
  39      total = Float(Times::times[0]) - Start
  40      if total == 0 then total = 0.01 end
  41      MAP["#toplevel"][1] = total
  42  #    f = open("./rmon.out", "w")
  43      f = STDERR
  44      data = MAP.values.sort!{|a,b| b[2] <=> a[2]}
  45      sum = 0
  46      f.printf "  %%   cumulative   self              self     total\n"           
  47      f.printf " time   seconds   seconds    calls  ms/call  ms/call  name\n"
  48      for d in data
  49        sum += d[2]
  50        f.printf "%6.2f %8.2f  %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
  51        f.printf "%8.2f %8.2f  %s\n", d[2]*1000/d[0], d[1]*1000/d[0], d[3]
  52      end
  53      f.close
  54    }
  55    set_trace_func p
  56  end