Class: Profiler::Entry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ Entry

Returns a new instance of Entry.



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

def initialize(format)
  @format = format
  @start  = Time.now
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



60
61
62
# File 'lib/log_and_profile.rb', line 60

def duration
  @duration
end

Instance Method Details

#add_child(child) ⇒ Object



84
85
86
87
# File 'lib/log_and_profile.rb', line 84

def add_child(child)
  @children ||= []
  @children << child
end

#dump(level = 0) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/log_and_profile.rb', line 97

def dump(level = 0)
  self.finished!

  STDERR.puts indent(level) + (@format % @duration)

  if @groups
    @groups.sort_by { |group, info| info[:duration] }.reverse.each do |group, info|
      STDERR.puts indent(level+1) + "[#{group}: %.3f seconds, called #{info[:count]} time(s), %.3f seconds/time]" % [ info[:duration], info[:duration] / info[:count] ]
    end
  end

  if @children
    @children.sort_by { |child| child.duration }.reverse.each do |child|
      child.dump(level + 1)
    end
  end
end

#finished!Object



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

def finished!
  @duration ||= Time.now - @start
end

#group(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/log_and_profile.rb', line 67

def group(name)
  @groups ||= {}
  @groups[name] ||= { :duration => 0, :count => 0 }

  previous_group, @current_group = @current_group, name

  start = Time.now
  res = yield
  @groups[name][:duration] += Time.now - start
  @groups[name][:count] += 1

  @groups[previous_group][:duration] -= Time.now - start if previous_group
  @current_group = previous_group

  res
end

#indent(level) ⇒ Object



93
94
95
# File 'lib/log_and_profile.rb', line 93

def indent(level)
  '  ' * level
end