Skip to content

Commit 1956c24

Browse files
committed
Test allocation counts
1 parent 9607cdc commit 1956c24

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/test_allocations.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class TestAllocations < Minitest::Test
6+
def test_integer
7+
assert_encode_allocations 1, [1, 2, 3, 4, 5]
8+
end
9+
10+
def test_boolean
11+
assert_encode_allocations 1, [true, false, true, false, true, false]
12+
end
13+
14+
def test_float
15+
# FIXME: ideally would not allocate
16+
assert_encode_allocations 6, [1.0, 2.0, 3.0, 4.0, 5.0]
17+
end
18+
19+
def test_symbol
20+
assert_encode_allocations 1, %i[foo bar baz quux]
21+
end
22+
23+
def test_string
24+
assert_encode_allocations 1, %w[foo bar baz quux]
25+
end
26+
27+
def test_array
28+
assert_encode_allocations 1, [[], [], [], [], []]
29+
end
30+
31+
def test_hash
32+
assert_encode_allocations 1, {foo: 1, bar: 2, baz: 3, quux: 4}
33+
end
34+
35+
def assert_encode_allocations(expected, data)
36+
allocations = measure_allocations { RapidJSON.encode(data) }
37+
assert_equal expected, allocations
38+
end
39+
40+
def measure_allocations
41+
i = 0
42+
while i < 2
43+
before = allocations
44+
yield
45+
after = allocations
46+
i += 1
47+
end
48+
after - before
49+
end
50+
51+
def allocations
52+
GC.stat(:total_allocated_objects)
53+
end
54+
end

0 commit comments

Comments
 (0)