Class: Gestalt
- Inherits:
-
Object
- Object
- Gestalt
- Defined in:
- lib/gestalt.rb,
lib/gestalt/call.rb
Defined Under Namespace
Classes: Call
Constant Summary collapse
- VERSION =
'0.0.8'
Instance Attribute Summary collapse
-
#calls ⇒ Object
Returns the value of attribute calls.
Class Method Summary collapse
Instance Method Summary collapse
- #display_calls ⇒ Object
- #display_profile ⇒ Object
-
#initialize(options = {}) ⇒ Gestalt
constructor
A new instance of Gestalt.
- #run(&block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Gestalt
Returns a new instance of Gestalt.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gestalt.rb', line 12 def initialize( = {}) = { 'call' => true, 'c-call' => false, 'formatador' => Formatador.new }.merge!() @traceable_calls = [] @traceable_returns = [] if ['call'] @traceable_calls << 'call' @traceable_returns << 'return' end if ['c-call'] @traceable_calls << 'c-call' @traceable_returns << 'c-return' end @calls = [] @formatador = ['formatador'] @stack = [] @totals = {} end |
Instance Attribute Details
#calls ⇒ Object
Returns the value of attribute calls.
10 11 12 |
# File 'lib/gestalt.rb', line 10 def calls @calls end |
Class Method Details
.profile(options = {}, &block) ⇒ Object
113 114 115 116 117 |
# File 'lib/gestalt.rb', line 113 def self.profile( = {}, &block) gestalt = new() gestalt.run(&block) gestalt.display_profile end |
.trace(options = {}, &block) ⇒ Object
119 120 121 122 123 |
# File 'lib/gestalt.rb', line 119 def self.trace( = {}, &block) gestalt = new() gestalt.run(&block) gestalt.display_calls end |
Instance Method Details
#display_calls ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gestalt.rb', line 36 def display_calls @formatador.display_line condensed = [] total = 0.0 for call in @calls if condensed.last && condensed.last == call condensed.last.durations.concat(call.durations) else condensed << call end total += call.duration end for call in condensed call.display(total, @formatador) end @formatador.display_line end |
#display_profile ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gestalt.rb', line 54 def display_profile for call in calls parse_call(call) end table = [] for key, value in @totals table << { :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 end |
#run(&block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gestalt.rb', line 73 def run(&block) Kernel.set_trace_func( lambda do |event, file, line, id, binding, classname| case event when *@traceable_calls call = Gestalt::Call.new( :action => "#{classname}##{id}", :binding => binding, :location => "#{file}:#{line}" ) unless @stack.empty? @stack.last.children.push(call) end @stack.push(call) when *@traceable_returns unless @stack.empty? # we get one of these when we set the trace_func call = @stack.pop call.finish if @stack.empty? @calls << call end 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 while call = @stack.pop # leftovers, not sure why... call.finish if @stack.empty? @calls << call end end end |