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
|