class RorVsWild::Section

Constants

COMMAND_MAX_SIZE

Attributes

calls[RW]
children_ms[RW]
commands[R]
file[RW]
gc_start_ms[R]
gc_time_ms[RW]
kind[RW]
line[RW]
start_ms[R]
total_ms[RW]

Public Class Methods

current() click to toggle source
# File lib/rorvswild/section.rb, line 27
def self.current
  (sections = stack) && sections.last
end
gc_total_ms() click to toggle source
# File lib/rorvswild/section.rb, line 47
def self.gc_total_ms
  GC.total_time / 1_000_000.0 # nanosecond -> millisecond
end
new() click to toggle source
# File lib/rorvswild/section.rb, line 56
def initialize
  @start_ms = RorVsWild.clock_milliseconds
  @end_ms = nil
  @gc_start_ms = Section.gc_total_ms
  @gc_end_ms = nil
  @gc_time_ms = 0
  @calls = 1
  @total_ms = 0
  @children_ms = 0
  @kind = "code"
  location = RorVsWild.agent.locator.find_most_relevant_location(caller_locations)
  @file = RorVsWild.agent.locator.relative_path(location.path)
  @line = location.lineno
  @commands = Set.new
end
stack() click to toggle source
# File lib/rorvswild/section.rb, line 23
def self.stack
  (data = RorVsWild.agent.current_data) && data[:section_stack]
end
start(&block) click to toggle source
# File lib/rorvswild/section.rb, line 8
def self.start(&block)
  section = Section.new
  block.call(section) if block_given?
  stack && stack.push(section)
  section
end
start_gc_timing() click to toggle source
# File lib/rorvswild/section.rb, line 31
def self.start_gc_timing
  section = Section.new
  section.calls = GC.count
  section.file, section.line = "ruby/gc.c", 42
  section.add_command("GC.start")
  section.kind = "gc"
  section
end
stop(&block) click to toggle source
# File lib/rorvswild/section.rb, line 15
def self.stop(&block)
  return unless stack && section = stack.pop
  block.call(section) if block_given?
  section.stop
  current.children_ms += section.total_ms if current
  RorVsWild.agent.add_section(section)
end
stop_gc_timing(section) click to toggle source
# File lib/rorvswild/section.rb, line 40
def self.stop_gc_timing(section)
  section.total_ms = gc_total_ms - section.gc_start_ms
  section.calls = GC.count - section.calls
  section
end

Public Instance Methods

add_command(command) click to toggle source
# File lib/rorvswild/section.rb, line 103
def add_command(command)
  commands << command
end
as_json(options = nil) click to toggle source
# File lib/rorvswild/section.rb, line 95
def as_json(options = nil)
  {calls: calls, total_runtime: total_ms, children_runtime: children_ms, kind: kind, started_at: start_ms, file: file, line: line, command: command}
end
command() click to toggle source
# File lib/rorvswild/section.rb, line 109
def command
  string = @commands.to_a.join("\n")
  string.size > COMMAND_MAX_SIZE ? string[0, COMMAND_MAX_SIZE] + " [TRUNCATED]" : string
end
merge(section) click to toggle source
# File lib/rorvswild/section.rb, line 83
def merge(section)
  self.calls += section.calls
  self.total_ms += section.total_ms
  self.children_ms += section.children_ms
  self.gc_time_ms += section.gc_time_ms
  commands.merge(section.commands)
end
self_ms() click to toggle source
# File lib/rorvswild/section.rb, line 91
def self_ms
  total_ms - children_ms
end
sibling?(section) click to toggle source
# File lib/rorvswild/section.rb, line 79
def sibling?(section)
  kind == section.kind && line == section.line && file == section.file
end
stop() click to toggle source
# File lib/rorvswild/section.rb, line 72
def stop
  @gc_end_ms = self.class.gc_total_ms
  @gc_time_ms = @gc_end_ms - @gc_start_ms
  @end_ms = RorVsWild.clock_milliseconds
  @total_ms = @end_ms - @start_ms - gc_time_ms
end
to_json(options = {}) click to toggle source
# File lib/rorvswild/section.rb, line 99
def to_json(options = {})
  as_json.to_json(options)
end