lib/gestalt.rb in gestalt-0.0.1 vs lib/gestalt.rb in gestalt-0.0.2
- old
+ new
@@ -3,46 +3,61 @@
require 'rubygems'
require 'formatador'
class Gestalt
- attr_accessor :calls
+ VERSION = '0.0.2'
+ attr_accessor :calls, :formatador
+
def initialize
@calls = []
@stack = []
@totals = {}
end
def display_calls
- Formatador.display_line
+ formatador.display_line
+ condensed = []
+ total = 0.0
for call in @calls
- call.display
+ if condensed.last && condensed.last == call
+ condensed.last.durations.concat(call.durations)
+ else
+ condensed << call
+ end
+ total += call.duration
end
- Formatador.display_line
+ for call in condensed
+ call.display(total, formatador)
+ end
+ formatador.display_line
end
def display_profile
for call in calls
parse_call(call)
end
table = []
for key, value in @totals
table << {
- '#' => value[:occurances],
- :action => key,
+ :action => "#{value[:occurances]}x #{key}",
:duration => format("%.6f", value[:duration])
}
end
table = table.sort {|x,y| y[:duration] <=> x[:duration]}
- Formatador.display_line
- Formatador.display_table(table)
- Formatador.display_line
+ formatador.display_line
+ formatador.display_table(table)
+ formatador.display_line
end
+ def formatador
+ @formatador ||= Formatador.new
+ end
+
def run(&block)
Kernel.set_trace_func(
lambda do |event, file, line, id, binding, classname|
case event
when 'call', 'c-call'
@@ -66,10 +81,11 @@
end
end
end
)
yield
+ rescue StandardError, Interrupt
Kernel.set_trace_func(nil)
@stack.pop # pop Kernel#set_trace_func(nil)
unless @stack.empty?
@stack.last.children.pop # pop Kernel#set_trace_func(nil)
end
@@ -105,33 +121,44 @@
end
if __FILE__ == $0
+ Gestalt.trace do
+ raise StandardError.new('exception')
+ slow = Slow.new
+ slow.slowing
+ end
+
class Slow
- def single
- 'slow' << 'er'
+ def slow(est = false)
+ unless est
+ 'slow' << 'e' << 'r'
+ else
+ 'slow' << 'e' << 's' << 't'
+ end
end
- def double
- single
- single
+ def slowing
+ slow
+ slow
+ slow(true)
end
end
Gestalt.trace do
slow = Slow.new
- slow.single
+ slow.slowing
end
Gestalt.profile do
slow = Slow.new
- slow.double
+ slow.slowing
end
end