-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathContextCpuTest.java
More file actions
132 lines (115 loc) · 6.22 KB
/
Copy pathContextCpuTest.java
File metadata and controls
132 lines (115 loc) · 6.22 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
package com.datadoghq.profiler.cpu;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.datadoghq.profiler.CStackAwareAbstractProfilerTest;
import com.datadoghq.profiler.junit.CStack;
import com.datadoghq.profiler.junit.CStackInjector;
import com.datadoghq.profiler.junit.RetryTest;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.provider.ValueSource;
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.common.unit.IQuantity;
import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
import com.datadoghq.profiler.AbstractProfilerTest;
import static com.datadoghq.profiler.MoreAssertions.assertInRange;
import com.datadoghq.profiler.Platform;
public class ContextCpuTest extends CStackAwareAbstractProfilerTest {
private ProfiledCode profiledCode;
public ContextCpuTest(@CStack String cstack) {
super(cstack);
}
@Override
protected void before() {
profiledCode = new ProfiledCode(profiler);
}
@RetryTest(10)
@TestTemplate
@ValueSource(strings = {"vm", "vmx", "fp", "dwarf"})
public void test(@CStack String cstack) throws ExecutionException, InterruptedException {
Assumptions.assumeTrue(!Platform.isJ9());
for (int i = 0, id = 1; i < 100; i++, id += 3) {
profiledCode.method1(id);
}
stopProfiler();
verifyCStackSettings();
Set<Long> method1SpanIds = profiledCode.spanIdsForMethod("method1Impl");
Set<Long> method2SpanIds = profiledCode.spanIdsForMethod("method2Impl");
Set<Long> method3SpanIds = profiledCode.spanIdsForMethod("method3Impl");
IItemCollection events = verifyEvents("datadog.ExecutionSample");
// on mac the usage of itimer to drive the sampling provides very unreliable outputs
if (!Platform.isMac()) {
// we have 100 method1, method2, and method3 calls, but can't guarantee we sampled them all
long method1Weight = 0;
long method2Weight = 0;
long method3Weight = 0;
long totalWeight = 0;
for (IItemIterable cpuSamples : events) {
IMemberAccessor<String, IItem> frameAccessor = JdkAttributes.STACK_TRACE_STRING.getAccessor(cpuSamples.getType());
IMemberAccessor<IQuantity, IItem> spanIdAccessor = SPAN_ID.getAccessor(cpuSamples.getType());
IMemberAccessor<IQuantity, IItem> rootSpanIdAccessor = LOCAL_ROOT_SPAN_ID.getAccessor(cpuSamples.getType());
IMemberAccessor<String, IItem> stateAccessor = THREAD_STATE.getAccessor(cpuSamples.getType());
for (IItem sample : cpuSamples) {
String stackTrace = frameAccessor.getMember(sample);
long spanId = spanIdAccessor.getMember(sample).longValue();
long rootSpanId = rootSpanIdAccessor.getMember(sample).longValue();
String state = stateAccessor.getMember(sample);
assertDoesNotThrow(() -> Thread.State.valueOf(state));
assertEquals(Thread.State.RUNNABLE, Thread.State.valueOf(state));
if (stackTrace.contains("method3Impl")) {
// method3 is scheduled after method2, and method1 blocks on it, so spanId == rootSpanId + 2
if (spanId > 0) {
assertEquals(rootSpanId + 2, spanId, stackTrace);
assertTrue(method3SpanIds.contains(spanId), stackTrace);
method3Weight += 1;
}
} else if (stackTrace.contains("method2Impl")) {
// method2 is called next, so spanId == rootSpanId + 1
if (spanId > 0) {
assertEquals(rootSpanId + 1, spanId, stackTrace);
assertTrue(method2SpanIds.contains(spanId), stackTrace);
method2Weight += 1;
}
} else if (stackTrace.contains("method1Impl")
&& !stackTrace.contains("method2") && !stackTrace.contains("method3")) {
// need to check this after method2 because method1 calls method2
// it's the root so spanId == rootSpanId
assertEquals(rootSpanId, spanId, stackTrace);
assertTrue(spanId == 0 || method1SpanIds.contains(spanId), stackTrace);
method1Weight += 1;
}
totalWeight++;
}
}
assertInRange(method1Weight / (double) totalWeight, 0.1, 0.6);
assertInRange(method2Weight / (double) totalWeight, 0.1, 0.6);
assertInRange(method3Weight / (double) totalWeight, 0.05, 0.6);
}
// The recording captures counter values before the final cleanup (before processTraces
// runs and frees all traces). Verify the recording contains meaningful data.
assertInRange(getRecordedCounterValue("calltrace_storage_traces"), 1, 100);
assertInRange(getRecordedCounterValue("calltrace_storage_bytes"), 1024, 8 * 1024 * 1024);
// live counters are 0 after stop (all traces freed - correct, non-leaking behaviour)
Map<String, Long> debugCounters = profiler.getDebugCounters();
assertEquals(0, debugCounters.get("calltrace_storage_traces"));
assertEquals(0, debugCounters.get("calltrace_storage_bytes"));
assertEquals(0, debugCounters.get("linear_allocator_bytes"));
assertEquals(0, debugCounters.get("linear_allocator_chunks"));
}
@Override
protected void after() throws Exception {
profiledCode.close();
}
@Override
protected String getProfilerCommand() {
return "cpu=10ms";
}
}