Class: Stickshift::Timer
- Inherits:
-
Object
- Object
- Stickshift::Timer
- Defined in:
- lib/stickshift.rb
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
Class Method Summary collapse
Instance Method Summary collapse
- #add(child) ⇒ Object
- #children ⇒ Object
- #elapsed ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(obj, meth, options, *args) ⇒ Timer
constructor
A new instance of Timer.
- #invoke(&block) ⇒ Object
- #ms(t) ⇒ Object
- #realtime ⇒ Object
- #report ⇒ Object
- #self_format ⇒ Object
- #self_time ⇒ Object
- #total_time ⇒ Object
Constructor Details
#initialize(obj, meth, options, *args) ⇒ Timer
Returns a new instance of Timer.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/stickshift.rb', line 14 def initialize(obj, meth, , *args) @depth, @options, @args = 1, , args @label = if @options[:label] @options[:label] else klass = Class === obj ? "#{obj.name}." : "#{obj.class.name}#" "#{klass}#{meth}" end @label << "{#{obj.inspect}}" if @options[:inspect_self] @label << "(#{@args[@options[:with_args]].inspect})" if @options[:with_args] if @parent = Timer.current @parent.add(self) @depth = @parent.depth + 1 end end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
12 13 14 |
# File 'lib/stickshift.rb', line 12 def depth @depth end |
Class Method Details
.current ⇒ Object
10 |
# File 'lib/stickshift.rb', line 10 def self.current; Thread.current['__stickshift']; end |
.current=(timer) ⇒ Object
11 |
# File 'lib/stickshift.rb', line 11 def self.current=(timer); Thread.current['__stickshift'] = timer; end |
Instance Method Details
#add(child) ⇒ Object
55 56 57 |
# File 'lib/stickshift.rb', line 55 def add(child) children << child end |
#children ⇒ Object
59 60 61 |
# File 'lib/stickshift.rb', line 59 def children @children ||= [] end |
#elapsed ⇒ Object
63 64 65 |
# File 'lib/stickshift.rb', line 63 def elapsed @elapsed ||= 0 end |
#enabled? ⇒ Boolean
30 31 32 |
# File 'lib/stickshift.rb', line 30 def enabled? Stickshift.enabled && (Timer.current || Stickshift.top_level_trigger.nil? || @options[:top_level]) end |
#invoke(&block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/stickshift.rb', line 34 def invoke(&block) return block.call unless enabled? begin Timer.current = self result = nil @elapsed = realtime do result = block.call end result ensure Timer.current = @parent report unless @parent end end |
#ms(t) ⇒ Object
72 73 74 |
# File 'lib/stickshift.rb', line 72 def ms(t) (t * 1000) end |
#realtime ⇒ Object
49 50 51 52 53 |
# File 'lib/stickshift.rb', line 49 def realtime start = Time.now yield Time.now - start end |
#report ⇒ Object
67 68 69 70 |
# File 'lib/stickshift.rb', line 67 def report Stickshift.output.puts "#{self_format % self_time}ms >#{' ' * @depth}#{@label} < #{total_time}ms" children.each {|c| c.report} end |
#self_format ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/stickshift.rb', line 76 def self_format @self_format ||= if @parent @parent.self_format else width = ("%0.2f" % total_time).length "%#{width}.2f" end end |
#self_time ⇒ Object
85 86 87 |
# File 'lib/stickshift.rb', line 85 def self_time @self_time ||= ms(elapsed - children.inject(0) {|sum,el| sum += el.elapsed}) end |
#total_time ⇒ Object
89 90 91 |
# File 'lib/stickshift.rb', line 89 def total_time ms(elapsed) end |