-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathContentionProfilerTest.cs
150 lines (119 loc) · 6.81 KB
/
ContentionProfilerTest.cs
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
// <copyright file="ContentionProfilerTest.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
// </copyright>
using System;
using System.Linq;
using Datadog.Profiler.IntegrationTests.Helpers;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace Datadog.Profiler.IntegrationTests.Contention
{
public class ContentionProfilerTest
{
private const string ScenarioContention = "--scenario 10 --threads 20";
private const string ScenarioWithoutContention = "--scenario 6 --threads 1";
private readonly ITestOutputHelper _output;
public ContentionProfilerTest(ITestOutputHelper output)
{
_output = output;
}
[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0", "net8.0" })]
public void ShouldGetContentionSamples(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioContention);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only contention profiler enabled so should only see the 2 related values per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 2);
Assert.True(SamplesHelper.IsLabelPresent(runner.Environment.PprofDir, "raw duration"));
if (framework == "net8.0")
{
AssertBlockingThreadLabel(runner.Environment.PprofDir);
}
}
[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0", "net8.0" })]
public void ShouldContentionProfilerBeEnabledByDefault(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioContention);
// disable default profilers except contention
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.GarbageCollectionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only contention profiler enabled so should see 2 value per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 2);
Assert.NotEqual(0, SamplesHelper.GetSamplesCount(runner.Environment.PprofDir));
if (framework == "net8.0")
{
AssertBlockingThreadLabel(runner.Environment.PprofDir);
}
}
[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0", "net8.0" })]
public void ExplicitlyDisableContentionProfiler(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioContention);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only walltime profiler enabled so should see 1 value per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 1);
}
[TestAppFact("Samples.Computer01", new[] { "net462" })]
public void ShouldGetLockContentionSamplesViaEtw(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioWithoutContention);
// allow agent proxy to send the recorded events
runner.TestDurationInSeconds = 30;
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "1");
Guid guid = Guid.NewGuid();
runner.Environment.SetVariable(EnvironmentVariables.EtwReplayEndpoint, "\\\\.\\pipe\\DD_ETW_TEST_AGENT-" + guid);
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
if (IntPtr.Size == 4)
{
// 32-bit
agent.StartEtwProxy(runner.XUnitLogger, "DD_ETW_TEST_AGENT-" + guid, "Contention\\lockContention-32.bevents");
}
else
{
// 64-bit
agent.StartEtwProxy(runner.XUnitLogger, "DD_ETW_TEST_AGENT-" + guid, "Contention\\lockContention-64.bevents");
}
int eventsCount = 0;
agent.EventsSent += (sender, e) => eventsCount = e.Value;
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);
AssertContainLockContentionSamples(runner.Environment.PprofDir);
Assert.True(eventsCount > 0);
}
private static void AssertContainLockContentionSamples(string pprofDir)
{
var contentionSamples = SamplesHelper.GetSamples(pprofDir, "lock-count");
contentionSamples.Should().NotBeEmpty();
}
private static void AssertBlockingThreadLabel(string pprofDir)
{
var threadIds = SamplesHelper.GetThreadIds(pprofDir);
// get samples with lock-count value set and blocking thread info
var contentionSamples = SamplesHelper.GetSamples(pprofDir, "lock-count")
.Where(e => e.Labels.Any(x => x.Name == "blocking thread id"));
contentionSamples.Should().NotBeEmpty();
foreach (var (_, labels, _) in contentionSamples)
{
var blockingThreadIdLabel = labels.FirstOrDefault(l => l.Name == "blocking thread id");
blockingThreadIdLabel.Name.Should().NotBeNullOrWhiteSpace();
threadIds.Should().Contain(int.Parse(blockingThreadIdLabel.Value), $"Unknown blocking thread id {blockingThreadIdLabel.Value}");
var blockingThreadNameLabel = labels.FirstOrDefault(l => l.Name == "blocking thread name");
blockingThreadIdLabel.Name.Should().NotBeNullOrWhiteSpace();
blockingThreadIdLabel.Value.Should().NotBeNullOrWhiteSpace();
}
}
}
}