-
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathGcModeTests.cs
168 lines (148 loc) · 4.48 KB
/
GcModeTests.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
using System;
using System.Runtime;
using Xunit;
using Xunit.Abstractions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
#if NETFRAMEWORK
using BenchmarkDotNet.Environments;
#endif
namespace BenchmarkDotNet.IntegrationTests
{
public class GcModeTests : BenchmarkTestExecutor
{
public GcModeTests(ITestOutputHelper outputHelper) : base(outputHelper) { }
private IConfig CreateConfig(GcMode gc) => ManualConfig.CreateEmpty().AddJob(new Job(Job.Dry, gc));
[Fact]
public void HostProcessSettingsAreCopiedByDefault()
{
var config = CreateConfig(GcMode.Default);
if (GCSettings.IsServerGC)
CanExecute<ServerModeEnabled>(config);
else
CanExecute<WorkstationGcOnly>(config);
}
[Fact]
public void CanEnableServerGcMode()
{
var config = CreateConfig(new GcMode { Server = true });
CanExecute<ServerModeEnabled>(config);
}
[Fact]
public void CanDisableServerGcMode()
{
var config = CreateConfig(new GcMode { Server = false });
CanExecute<WorkstationGcOnly>(config);
}
[Fact]
public void CanEnableConcurrentGcMode()
{
var config = CreateConfig(new GcMode { Concurrent = true });
CanExecute<ConcurrentModeEnabled>(config);
}
[Fact]
public void CanDisableConcurrentGcMode()
{
var config = CreateConfig(new GcMode { Concurrent = false });
CanExecute<ConcurrentModeDisabled>(config);
}
[Fact]
public void CanAvoidForcingGarbageCollections()
{
var config = CreateConfig(new GcMode { Force = false });
CanExecute<AvoidForcingGarbageCollection>(config);
}
#if NETFRAMEWORK // not supported by project.json so far
[Fact]
public void CanAllowToCreateVeryLargeObjectsFor64Bit()
{
var config = ManualConfig.CreateEmpty().AddJob(
new Job(Job.Dry)
{
Environment =
{
Platform = Platform.X64,
Gc =
{
AllowVeryLargeObjects = true
}
}
});
CanExecute<CreateVeryLargeObjects>(config);
}
#endif
}
public class ServerModeEnabled
{
[Benchmark]
public void Benchmark()
{
if (GCSettings.IsServerGC == false)
{
throw new InvalidOperationException("Did not enable GC Server mode");
}
}
}
public class WorkstationGcOnly
{
[Benchmark]
public void Benchmark()
{
if (GCSettings.IsServerGC)
{
throw new InvalidOperationException("Did not disable GC Server mode");
}
}
}
public class ConcurrentModeEnabled
{
[Benchmark]
public void Benchmark()
{
if (GCSettings.LatencyMode == GCLatencyMode.Batch)
{
throw new InvalidOperationException("Did not enable Concurrent GC mode");
}
}
}
public class ConcurrentModeDisabled
{
[Benchmark]
public void Benchmark()
{
if (GCSettings.LatencyMode != GCLatencyMode.Batch)
{
throw new InvalidOperationException("Did not disable Concurrent GC mode");
}
}
}
public class AvoidForcingGarbageCollection
{
private int initialCollectionCountGen1;
private int initialCollectionCountGen2;
[GlobalSetup]
public void GlobalSetup()
{
initialCollectionCountGen1 = GC.CollectionCount(1);
initialCollectionCountGen2 = GC.CollectionCount(2);
}
[Benchmark]
public void Benchmark()
{
if (initialCollectionCountGen1 != GC.CollectionCount(1)
|| initialCollectionCountGen2 != GC.CollectionCount(2))
{
throw new InvalidOperationException("Did not disable GC Force");
}
}
}
public class CreateVeryLargeObjects
{
[Benchmark]
public long[] Benchmark()
{
return new long[2147483648 / sizeof(long)]; // 2GB is the default limit
}
}
}