-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMetadataNormalisationTest.java
More file actions
72 lines (66 loc) · 3.16 KB
/
Copy pathMetadataNormalisationTest.java
File metadata and controls
72 lines (66 loc) · 3.16 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
package com.datadoghq.profiler.metadata;
import com.datadoghq.profiler.AbstractProfilerTest;
import org.junit.jupiter.api.Test;
import org.openjdk.jmc.common.item.IItem;
import org.openjdk.jmc.common.item.IItemCollection;
import org.openjdk.jmc.common.item.IItemIterable;
import org.openjdk.jmc.common.item.IMemberAccessor;
import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class MetadataNormalisationTest extends AbstractProfilerTest {
@Test
public void test() throws Exception {
Constructor<?> arrayListConstructor = ArrayList.class.getConstructor();
Method arrayListAdd = ArrayList.class.getDeclaredMethod("add", Object.class);
Constructor<?> linkedListConstructor = LinkedList.class.getConstructor();
Method linkedListAdd = LinkedList.class.getDeclaredMethod("add", Object.class);
// need to invoke enough times to result in generation of accessors and to record some cpu time.
// Under ASAN, cpu=100us sampling combined with this fixed iteration count produces a much
// larger sample set (stack-trace signal handling is slower under ASAN), and every sample's
// stack trace is materialized below, so reduce the iteration count to bound the sample count.
int iterations = isAsan() ? 10_000 : 100_000;
int count = 0;
for (int i = 0; i < iterations; i++) {
Object list = arrayListConstructor.newInstance();
arrayListAdd.invoke(list, "element");
count += ((List<?>) list).size();
}
for (int i = 0; i < iterations; i++) {
Object list = linkedListConstructor.newInstance();
linkedListAdd.invoke(list, "element");
count += ((List<?>) list).size();
}
System.out.println(count);
stopProfiler();
IItemCollection executionSamples = verifyEvents("datadog.ExecutionSample");
Matcher[] forbiddenPatternMatchers = Stream.of(
"MH.*0x[A-Fa-f0-9]{3}", // method handles
"GeneratedConstructorAccessor\\d+",
"GeneratedMethodAccessor\\d+"
)
.map(regex -> Pattern.compile(regex).matcher(""))
.toArray(Matcher[]::new);
for (IItemIterable samples : executionSamples) {
IMemberAccessor<String, IItem> stacktraceAccessor = JdkAttributes.STACK_TRACE_STRING.getAccessor(samples.getType());
for (IItem item : samples) {
String stacktrace = stacktraceAccessor.getMember(item);
for (Matcher matcher : forbiddenPatternMatchers) {
matcher.reset(stacktrace);
assertFalse(matcher.find(), () -> matcher.pattern() + "\n" + stacktrace);
}
}
}
}
@Override
protected String getProfilerCommand() {
return isAsan() ? "cpu=1ms" : "cpu=100us";
}
}