-
-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathMemoryDiagnoserTests.cs
executable file
·365 lines (307 loc) · 14.9 KB
/
MemoryDiagnoserTests.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.IntegrationTests.Xunit;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.NativeAot;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using Xunit;
using Xunit.Abstractions;
using BenchmarkDotNet.Toolchains.Mono;
namespace BenchmarkDotNet.IntegrationTests
{
public class MemoryDiagnoserTests
{
private readonly ITestOutputHelper output;
public MemoryDiagnoserTests(ITestOutputHelper outputHelper) => output = outputHelper;
public static IEnumerable<object[]> GetToolchains()
{
yield return new object[] { Job.Default.GetToolchain() };
yield return new object[] { InProcessEmitToolchain.Instance };
}
public class AccurateAllocations
{
[Benchmark] public byte[] EightBytesArray() => new byte[8];
[Benchmark] public byte[] SixtyFourBytesArray() => new byte[64];
[Benchmark] public Task<int> AllocateTask() => Task.FromResult<int>(-12345);
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurate(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
if (toolchain is MonoToolchain)
{
objectAllocationOverhead += IntPtr.Size;
}
AssertAllocations(toolchain, typeof(AccurateAllocations), new Dictionary<string, long>
{
{ nameof(AccurateAllocations.EightBytesArray), 8 + objectAllocationOverhead + arraySizeOverhead },
{ nameof(AccurateAllocations.SixtyFourBytesArray), 64 + objectAllocationOverhead + arraySizeOverhead },
{ nameof(AccurateAllocations.AllocateTask), CalculateRequiredSpace<Task<int>>() },
});
}
[FactEnvSpecific("We don't want to test NativeAOT twice (for .NET Framework 4.6.2 and .NET 7.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsNativeAOT()
{
if (OsDetector.IsMacOS())
return; // currently not supported
MemoryDiagnoserIsAccurate(NativeAotToolchain.Net80);
}
[FactEnvSpecific("We don't want to test MonoVM twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsModernMono()
{
MemoryDiagnoserIsAccurate(MonoToolchain.Mono80);
}
public class AllocatingGlobalSetupAndCleanup
{
private List<int> list;
[Benchmark] public void AllocateNothing() { }
[IterationSetup]
[GlobalSetup]
public void AllocatingSetUp() => AllocateUntilGcWakesUp();
[IterationCleanup]
[GlobalCleanup]
public void AllocatingCleanUp() => AllocateUntilGcWakesUp();
private void AllocateUntilGcWakesUp()
{
int initialCollectionCount = GC.CollectionCount(0);
while (initialCollectionCount == GC.CollectionCount(0))
list = Enumerable.Range(0, 100).ToList();
}
}
[Theory(Skip = "#1542 Tiered JIT Thread allocates memory in the background"), MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserDoesNotIncludeAllocationsFromSetupAndCleanup(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(AllocatingGlobalSetupAndCleanup), new Dictionary<string, long>
{
{ nameof(AllocatingGlobalSetupAndCleanup.AllocateNothing), 0 }
});
}
public class NoAllocationsAtAll
{
[Benchmark] public void EmptyMethod() { }
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void EngineShouldNotInterfereAllocationResults(IToolchain toolchain)
{
if (RuntimeInformation.IsFullFramework && toolchain.IsInProcess)
{
return; // this test is flaky on Full Framework
}
AssertAllocations(toolchain, typeof(NoAllocationsAtAll), new Dictionary<string, long>
{
{ nameof(NoAllocationsAtAll.EmptyMethod), 0 }
});
}
public class NoBoxing
{
[Benchmark] public ValueTuple<int> ReturnsValueType() => new ValueTuple<int>(0);
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void EngineShouldNotIntroduceBoxing(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(NoBoxing), new Dictionary<string, long>
{
{ nameof(NoBoxing.ReturnsValueType), 0 }
});
}
public class NonAllocatingAsynchronousBenchmarks
{
private readonly Task<int> completedTaskOfT = Task.FromResult(default(int)); // we store it in the field, because Task<T> is reference type so creating it allocates heap memory
[Benchmark] public Task CompletedTask() => Task.CompletedTask;
[Benchmark] public Task<int> CompletedTaskOfT() => completedTaskOfT;
[Benchmark] public ValueTask<int> CompletedValueTaskOfT() => new ValueTask<int>(default(int));
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AwaitingTasksShouldNotInterfereAllocationResults(IToolchain toolchain)
{
if (toolchain.IsInProcess)
{
return; // it's flaky: https://github.com/dotnet/BenchmarkDotNet/issues/1925
}
AssertAllocations(toolchain, typeof(NonAllocatingAsynchronousBenchmarks), new Dictionary<string, long>
{
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedTask), 0 },
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedTaskOfT), 0 },
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedValueTaskOfT), 0 }
});
}
public class WithOperationsPerInvokeBenchmarks
{
[Benchmark(OperationsPerInvoke = 4)]
public void WithOperationsPerInvoke()
{
DoNotInline(new object(), new object());
DoNotInline(new object(), new object());
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void DoNotInline(object left, object right) { }
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocatedMemoryShouldBeScaledForOperationsPerInvoke(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
AssertAllocations(toolchain, typeof(WithOperationsPerInvokeBenchmarks), new Dictionary<string, long>
{
{ nameof(WithOperationsPerInvokeBenchmarks.WithOperationsPerInvoke), objectAllocationOverhead + IntPtr.Size }
});
}
public class TimeConsuming
{
[Benchmark]
public byte[] SixtyFourBytesArray()
{
// this benchmark should hit allocation quantum problem
// it allocates a little of memory, but it takes a lot of time to execute so we can't run in thousands of times!
Thread.Sleep(TimeSpan.FromSeconds(0.5));
return new byte[64];
}
}
[Theory(Skip = "#1542 Tiered JIT Thread allocates memory in the background"), MemberData(nameof(GetToolchains))]
//[TheoryNetCoreOnly("Only .NET Core 2.0+ API is bug free for this case"), MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocationQuantumIsNotAnIssueForNetCore21Plus(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
AssertAllocations(toolchain, typeof(TimeConsuming), new Dictionary<string, long>
{
{ nameof(TimeConsuming.SixtyFourBytesArray), 64 + objectAllocationOverhead + arraySizeOverhead }
});
}
public class MultiThreadedAllocation
{
public const int Size = 1_000_000;
public const int ThreadsCount = 10;
private Thread[] threads;
[IterationSetup]
public void SetupIteration()
{
threads = Enumerable.Range(0, ThreadsCount)
.Select(_ => new Thread(() => GC.KeepAlive(new byte[Size])))
.ToArray();
}
[Benchmark]
public void Allocate()
{
foreach (var thread in threads)
{
thread.Start();
thread.Join();
}
}
}
[Theory(Skip = "Test is flaky even in latest .Net")]
[MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurateForMultiThreadedBenchmarks(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
long memoryAllocatedPerArray = (MultiThreadedAllocation.Size + objectAllocationOverhead + arraySizeOverhead);
long threadStartAndJoinOverhead = 112; // this is more or less a magic number taken from memory profiler
long allocatedMemoryPerThread = memoryAllocatedPerArray + threadStartAndJoinOverhead;
AssertAllocations(toolchain, typeof(MultiThreadedAllocation), new Dictionary<string, long>
{
{ nameof(MultiThreadedAllocation.Allocate), allocatedMemoryPerThread * MultiThreadedAllocation.ThreadsCount }
});
}
private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Dictionary<string, long> benchmarksAllocationsValidators)
{
var config = CreateConfig(toolchain);
var benchmarks = BenchmarkConverter.TypeToBenchmarks(benchmarkType, config);
var summary = BenchmarkRunner.Run(benchmarks);
try
{
summary.CheckPlatformLinkerIssues();
}
catch (MisconfiguredEnvironmentException e)
{
if (ContinuousIntegration.IsLocalRun())
{
output.WriteLine(e.SkipMessage);
return;
}
throw;
}
foreach (var benchmarkAllocationsValidator in benchmarksAllocationsValidators)
{
var allocatingBenchmarks = benchmarks.BenchmarksCases.Where(benchmark => benchmark.DisplayInfo.Contains(benchmarkAllocationsValidator.Key));
foreach (var benchmark in allocatingBenchmarks)
{
var benchmarkReport = summary.Reports.Single(report => report.BenchmarkCase == benchmark);
Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.GetBytesAllocatedPerOperation(benchmark));
if (benchmarkAllocationsValidator.Value == 0)
{
Assert.Equal(0, benchmarkReport.GcStats.GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects: true));
}
}
}
}
private IConfig CreateConfig(IToolchain toolchain)
=> ManualConfig.CreateEmpty()
.AddJob(Job.ShortRun
.WithEvaluateOverhead(false) // no need to run idle for this test
.WithWarmupCount(0) // don't run warmup to save some time for our CI runs
.WithIterationCount(1) // single iteration is enough for us
.WithGcForce(false)
.WithGcServer(false)
.WithGcConcurrent(false)
.WithEnvironmentVariables([
// Tiered JIT can allocate some memory on a background thread, let's disable it to make our tests less flaky (#1542)
new EnvironmentVariable("DOTNET_TieredCompilation", "0"),
new EnvironmentVariable("COMPlus_TieredCompilation", "0")
])
.WithToolchain(toolchain))
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddDiagnoser(MemoryDiagnoser.Default)
.AddLogger(toolchain.IsInProcess ? ConsoleLogger.Default : new OutputLogger(output)); // we can't use OutputLogger for the InProcess toolchains because it allocates memory on the same thread
// note: don't copy, never use in production systems (it should work but I am not 100% sure)
private int CalculateRequiredSpace<T>()
{
int total = SizeOfAllFields<T>();
if (!typeof(T).GetTypeInfo().IsValueType)
total += IntPtr.Size * 2; // pointer to method table + object header word
if (total % IntPtr.Size != 0) // aligning..
total += IntPtr.Size - (total % IntPtr.Size);
return total;
}
// note: don't copy, never use in production systems (it should work but I am not 100% sure)
private int SizeOfAllFields<T>()
{
int GetSize(Type type)
{
var sizeOf = typeof(Unsafe).GetTypeInfo().GetMethod(nameof(Unsafe.SizeOf));
return (int)sizeOf.MakeGenericMethod(type).Invoke(null, null);
}
return typeof(T)
.GetAllFields()
.Where(field => !field.IsStatic && !field.IsLiteral)
.Distinct()
.Sum(field => GetSize(field.FieldType));
}
}
}