Class: Fable::Profiler::ProfileNode

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

Overview

Node used in the hierarchical tree of timings used by the Profiler. Each node corresponds to a single line viewable in a UI-based representation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = "") ⇒ ProfileNode

Returns a new instance of ProfileNode.



175
176
177
178
179
180
181
# File 'lib/fable/profiler.rb', line 175

def initialize(key="")
  self.key = key
  self.total_sample_count = 0
  self.total_milliseconds = 0
  self.self_sample_count = 0
  self.self_milliseconds = 0
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def key
  @key
end

#nodesObject

Returns the value of attribute nodes.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def nodes
  @nodes
end

#self_millisecondsObject

Returns the value of attribute self_milliseconds.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def self_milliseconds
  @self_milliseconds
end

#self_sample_countObject

Returns the value of attribute self_sample_count.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def self_sample_count
  @self_sample_count
end

#total_millisecondsObject

Returns the value of attribute total_milliseconds.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def total_milliseconds
  @total_milliseconds
end

#total_sample_countObject

Returns the value of attribute total_sample_count.



168
169
170
# File 'lib/fable/profiler.rb', line 168

def total_sample_count
  @total_sample_count
end

Class Method Details

.pad(io, indent) ⇒ Object



242
243
244
# File 'lib/fable/profiler.rb', line 242

def self.pad(io, indent)
  io << " " * indent
end

Instance Method Details

#add_sample(stack, duration) ⇒ Object



183
184
185
# File 'lib/fable/profiler.rb', line 183

def add_sample(stack, duration)
  add_sample_with_index(stack, -1, duration)
end

#add_sample_to_node(stack, stack_index, duration) ⇒ Object



201
202
203
204
205
206
# File 'lib/fable/profiler.rb', line 201

def add_sample_to_node(stack, stack_index, duration)
  node_key = stack[stack_index]
  nodes ||= {node_key => ProfileNode.new(node_key)}

  nodes[node_key].add_sample_with_index(stack, stack_index, duration)
end

#add_sample_with_index(stack, stack_index, duration) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/fable/profiler.rb', line 187

def add_sample_with_index(stack, stack_index, duration)
  self.total_milliseconds += 1
  self.total_milliseconds += duration

  if stack_index == (stack.size - 1)
    self.self_sample_count += 1
    self.self_milliseconds += duration
  end

  if stack_index < stack.size
    add_sample_to_node(stack, stack_index + 1, duration)
  end
end

#has_children?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/fable/profiler.rb', line 171

def has_children?
  !nodes.nil? && nodes.size > 0
end

#own_reportObject

Generates a string giving timing information for this single node, including total milliseconds spent on the piece of ink, the time spent within itself (v.s. spent in children), as well as the number of samples (instruction steps) recorded for both too.



224
225
226
227
228
229
230
231
232
233
# File 'lib/fable/profiler.rb', line 224

def own_report
  report = StringIO.new

  report << "total #{Profiler.format_milliseconds(total_milliseconds)}"
  report << ", self #{Profiler.format_milliseconds(self_milliseconds)}"
  report << " (#{self_sample_count} self samples, #{total_sample_count} total)"

  report.rewind
  report.read
end


208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fable/profiler.rb', line 208

def print_hierarchy(io, indent)
  self.class.pad(io, indent)

  io << "#{key}: #{own_report}\n"

  return if nodes.nil?

  nodes.sort_by{|k,v| v.total_milliseconds }.reverse.each do |key, node|
    node.print_hierarchy(io, indent + 1)
  end
end

#to_sObject



235
236
237
238
239
240
# File 'lib/fable/profiler.rb', line 235

def to_s
  report = StringIO.new
  print_hierarchy(report, 0)
  report.rewind
  report.read
end