Class: RorVsWild::Section

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

Constant Summary collapse

COMMAND_MAX_SIZE =
5_000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSection

Returns a new instance of Section.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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
  @async_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

Instance Attribute Details

#async_msObject

Returns the value of attribute async_ms.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def async_ms
  @async_ms
end

#callsObject

Returns the value of attribute calls.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def calls
  @calls
end

#children_msObject

Returns the value of attribute children_ms.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def children_ms
  @children_ms
end

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/rorvswild/section.rb', line 5

def commands
  @commands
end

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def file
  @file
end

#gc_start_msObject (readonly)

Returns the value of attribute gc_start_ms.



5
6
7
# File 'lib/rorvswild/section.rb', line 5

def gc_start_ms
  @gc_start_ms
end

#gc_time_msObject

Returns the value of attribute gc_time_ms.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def gc_time_ms
  @gc_time_ms
end

#kindObject

Returns the value of attribute kind.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def kind
  @kind
end

#lineObject

Returns the value of attribute line.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def line
  @line
end

#start_msObject (readonly)

Returns the value of attribute start_ms.



5
6
7
# File 'lib/rorvswild/section.rb', line 5

def start_ms
  @start_ms
end

#total_msObject

Returns the value of attribute total_ms.



6
7
8
# File 'lib/rorvswild/section.rb', line 6

def total_ms
  @total_ms
end

Class Method Details

.currentObject



27
28
29
# File 'lib/rorvswild/section.rb', line 27

def self.current
  (sections = stack) && sections.last
end

.gc_total_msObject



47
48
49
# File 'lib/rorvswild/section.rb', line 47

def self.gc_total_ms
  GC.total_time / 1_000_000.0 # nanosecond -> millisecond
end

.stackObject



23
24
25
# File 'lib/rorvswild/section.rb', line 23

def self.stack
  execution = RorVsWild.agent.current_execution and execution.section_stack
end

.start(&block) ⇒ Object



8
9
10
11
12
13
# 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_timingObject



31
32
33
34
35
36
37
38
# 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", 0
  section.add_command("GC.start")
  section.kind = "gc"
  section
end

.stop(&block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rorvswild/section.rb', line 15

def self.stop(&block)
  return if !(sections = stack) || !(section = sections.pop)
  block.call(section) if block_given?
  section.stop
  current.children_ms += section.total_ms if current
  execution = RorVsWild.agent.current_execution and execution.add_section(section)
end

.stop_gc_timing(section) ⇒ Object



40
41
42
43
44
# 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

Instance Method Details

#add_command(command) ⇒ Object



104
105
106
# File 'lib/rorvswild/section.rb', line 104

def add_command(command)
  commands << command
end

#as_json(options = nil) ⇒ Object



96
97
98
# File 'lib/rorvswild/section.rb', line 96

def as_json(options = nil)
  {calls: calls, total_runtime: total_ms, children_runtime: children_ms, async_runtime: async_ms, kind: kind, file: file, line: line, command: command}
end

#commandObject



110
111
112
113
# File 'lib/rorvswild/section.rb', line 110

def command
  string = @commands.to_a.join("\n")
  string.size > COMMAND_MAX_SIZE ? string[0, COMMAND_MAX_SIZE] + " [TRUNCATED]" : string
end

#merge(section) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/rorvswild/section.rb', line 84

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_msObject



92
93
94
# File 'lib/rorvswild/section.rb', line 92

def self_ms
  total_ms - children_ms
end

#sibling?(section) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rorvswild/section.rb', line 80

def sibling?(section)
  kind == section.kind && line == section.line && file == section.file
end

#stopObject



73
74
75
76
77
78
# File 'lib/rorvswild/section.rb', line 73

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 = {}) ⇒ Object



100
101
102
# File 'lib/rorvswild/section.rb', line 100

def to_json(options = {})
  as_json.to_json(options)
end