-
Notifications
You must be signed in to change notification settings - Fork 915
/
profiler_spec_helpers.rb
190 lines (159 loc) · 4.19 KB
/
profiler_spec_helpers.rb
1
2
3
4
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
63
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'jruby/profiler'
require 'json'
module JRuby::Profiler::SpecHelpers
def profile_data
@profile_data ||= JRuby::Profiler.profile_data
end
def top_invocation
@top_invocation ||= profile_data.compute_results
end
def method_name(inv)
profile_data.method_name(inv.method_serial_number)
end
def get_inv(name, inv=top_invocation)
inv.children.values.detect {|inv| method_name(inv) == name}
end
def tree(inv, indent = 0)
puts " "*indent + method_name(inv) + "(#{inv.method_serial_number}@#{inv.recursive_depth}) x#{inv.count}"
inv.children.values.to_a.each do |c|
tree(c, indent + 2)
end
end
def graph_output
data_output JRuby::Profiler::GraphProfilePrinter
end
def json_output
raw_output = data_output(JRuby::Profiler::JsonProfilePrinter)
JSON.parse(raw_output)
end
def flat_output
data_output JRuby::Profiler::FlatProfilePrinter
end
def data_output(printer)
output_stream = java.io.ByteArrayOutputStream.new
print_stream = java.io.PrintStream.new(output_stream)
printer.new(profile_data).printProfile(print_stream)
output_stream.toString
end
def line_for(text, method)
lines = lines_for(text, method)
if lines.length == 0
return nil
elsif lines.length > 1
raise "multiple lines matching #{method}"
end
lines.first
end
def main_line_for(text, method)
lines = lines_for(text, method).select {|l| l[:main_row] == true}
if lines.length == 0
return nil
elsif lines.length > 1
raise "multiple main lines matching #{method}"
end
lines.first
end
def lines_for(text, method)
lines = text.split("\n").select {|l| l.include?(method)}
lines.map do |line|
bits = line.split(" ")
if bits.length == 7
{:total_pc => bits[0], :self_pc => bits[1], :total => bits[2].to_f,
:self => bits[3].to_f, :children => bits[4].to_f, :calls => bits[5].to_i,
:name => bits[6].strip, :main_row => true
}
elsif bits.length == 5
{:total => bits[0].to_f,
:self => bits[1].to_f, :children => bits[2].to_f, :calls => bits[3].to_i,
:name => bits[4].strip, :main_row => false
}
end
end
end
def decode_main_row(line)
bits = line.split(" ")
{:total_pc => bits[0], :self_pc => bits[1], :total => bits[2].to_f,
:self => bits[3].to_f, :children => bits[4].to_f, :calls => bits[5].to_i,
:name => bits[6].strip, :parents => nil, :children => []
}
end
def decode_parent_or_child(line)
bits = line.split(" ")
{:total => bits[0].to_f,
:self => bits[1].to_f, :children => bits[2].to_f, :calls => bits[3].split("/").map{|b| b.to_i},
:name => bits[4].strip
}
end
def decode_graph(text)
methods = []
current_method = nil
parents = []
text.split("\n").each do |line|
if line =~ /^\s*-+\s*$/
current_method = nil
parents = []
elsif line =~ /^\s+\d+%/ # main row
current_method = decode_main_row(line)
current_method[:parents] = parents
methods << current_method
parents = nil
elsif line =~ /^\s+\d+\.\d+\s/ # parent or child
if current_method # child
current_method[:children] << decode_parent_or_child(line)
else # parent
parents << decode_parent_or_child(line)
end
end
end
methods
end
end
class ProfilerTest
def wait(t)
sleep t
end
def test_instance_method
end
def self.test_static_method
end
class << self
def test_metaclass_method
end
end
def level1
level2
end
def level2
level3
level3
end
def level3
end
def recurse(x)
if x > 0
recurse(x-1)
end
end
def recurse_wait(x, wait)
if x > 0
s = Time.now
true until Time.now - s > wait
recurse_wait(x-1, wait)
end
end
def recurse_and_start_profiling(x)
if x > 0
recurse_and_start_profiling(x - 1)
else
JRuby::Profiler.start
end
end
def start
JRuby::Profiler.start
test_instance_method
end
def stop
ProfilerTest.test_static_method
JRuby::Profiler.stop
end
end