-
Notifications
You must be signed in to change notification settings - Fork 88
/
test_reporter.rb
281 lines (237 loc) · 9.37 KB
/
test_reporter.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# frozen_string_literal: true
require_relative 'test_helper'
class TestReporter < Minitest::Test
def setup
@retained = []
end
# Reusable block for reporting.
def default_block
# Create an object from a gem outside memory_profiler which allocates
# its own objects internally
MiniTest::Reporter.new
# Create 10 strings
10.times { |i| i.to_s }
# Create 1 string and retain it
@retained << +"hello"
# Create one object defined by the test_helpers file
Foo.new
end
# Shared method that creates a Results with 1 retained object using options provided
def create_report(options = {}, &profiled_block)
profiled_block ||= -> { default_block }
MemoryProfiler::Reporter.report(options, &profiled_block)
end
def test_basic_object
results = create_report do
@retained << BasicObject.new
@retained << BasicObjectSubclass.new
end
assert_equal(2, results.total_allocated)
assert_equal(2, results.total_retained)
assert_equal('BasicObject', results.allocated_objects_by_class[0][:data])
assert_equal('BasicObjectSubclass', results.allocated_objects_by_class[1][:data])
assert_equal(2, results.retained_objects_by_location.length)
end
def test_anonymous_class_object
anon_class1 = Class.new
anon_class2 = Class.new(String)
results = create_report do
@retained << anon_class1.new
@retained << anon_class2.new
end
assert_equal(2, results.total_allocated)
assert_equal(2, results.total_retained)
assert_equal('<<Unknown>>', results.allocated_objects_by_class[0][:data])
assert_equal(2, results.retained_objects_by_location.length)
end
def test_nil_reporting_class
results = create_report do
@retained << NilReportingClass.new
end
assert_equal(1, results.total_allocated)
assert_equal(1, results.total_retained)
assert_equal('NilReportingClass', results.allocated_objects_by_class[0][:data])
assert_equal(1, results.retained_objects_by_location.length)
end
def test_string_reporting_class
results = create_report do
@retained << StringReportingClass.new
end
assert_equal(1, results.total_allocated)
assert_equal(1, results.total_retained)
assert_equal('StringReportingClass', results.allocated_objects_by_class[0][:data])
assert_equal(1, results.retained_objects_by_location.length)
end
def test_counts
results = create_report
assert_equal(16, results.total_allocated)
assert_equal(1, results.total_retained)
assert_equal(1, results.retained_objects_by_location.length)
end
def test_class_tracing_with_array
results = create_report(trace: [Foo])
assert_equal(1, results.total_allocated)
assert_equal(0, results.total_retained)
end
def test_class_tracing_with_value
results = create_report(trace: Foo)
assert_equal(1, results.total_allocated)
assert_equal(0, results.total_retained)
end
def test_ignore_file_with_regex
results = create_report(ignore_files: /test_reporter\.rb/)
assert_equal(3, results.total_allocated)
assert_equal(0, results.total_retained)
end
def test_ignore_file_with_string
results = create_report(ignore_files: 'test_reporter.rb|another_file.rb')
assert_equal(3, results.total_allocated)
assert_equal(0, results.total_retained)
end
def test_allow_files_with_string
results = create_report(allow_files: 'test_reporter')
assert_equal(13, results.total_allocated)
assert_equal(1, results.total_retained)
end
def test_allow_files_with_array
results = create_report(allow_files: ['test_reporter', 'another_file'])
assert_equal(13, results.total_allocated)
assert_equal(1, results.total_retained)
end
def test_no_color_output
results = create_report
io = StringIO.new
results.pretty_print io, color_output: false
assert(!io.string.include?("\033"), 'excludes color information')
end
def test_color_output
results = create_report
io = StringIO.new
results.pretty_print io, color_output: true
assert(io.string.include?("\033"), 'includes color information')
end
class StdoutMock < StringIO
def isatty
true
end
end
def test_color_output_defaults_to_true_when_run_from_tty
results = create_report
io = StdoutMock.new
results.pretty_print io
assert(io.string.include?("\033"), 'includes color information')
end
def test_mono_output_defaults_to_true_when_not_run_from_tty
results = create_report
io = StringIO.new
results.pretty_print io
assert(!io.string.include?("\033"), 'excludes color information')
end
def test_reports_can_be_reused_with_different_color_options
results = create_report
io = StringIO.new
results.pretty_print io, color_output: true
assert(io.string.include?("\033"), 'includes color information')
io = StringIO.new
results.pretty_print io, color_output: false
assert(!io.string.include?("\033"), 'excludes color information')
end
def test_non_string_named_class
retained = []
results = create_report do
retained << NonStringNamedClass.new
retained << +"test"
end
io = StringIO.new
results.pretty_print io
assert_equal(2, results.total_allocated)
assert_equal(2, results.total_retained)
assert_equal('String', results.allocated_objects_by_class[0][:data])
assert_equal('Symbol', results.allocated_objects_by_class[1][:data])
assert_equal(2, results.retained_objects_by_location.length)
end
def test_exception_handling
results = nil
assert_raises Exception do
results = create_report do
@retained << +"hello"
raise Exception, +"Raising exception"
end
end
assert_nil(results)
refute(GC.enable, "Re-enabling GC should return false because it is already enabled")
# Verify that memory_profiler is not reporting on itself following the exception
results = create_report(allow_files: 'lib/memory_profiler')
assert_equal(0, results.total_allocated)
end
def test_strings_report
# Note: There is something strange about `string *`. The first time it is dup'd it allocates 2 objects.
string_250 = String.new("1234567890" * 25)
string_300 = String.new("1234567890" * 30)
results = create_report do
string_250.dup
string_250.dup
5.times { string_250.dup }
string_300.dup
end
assert_equal(8, results.total_allocated, "8 strings should be allocated")
assert_equal(2, results.strings_allocated.size, "2 unique strings should be observed")
assert_equal(results.strings_allocated[0][0],
results.strings_allocated[1][0], "The 2 unique strings have the same summary string")
assert_equal(200, results.strings_allocated[0][0].size, "The first string summary should be shortened to 200 chars")
assert_equal(3, results.strings_allocated[0][1].size, "The first string was allocated in 3 locations")
assert_equal(5, results.strings_allocated[0][1][0][1], "The first string was allocated 5 times in the first location")
end
def test_no_strings_retained_report
# Strings longer than 23 characters share a reference to a "shared" frozen string which should also be GC'd
results = create_report do
5.times do |i|
short_text = "SHORT TEXT ##{i}"
short_text.dup
long_text = "LONG TEXT ##{i} 12345678901234567890123456789012345678901234567890"
long_text.dup
very_long_text = "VERY LONG TEXT ##{i} 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890"
very_long_text.dup
nil # Prevent the last frozen string from being the return value of the block
end
end
if RUBY_VERSION < '3'
# 3 times "0", 2 times for interpolated strings
total_allocated = 5 * (3 + 2 + 2 + 2)
unique = 20
elsif RUBY_VERSION < '3.1'
# 3 times "0", 2 times for short interpolated strings, 3 times for long interpolated strings
total_allocated = 5 * (3 + 2 + 3 + 3)
unique = 20
else
# 2 times for short interpolated strings, 3 times for long interpolated strings
total_allocated = 5 * (2 + 3 + 3)
unique = 15
end
assert_equal(total_allocated, results.total_allocated, "#{total_allocated} strings should be allocated")
assert_equal(unique, results.strings_allocated.size, "#{unique} unique strings should be observed")
assert_equal(0, results.strings_retained.size, "0 unique strings should be retained")
end
def test_symbols_report
string = "this is a string"
results = create_report do
string.to_sym
end
strings_allocated = RUBY_VERSION < '3' ? 2 : 1
assert_equal(strings_allocated + 1, results.total_allocated)
assert_includes(0..1, results.total_retained)
assert_equal(1, results.strings_allocated.size)
assert_equal('String', results.allocated_objects_by_class[0][:data])
assert_equal(strings_allocated, results.allocated_objects_by_class[0][:count])
assert_equal('Symbol', results.allocated_objects_by_class[1][:data])
assert_equal(1, results.allocated_objects_by_class[1][:count])
end
def test_yield_block
results = MemoryProfiler.report do
# Do not allocate anything
end
assert_equal(0, results.total_allocated)
assert_equal(0, results.total_retained)
assert_equal(0, results.retained_objects_by_location.length)
end
end