Skip to content

Commit 0b9f0d0

Browse files
committed
test & benchmark clean-up
Added tests to verify that big strings are not cached
1 parent 41af3df commit 0b9f0d0

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

dd-trace-core/src/jmh/java/datadog/trace/common/writer/ddagent/Utf8Benchmark.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import org.openjdk.jmh.annotations.Mode;
88
import org.openjdk.jmh.infra.Blackhole;
99

10-
import datadog.trace.common.writer.ddagent.GenerationalUtf8Cache;
11-
import datadog.trace.common.writer.ddagent.SimpleUtf8CacheTest;
12-
1310
/**
1411
* This benchmark isn't really intended to used to measure throughput, but rather to be used with
1512
* "-prof gc" to check bytes / op.

dd-trace-core/src/test/java/datadog/trace/common/writer/ddagent/GenerationalUtf8CacheTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package datadog.trace.common.writer.ddagent;
22

33
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotEquals;
46
import static org.junit.Assert.assertNotSame;
57
import static org.junit.Assert.assertSame;
6-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
78

89
import java.nio.charset.StandardCharsets;
910
import java.util.Random;
@@ -110,6 +111,33 @@ public void fuzz() {
110111
assertNotEquals(0, promotedHits);
111112
}
112113

114+
@Test
115+
public void bigString_dont_cache() {
116+
String lorem = "Lorem ipsum dolor sit amet";
117+
while (lorem.length() < 500) {
118+
lorem += lorem;
119+
}
120+
byte[] expected = lorem.getBytes(StandardCharsets.UTF_8);
121+
122+
GenerationalUtf8Cache cache = new GenerationalUtf8Cache();
123+
byte[] first = cache.getUtf8(lorem);
124+
assertArrayEquals(expected, first);
125+
126+
byte[] second = cache.getUtf8(lorem);
127+
assertArrayEquals(expected, second);
128+
assertNotSame(first, second);
129+
130+
for (int i = 0; i < 10; ++i) {
131+
byte[] result = cache.getUtf8(lorem);
132+
assertArrayEquals(expected, result);
133+
134+
assertNotSame(first, result);
135+
assertNotSame(second, result);
136+
}
137+
assertEquals(0, cache.edenHits);
138+
assertEquals(0, cache.tenuredHits);
139+
}
140+
113141
static final String[] TAGS = {"foo", "bar", "baz"};
114142

115143
static final String[] BASE_STRINGS = {"Hello", "world", "foo", "bar", "baz", "quux"};

dd-trace-core/src/test/java/datadog/trace/common/writer/ddagent/SimpleUtf8CacheTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package datadog.trace.common.writer.ddagent;
22

33
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotEquals;
46
import static org.junit.Assert.assertNotSame;
57
import static org.junit.Assert.assertSame;
6-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
78

89
import java.nio.charset.StandardCharsets;
910
import java.util.Random;
@@ -75,6 +76,32 @@ public void fuzz() {
7576
assertNotEquals(0, hits);
7677
}
7778

79+
@Test
80+
public void bigString_dont_cache() {
81+
String lorem = "Lorem ipsum dolor sit amet";
82+
while (lorem.length() < 100) {
83+
lorem += lorem;
84+
}
85+
byte[] expected = lorem.getBytes(StandardCharsets.UTF_8);
86+
87+
SimpleUtf8Cache cache = new SimpleUtf8Cache();
88+
byte[] first = cache.getUtf8(lorem);
89+
assertArrayEquals(expected, first);
90+
91+
byte[] second = cache.getUtf8(lorem);
92+
assertArrayEquals(expected, second);
93+
assertNotSame(first, second);
94+
95+
for (int i = 0; i < 10; ++i) {
96+
byte[] result = cache.getUtf8(lorem);
97+
assertArrayEquals(expected, result);
98+
99+
assertNotSame(first, result);
100+
assertNotSame(second, result);
101+
}
102+
assertEquals(0, cache.hits);
103+
}
104+
78105
static final String[] TAGS = {"foo", "bar", "baz"};
79106

80107
static final String[] BASE_STRINGS = {"Hello", "world", "foo", "bar", "baz", "quux"};

0 commit comments

Comments
 (0)