-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathCpuAndWallTimeTest.cs
163 lines (127 loc) · 8.17 KB
/
CpuAndWallTimeTest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
// <copyright file="CpuAndWallTimeTest.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 Datadog.Profiler.IntegrationTests.Helpers;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace Datadog.Profiler.IntegrationTests.CpuProfiler
{
public class CpuAndWallTimeTest
{
private const string CmdLine = "--timeout 10"; // default scenario is PI computation to run for 10 seconds
private readonly ITestOutputHelper _output;
public CpuAndWallTimeTest(ITestOutputHelper output)
{
_output = output;
}
[TestAppFact("Samples.Computer01")]
public void GetCpuSamplesIfCpuProfilerIsActivatedByDefault(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
// disable default profilers except CPU
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.GarbageCollectionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "0");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only cpu profiler enabled so should see 2 value per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 2);
}
[TestAppFact("Samples.Computer01")]
public void GetWalltimeSamplesIfWalltimeProfilerIsActivatedByDefault(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
// disable default profilers except wall time
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.GarbageCollectionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "0");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only wall time profiler enabled so should see 1 value per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 1);
}
[TestAppFact("Samples.Computer01")]
public void NoSampleIfCpuAndWalltimeProfilersAreDeactivated(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// no profiler enabled so should not see any sample
Assert.Equal(0, SamplesHelper.GetSamplesCount(runner.Environment.PprofDir));
}
[TestAppFact("Samples.Computer01")]
public void GetCpuSamplesIfCpuProfilerIsActivated(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 2);
}
[TestAppFact("Samples.Computer01")]
public void CheckCpuDurationInSamples(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// Ensure that we don't count too much CPU like when that nano/milli sec bug was introduced
var cpuDuration = SamplesHelper.GetValueSum(runner.Environment.PprofDir, 0);
// Test is supposed to run 10s so count additional seconds both for extended duration + more than 1 managed thread (tracing code for example)
// --> could be flacky otherwise
var totalDuration = runner.TotalTestDurationInMilliseconds * 1000000L;
Assert.True(cpuDuration <= totalDuration);
var cpuSamples = SamplesHelper.GetValueSum(runner.Environment.PprofDir, 1);
cpuSamples.Should().BeGreaterThan(0);
}
[TestAppFact("Samples.Computer01")]
public void CheckCpuDurationInSamplesForNewCpuProfiler(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerType, "TimerCreate");
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(_output);
runner.Run(agent);
// Ensure that we don't count too much CPU like when that nano/milli sec bug was introduced
var cpuDuration = SamplesHelper.GetValueSum(runner.Environment.PprofDir, 0);
// Test is supposed to run 10s so count additional seconds both for extended duration + more than 1 managed thread (tracing code for example)
// --> could be flacky otherwise
var totalDuration = runner.TotalTestDurationInMilliseconds * 1000000L;
Assert.True(cpuDuration <= totalDuration);
}
[TestAppFact("Samples.Computer01")]
public void CheckWalltimeDurationInSamples(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// the wall time duration depends on the number of managed threads
// --> could be up to the number of threads x total test duration
var walltimeDuration = SamplesHelper.GetValueSum(runner.Environment.PprofDir, 0);
var managedThreadsCount = SamplesHelper.GetThreadCount(runner.Environment.PprofDir);
var totalDuration = (runner.TotalTestDurationInMilliseconds * 1000000L) * managedThreadsCount;
Assert.True(walltimeDuration <= totalDuration);
}
[TestAppFact("Samples.Computer01")]
public void GetWalltimeSamplesIfWalltimeProfilerIsActivated(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: CmdLine);
EnvironmentHelper.DisableDefaultProfilers(runner);
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "1");
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
// only wall time profiler enabled so should see 1 value per sample
SamplesHelper.CheckSamplesValueCount(runner.Environment.PprofDir, 1);
}
}
}