Class: Yuslow::Investigation

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

Instance Method Summary collapse

Constructor Details

#initialize(debug: false, printer: nil, max_depth:) ⇒ Investigation

Returns a new instance of Investigation.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/yuslow/investigation.rb', line 3

def initialize(debug: false, printer: nil, max_depth:)
  if max_depth
    raise ArgumentError, 'max_depth must be a number'      if !max_depth.is_a? Numeric
    raise ArgumentError, 'max_depth cannot be less than 1' if max_depth < 1
  end

  @printer = printer
  @debug = debug
  @tracing = false
  @trace = nil
  @max_depth = max_depth
  @execution = {}
end

Instance Method Details

#debug(trace_point, indent) ⇒ Object



69
70
71
72
73
# File 'lib/yuslow/investigation.rb', line 69

def debug(trace_point, indent)
  return unless @debug

  puts (' ' * indent) + "#{trace_point.event} #{trace_point.defined_class}##{trace_point.callee_id}"
end

#finishObject



63
64
65
66
67
# File 'lib/yuslow/investigation.rb', line 63

def finish
  @trace.disable

  print_result @printer if @printer
end


75
76
77
78
79
80
81
82
83
84
# File 'lib/yuslow/investigation.rb', line 75

def print_result(printer)
  return unless printer

  operations =
    @execution.keys.map do |thread_id|
      @execution[thread_id][:root]
    end.compact

  printer.execute operations
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yuslow/investigation.rb', line 17

def start
  @trace = TracePoint.new(:call, :return, :c_call, :c_return) do |trace_point|
    thread_id = Thread.current.object_id
    @execution[thread_id] ||= {root: nil, current: nil, depth: 0}

    if trace_point.defined_class == self.class
      if trace_point.callee_id == :start
        @tracing = true
      elsif trace_point.callee_id == :finish
        @tracing = false
      else
        raise "Unexpected method #{trace_point.callee_id} called on #{self.class}"
      end
    elsif @tracing
      if %i(call c_call).include?(trace_point.event)
        if !@max_depth || @execution[thread_id][:depth] < @max_depth
          if @execution[thread_id][:current]
            @execution[thread_id][:current] = @execution[thread_id][:current].fork object: trace_point.defined_class,
                                                                                   method: trace_point.callee_id
          else
            operation = Operation.new object: trace_point.defined_class, method: trace_point.callee_id
            @execution[thread_id][:root]    = operation
            @execution[thread_id][:current] = operation
          end

          debug trace_point, @execution[thread_id][:depth]
          @execution[thread_id][:depth] += 1
        end
      elsif %i(return c_return).include? trace_point.event
        if @execution[thread_id][:current]&.identifier == "#{trace_point.defined_class}##{trace_point.callee_id}"
          @execution[thread_id][:depth] -= 1

          if !@max_depth || @execution[thread_id][:depth] < @max_depth
            debug trace_point, @execution[thread_id][:depth]

            @execution[thread_id][:current]&.complete
            @execution[thread_id][:current] = @execution[thread_id][:current]&.parent
          end
        end
      end
    end
  end

  @trace.enable
end