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 << "    {\n      \"type\": \"O\",\n      \"frame\": \#{frames[call_tree.target.object_id]},\n      \"at\": \#{JSON.dump(start_time)}\n    },\n  BEGINEVENT\n\n  original_start_time = start_time\n  start_time += call_tree.self_time\n  call_tree.children.each do |child_tree|\n    next if child_tree.total_time < 0\n    print_call_tree(child_tree, frames, start_time)\n    start_time += child_tree.total_time\n  end\n\n  @output << <<~CLOSEEVENT\n    {\n      \"type\": \"C\",\n      \"frame\": \#{frames[call_tree.target.object_id]},\n      \"at\": \#{JSON.dump(original_start_time + call_tree.total_time)}\n    }\#{root ? \"\" : \",\"}\n  CLOSEEVENT\nend\n"


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 << "  {\n    \"$schema\": \"https://www.speedscope.app/file-format-schema.json\",\n    \"exporter\": \"ruby-prof-speedscope\",\n    \"shared\": {\n      \"frames\": [\n  HEADER\n\n  frames = {}\n  frame_index = 0\n  @result.threads.each do |thread|\n    thread.methods.each_with_index do |method, idx|\n      next if frames.has_key?(method.object_id)\n      name = \"\#{method.klass_name}#\#{method.method_name}\"\n      name += \" *recursive*\" if method.recursive?\n      @output << <<~FRAME\n        {\n          \"name\": \"\#{name}\",\n          \"file\": \"\#{method.source_file}\",\n          \"line\": \"\#{method.line}\"\n        },\n      FRAME\n      frames[method.object_id] = frame_index\n      frame_index += 1\n    end\n  end\n\n  @output << <<~FRAMES\n    {\"name\": \"dummy_trailing_comma\"}\n    ]\n    },\n    \"profiles\": [\n  FRAMES\n\n  @result.threads.each_with_index do |thread, idx|\n    @output << <<~PROFILES\n      {\n        \"type\": \"evented\",\n        \"name\": \"Thread: \#{thread.id}, Fiber: \#{thread.fiber_id}\",\n        \"unit\": \"seconds\",\n        \"startValue\": 0,\n        \"endValue\": \#{JSON.dump(thread.call_tree.measurement.total_time)},\n        \"events\": [\n    PROFILES\n    print_call_tree(thread.call_tree, frames, 0.0, true)\n    @output << <<~PROFILES\n      ]\n      }\#{idx < @result.threads.length - 1 ? \",\" : \"\"}\n    PROFILES\n  end\n\n  @output << <<~ENDING\n    ]\n    }\n  ENDING\nend\n"