Class: RubyProf::SpeedscopePrinter

Inherits:
AbstractPrinter
  • Object
show all
Defined in:
lib/ruby-prof-speedscope.rb

Instance Method Summary collapse

Instance Method Details



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby-prof-speedscope.rb', line 64

def print_call_tree(call_tree, frames, start_time, root = false)
  @output << <<~BEGINEVENT
    {
      "type": "O",
      "frame": #{frames[call_tree.target.object_id]},
      "at": #{JSON.dump(start_time)}
    },
  BEGINEVENT

  original_start_time = start_time
  start_time += call_tree.self_time
  call_tree.children.each do |child_tree|
    next if child_tree.total_time < 0
    print_call_tree(child_tree, frames, start_time)
    start_time += child_tree.total_time
  end

  @output << <<~CLOSEEVENT
    {
      "type": "C",
      "frame": #{frames[call_tree.target.object_id]},
      "at": #{JSON.dump(original_start_time + call_tree.total_time)}
    }#{root ? "" : ","}
  CLOSEEVENT
end


5
6
7
8
9
10
11
12
13
14
15
16
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
62
# File 'lib/ruby-prof-speedscope.rb', line 5

def print_threads
  profiles = []
  @output << <<~HEADER
  {
    "$schema": "https://www.speedscope.app/file-format-schema.json",
    "exporter": "ruby-prof-speedscope",
    "shared": {
      "frames": [
  HEADER

  frames = {}
  frame_index = 0
  @result.threads.each do |thread|
    thread.methods.each_with_index do |method, idx|
      next if frames.has_key?(method.object_id)
      name = "#{method.klass_name}##{method.method_name}"
      name += " *recursive*" if method.recursive?
      @output << <<~FRAME
        {
          "name": "#{name}",
          "file": "#{method.source_file}",
          "line": "#{method.line}"
        },
      FRAME
      frames[method.object_id] = frame_index
      frame_index += 1
    end
  end

  @output << <<~FRAMES
    {"name": "dummy_trailing_comma"}
    ]
    },
    "profiles": [
  FRAMES

  @result.threads.each_with_index do |thread, idx|
    @output << <<~PROFILES
      {
        "type": "evented",
        "name": "Thread: #{thread.id}, Fiber: #{thread.fiber_id}",
        "unit": "seconds",
        "startValue": 0,
        "endValue": #{JSON.dump(thread.call_tree.measurement.total_time)},
        "events": [
    PROFILES
    print_call_tree(thread.call_tree, frames, 0.0, true)
    @output << <<~PROFILES
      ]
      }#{idx < @result.threads.length - 1 ? "," : ""}
    PROFILES
  end

  @output << <<~ENDING
    ]
    }
  ENDING
end