-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_stackprof.rb
More file actions
218 lines (188 loc) · 5.47 KB
/
test_stackprof.rb
File metadata and controls
218 lines (188 loc) · 5.47 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
$:.unshift File.expand_path('../../lib', __FILE__)
require 'stackprof'
require 'minitest/autorun'
require 'tempfile'
require 'pathname'
class StackProfTest < MiniTest::Test
def test_info
profile = StackProf.run{}
assert_equal 1.2, profile[:version]
assert_equal :wall, profile[:mode]
assert_equal 1000, profile[:interval]
assert_equal 0, profile[:samples]
end
def test_running
assert_equal false, StackProf.running?
StackProf.run{ assert_equal true, StackProf.running? }
end
def test_start_stop_results
assert_nil StackProf.results
assert_equal true, StackProf.start
assert_equal false, StackProf.start
assert_equal true, StackProf.running?
assert_nil StackProf.results
assert_equal true, StackProf.stop
assert_equal false, StackProf.stop
assert_equal false, StackProf.running?
assert_kind_of Hash, StackProf.results
assert_nil StackProf.results
end
def test_object_allocation
profile_base_line = __LINE__+1
profile = StackProf.run(mode: :object) do
Object.new
Object.new
end
assert_equal :object, profile[:mode]
assert_equal 1, profile[:interval]
assert_equal 2, profile[:samples]
frame = profile[:frames].values.first
assert_includes frame[:name], "StackProfTest#test_object_allocation"
assert_equal 2, frame[:samples]
assert_includes [profile_base_line - 2, profile_base_line], frame[:line]
assert_equal [1, 1], frame[:lines][profile_base_line+1]
assert_equal [1, 1], frame[:lines][profile_base_line+2]
frame = profile[:frames].values[1] if RUBY_VERSION < '2.3'
assert_equal [2, 0], frame[:lines][profile_base_line]
end
def test_object_allocation_interval
profile = StackProf.run(mode: :object, interval: 10) do
100.times { Object.new }
end
assert_equal 10, profile[:samples]
end
def test_object_allocation_missed_samples
profile = StackProf.run(mode: :object, interval: 100) do
1000.times { Object.new }
end
assert_equal 10, profile[:samples]
assert_equal 0, profile[:missed_samples]
end
def test_cputime
profile = StackProf.run(mode: :cpu, interval: 500) do
math
end
assert_operator profile[:samples], :>=, 1
frame = profile[:frames].values.first
assert_includes frame[:name], "StackProfTest#math"
end
def test_walltime
profile = StackProf.run(mode: :wall) do
idle
end
frame = profile[:frames].values.first
assert_equal "StackProfTest#idle", frame[:name]
assert_in_delta 200, frame[:samples], 25
end
def test_custom
profile_base_line = __LINE__+1
profile = StackProf.run(mode: :custom) do
10.times do
StackProf.sample
end
end
assert_equal :custom, profile[:mode]
assert_equal 10, profile[:samples]
frame = profile[:frames].values.first
assert_includes frame[:name], "StackProfTest#test_custom"
assert_includes [profile_base_line-2, profile_base_line+1], frame[:line]
assert_equal [10, 10], frame[:lines][profile_base_line+2]
end
def test_raw
profile = StackProf.run(mode: :custom, raw: true) do
10.times do
StackProf.sample
end
end
raw = profile[:raw]
assert_equal 10, raw[-1]
assert_equal raw[0] + 2, raw.size
assert_includes profile[:frames][raw[-2]][:name], 'StackProfTest#test_raw'
assert_equal 10, profile[:raw_timestamp_deltas].size
end
def test_fork
StackProf.run do
pid = fork do
exit! StackProf.running?? 1 : 0
end
Process.wait(pid)
assert_equal 0, $?.exitstatus
assert_equal true, StackProf.running?
end
end
def foo(n = 10)
if n == 0
StackProf.sample
return
end
foo(n - 1)
end
def test_recursive_total_samples
profile = StackProf.run(mode: :cpu, raw: true) do
10.times do
foo
end
end
frame = profile[:frames].values.find do |frame|
frame[:name] == "StackProfTest#foo"
end
assert_equal 10, frame[:total_samples]
end
def test_gc
profile = StackProf.run(interval: 100, raw: true) do
5.times do
GC.start
end
end
raw = profile[:raw]
gc_frame = profile[:frames].values.find{ |f| f[:name] == "(garbage collection)" }
assert gc_frame
assert_equal gc_frame[:samples], profile[:gc_samples]
assert_operator profile[:gc_samples], :>, 0
assert_operator profile[:missed_samples], :<=, 10
end
def test_out
tmpfile = Tempfile.new('stackprof-out')
ret = StackProf.run(mode: :custom, out: tmpfile) do
StackProf.sample
end
assert_equal tmpfile, ret
tmpfile.rewind
profile = Marshal.load(tmpfile.read)
refute_empty profile[:frames]
end
def test_out_to_path_string
tmpfile = Tempfile.new('stackprof-out')
ret = StackProf.run(mode: :custom, out: tmpfile.path) do
StackProf.sample
end
refute_equal tmpfile, ret
assert_equal tmpfile.path, ret.path
tmpfile.rewind
profile = Marshal.load(tmpfile.read)
refute_empty profile[:frames]
end
def test_pathname_out
tmpfile = Tempfile.new('stackprof-out')
pathname = Pathname.new(tmpfile.path)
ret = StackProf.run(mode: :custom, out: pathname) do
StackProf.sample
end
assert_equal tmpfile.path, ret.path
tmpfile.rewind
profile = Marshal.load(tmpfile.read)
refute_empty profile[:frames]
end
def math
250_000.times do
2 ** 10
end
end
def idle
r, w = IO.pipe
IO.select([r], nil, nil, 0.2)
ensure
r.close
w.close
end
end