Skip to content

Commit de8fb91

Browse files
leonvandermeertimcasselladamsitnik
authored
Implement WakeLock so that the system does not enter sleep (#2670)
* Implement WakeLock so that the system does not enter sleep while benchmarks are running * Apply rework from code review Co-authored-by: Tim Cassell <[email protected]> * Apply suggestions from code review Co-authored-by: Adam Sitnik <[email protected]> * Apply rework from code review --------- Co-authored-by: Tim Cassell <[email protected]> Co-authored-by: Adam Sitnik <[email protected]>
1 parent 804482d commit de8fb91

25 files changed

+738
-1
lines changed

docs/articles/guides/console-args.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ dotnet run -c Release -- --filter * --runtimes net6.0 net8.0 --statisticalTest 5
243243
* `--platform` the Platform that should be used. If not specified, the host process platform is used (default). AnyCpu/X86/X64/Arm/Arm64/LoongArch64.
244244
* `--runOncePerIteration` run the benchmark exactly once per iteration.
245245
* `--buildTimeout` build timeout in seconds.
246+
* `--wakeLock` prevents the system from entering sleep or turning off the display. None/System/Display.
246247
* `--wasmEngine` full path to a java script engine used to run the benchmarks, used by Wasm toolchain.
247248
* `--wasmMainJS` path to the test-main.js file used by Wasm toolchain. Mandatory when using \"--runtimes wasm\"
248249
* `--expose_wasm` arguments for the JavaScript engine used by Wasm toolchain.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
uid: BenchmarkDotNet.Samples.IntroWakeLock
3+
---
4+
5+
## Sample: IntroWakeLock
6+
7+
Running benchmarks may sometimes take enough time such that the system enters sleep or turns off the display.
8+
9+
Using a WakeLock prevents the Windows system doing so.
10+
11+
### Source code
12+
13+
[!code-csharp[IntroWakeLock.cs](../../../samples/BenchmarkDotNet.Samples/IntroWakeLock.cs)]
14+
15+
### Command line
16+
17+
```
18+
--wakeLock None
19+
```
20+
```
21+
--wakeLock System
22+
```
23+
```
24+
--wakeLock Display
25+
```
26+
27+
### Links
28+
29+
* @BenchmarkDotNet.Attributes.WakeLockAttribute
30+
* The permanent link to this sample: @BenchmarkDotNet.Samples.IntroWakeLock
31+
32+
---

docs/articles/samples/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
href: IntroTailcall.md
127127
- name: IntroVisualStudioProfiler
128128
href: IntroVisualStudioProfiler.md
129+
- name: IntroWakeLock
130+
href: IntroWakeLock.md
129131
- name: IntroWasm
130132
href: IntroWasm.md
131133
- name: IntroUnicode
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
3+
using System;
4+
using System.Threading;
5+
6+
// *** Attribute Style applied to Assembly ***
7+
[assembly: WakeLock(WakeLockType.System)]
8+
9+
namespace BenchmarkDotNet.Samples;
10+
11+
// *** Attribute Style ***
12+
[WakeLock(WakeLockType.Display)]
13+
public class IntroWakeLock
14+
{
15+
[Benchmark]
16+
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10));
17+
}
18+
19+
// *** Object Style ***
20+
[Config(typeof(Config))]
21+
public class IntroWakeLockObjectStyle
22+
{
23+
private class Config : ManualConfig
24+
{
25+
public Config() => WakeLock = WakeLockType.System;
26+
}
27+
28+
[Benchmark]
29+
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10));
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using BenchmarkDotNet.Configs;
2+
using System;
3+
4+
namespace BenchmarkDotNet.Attributes
5+
{
6+
/// <summary>
7+
/// Placing a <see cref="WakeLockAttribute"/> on your assembly or class controls whether the
8+
/// Windows system enters sleep or turns off the display while benchmarks run.
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class)]
11+
public sealed class WakeLockAttribute : Attribute, IConfigSource
12+
{
13+
public WakeLockAttribute(WakeLockType wakeLockType) =>
14+
Config = ManualConfig.CreateEmpty().WithWakeLock(wakeLockType);
15+
16+
public IConfig Config { get; }
17+
}
18+
}

src/BenchmarkDotNet/Configs/DebugConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public abstract class DebugConfig : IConfig
7171
public SummaryStyle SummaryStyle => SummaryStyle.Default;
7272
public ConfigUnionRule UnionRule => ConfigUnionRule.Union;
7373
public TimeSpan BuildTimeout => DefaultConfig.Instance.BuildTimeout;
74+
public WakeLockType WakeLock => WakeLockType.None;
7475

7576
public string ArtifactsPath => null; // DefaultConfig.ArtifactsPath will be used if the user does not specify it in explicit way
7677

src/BenchmarkDotNet/Configs/DefaultConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public IEnumerable<IValidator> GetValidators()
8787

8888
public TimeSpan BuildTimeout => TimeSpan.FromSeconds(120);
8989

90+
public WakeLockType WakeLock => WakeLockType.System;
91+
9092
public string ArtifactsPath
9193
{
9294
get

src/BenchmarkDotNet/Configs/IConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public interface IConfig
5555
/// </summary>
5656
TimeSpan BuildTimeout { get; }
5757

58+
public WakeLockType WakeLock { get; }
59+
5860
/// <summary>
5961
/// Collect any errors or warnings when composing the configuration
6062
/// </summary>

src/BenchmarkDotNet/Configs/ImmutableConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ internal ImmutableConfig(
5656
SummaryStyle summaryStyle,
5757
ConfigOptions options,
5858
TimeSpan buildTimeout,
59+
WakeLockType wakeLock,
5960
IReadOnlyList<Conclusion> configAnalysisConclusion)
6061
{
6162
columnProviders = uniqueColumnProviders;
@@ -78,6 +79,7 @@ internal ImmutableConfig(
7879
SummaryStyle = summaryStyle;
7980
Options = options;
8081
BuildTimeout = buildTimeout;
82+
WakeLock = wakeLock;
8183
ConfigAnalysisConclusion = configAnalysisConclusion;
8284
}
8385

@@ -89,6 +91,7 @@ internal ImmutableConfig(
8991
public ICategoryDiscoverer CategoryDiscoverer { get; }
9092
public SummaryStyle SummaryStyle { get; }
9193
public TimeSpan BuildTimeout { get; }
94+
public WakeLockType WakeLock { get; }
9295

9396
public IEnumerable<IColumnProvider> GetColumnProviders() => columnProviders;
9497
public IEnumerable<IExporter> GetExporters() => exporters;

src/BenchmarkDotNet/Configs/ImmutableConfigBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static ImmutableConfig Create(IConfig source)
7676
source.SummaryStyle ?? SummaryStyle.Default,
7777
source.Options,
7878
source.BuildTimeout,
79+
source.WakeLock,
7980
configAnalyse.AsReadOnly()
8081
);
8182
}

0 commit comments

Comments
 (0)