Class: Stickshift::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/stickshift.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options, *args)
  @depth, @options, @args = 1, options, 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

#depthObject (readonly)

Returns the value of attribute depth.



12
13
14
# File 'lib/stickshift.rb', line 12

def depth
  @depth
end

Class Method Details

.currentObject



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

#childrenObject



59
60
61
# File 'lib/stickshift.rb', line 59

def children
  @children ||= []
end

#elapsedObject



63
64
65
# File 'lib/stickshift.rb', line 63

def elapsed
  @elapsed ||= 0
end

#enabled?Boolean

Returns:

  • (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

#realtimeObject



49
50
51
52
53
# File 'lib/stickshift.rb', line 49

def realtime
  start = Time.now
  yield
  Time.now - start
end

#reportObject



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_formatObject



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_timeObject



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_timeObject



89
90
91
# File 'lib/stickshift.rb', line 89

def total_time
  ms(elapsed)
end