Sample: IntroWakeLock
Running benchmarks may sometimes take enough time such that the system enters sleep or turns off the display.
Using a WakeLock prevents the Windows system doing so.
Source code
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using System;
using System.Threading;
// *** Attribute Style applied to Assembly ***
[assembly: WakeLock(WakeLockType.System)]
namespace BenchmarkDotNet.Samples;
// *** Attribute Style ***
[WakeLock(WakeLockType.Display)]
public class IntroWakeLock
{
[Benchmark]
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10));
}
// *** Object Style ***
[Config(typeof(Config))]
public class IntroWakeLockObjectStyle
{
private class Config : ManualConfig
{
public Config() => WakeLock = WakeLockType.System;
}
[Benchmark]
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10));
}
Command line
--wakeLock None
--wakeLock System
--wakeLock Display
Links
- WakeLockAttribute
- The permanent link to this sample: BenchmarkDotNet.Samples.IntroWakeLock