Skip to content

Commit a6a4625

Browse files
committed
Set benchmark process priority to realtime.
1 parent 7f163c8 commit a6a4625

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

tests/BenchmarkDotNet.IntegrationTests.ManualRunning/ExpectedBenchmarkResultsTests.cs

+45-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using BenchmarkDotNet.Analysers;
56
using BenchmarkDotNet.Attributes;
@@ -198,7 +199,7 @@ public struct Struct128
198199
l13, l14, l15, l16;
199200
}
200201

201-
public class DifferentSizedStructs
202+
public class DifferentSizedStructs : RealTimeBenchmarks
202203
{
203204
[Benchmark] public Struct16 Struct16() => default;
204205
[Benchmark] public Struct32 Struct32() => default;
@@ -207,17 +208,46 @@ public class DifferentSizedStructs
207208
}
208209
}
209210

210-
public class EmptyVoid { [Benchmark] public void Benchmark() { } }
211-
public class EmptyByte { [Benchmark] public byte Benchmark() => default; }
212-
public class EmptySByte { [Benchmark] public sbyte Benchmark() => default; }
213-
public class EmptyShort { [Benchmark] public short Benchmark() => default; }
214-
public class EmptyUShort { [Benchmark] public ushort Benchmark() => default; }
215-
public class EmptyChar { [Benchmark] public char Benchmark() => default; }
216-
public class EmptyInt32 { [Benchmark] public int Benchmark() => default; }
217-
public class EmptyUInt32 { [Benchmark] public uint Benchmark() => default; }
218-
public class EmptyInt64 { [Benchmark] public long Benchmark() => default; }
219-
public class EmptyUInt64 { [Benchmark] public ulong Benchmark() => default; }
220-
public class EmptyIntPtr { [Benchmark] public IntPtr Benchmark() => default; }
221-
public class EmptyUIntPtr { [Benchmark] public UIntPtr Benchmark() => default; }
222-
public class EmptyVoidPointer { [Benchmark] public unsafe void* Benchmark() => default; }
223-
public class EmptyClass { [Benchmark] public object Class() => default; }
211+
public class RealTimeBenchmarks
212+
{
213+
private Process process;
214+
private ProcessPriorityClass oldPriority;
215+
216+
[GlobalSetup]
217+
public void Setup()
218+
{
219+
process = Process.GetCurrentProcess();
220+
try
221+
{
222+
oldPriority = process.PriorityClass;
223+
// Requires admin mode. Makes the OS never give up CPU time for this process, so we can get more accurate timings.
224+
process.PriorityClass = ProcessPriorityClass.RealTime;
225+
}
226+
catch (PlatformNotSupportedException) { }
227+
}
228+
229+
[GlobalCleanup]
230+
public void Cleanup()
231+
{
232+
try
233+
{
234+
process.PriorityClass = oldPriority;
235+
}
236+
catch (PlatformNotSupportedException) { }
237+
}
238+
}
239+
240+
public class EmptyVoid : RealTimeBenchmarks { [Benchmark] public void Benchmark() { } }
241+
public class EmptyByte : RealTimeBenchmarks { [Benchmark] public byte Benchmark() => default; }
242+
public class EmptySByte : RealTimeBenchmarks { [Benchmark] public sbyte Benchmark() => default; }
243+
public class EmptyShort : RealTimeBenchmarks { [Benchmark] public short Benchmark() => default; }
244+
public class EmptyUShort : RealTimeBenchmarks { [Benchmark] public ushort Benchmark() => default; }
245+
public class EmptyChar : RealTimeBenchmarks { [Benchmark] public char Benchmark() => default; }
246+
public class EmptyInt32 : RealTimeBenchmarks { [Benchmark] public int Benchmark() => default; }
247+
public class EmptyUInt32 : RealTimeBenchmarks { [Benchmark] public uint Benchmark() => default; }
248+
public class EmptyInt64 : RealTimeBenchmarks { [Benchmark] public long Benchmark() => default; }
249+
public class EmptyUInt64 : RealTimeBenchmarks { [Benchmark] public ulong Benchmark() => default; }
250+
public class EmptyIntPtr : RealTimeBenchmarks { [Benchmark] public IntPtr Benchmark() => default; }
251+
public class EmptyUIntPtr : RealTimeBenchmarks { [Benchmark] public UIntPtr Benchmark() => default; }
252+
public class EmptyVoidPointer : RealTimeBenchmarks { [Benchmark] public unsafe void* Benchmark() => default; }
253+
public class EmptyClass : RealTimeBenchmarks { [Benchmark] public object Class() => default; }

0 commit comments

Comments
 (0)