diff --git a/docs/articles/guides/console-args.md b/docs/articles/guides/console-args.md index 8b79d6b506..d7c2e6f73b 100644 --- a/docs/articles/guides/console-args.md +++ b/docs/articles/guides/console-args.md @@ -243,7 +243,7 @@ dotnet run -c Release -- --filter * --runtimes net6.0 net8.0 --statisticalTest 5 * `--platform` the Platform that should be used. If not specified, the host process platform is used (default). AnyCpu/X86/X64/Arm/Arm64/LoongArch64. * `--runOncePerIteration` run the benchmark exactly once per iteration. * `--buildTimeout` build timeout in seconds. -* `--preventSleep` prevents the system from entering sleep or turning off the display. No/RequireSystem/RequireDisplay. +* `--preventSleep` prevents the system from entering sleep or turning off the display. None/RequireSystem/RequireDisplay. * `--wasmEngine` full path to a java script engine used to run the benchmarks, used by Wasm toolchain. * `--wasmMainJS` path to the test-main.js file used by Wasm toolchain. Mandatory when using \"--runtimes wasm\" * `--expose_wasm` arguments for the JavaScript engine used by Wasm toolchain. diff --git a/docs/articles/samples/IntroWakeLock.md b/docs/articles/samples/IntroWakeLock.md index 1cb3c5c96b..49368398bf 100644 --- a/docs/articles/samples/IntroWakeLock.md +++ b/docs/articles/samples/IntroWakeLock.md @@ -4,7 +4,7 @@ uid: BenchmarkDotNet.Samples.IntroWakeLock ## Sample: IntroWakeLock -Running Benchmarks usually takes enough time such that the system enters sleep or turns of the display. +Running benchmarks may sometimes take enough time such that the system enters sleep or turns off the display. Using a WakeLock prevents the system doing so. @@ -15,7 +15,7 @@ Using a WakeLock prevents the system doing so. ### Command line ``` ---preventSleep No +--preventSleep None ``` ``` --preventSleep RequireSystem diff --git a/src/BenchmarkDotNet/Configs/DefaultConfig.cs b/src/BenchmarkDotNet/Configs/DefaultConfig.cs index fc874b1359..ce4e073e71 100644 --- a/src/BenchmarkDotNet/Configs/DefaultConfig.cs +++ b/src/BenchmarkDotNet/Configs/DefaultConfig.cs @@ -87,7 +87,7 @@ public IEnumerable GetValidators() public TimeSpan BuildTimeout => TimeSpan.FromSeconds(120); - public WakeLockType WakeLock => WakeLockType.No; + public WakeLockType WakeLock => WakeLockType.RequireSystem; public string ArtifactsPath { diff --git a/src/BenchmarkDotNet/Configs/WakeLockType.cs b/src/BenchmarkDotNet/Configs/WakeLockType.cs index e676c57a51..b9bad818df 100644 --- a/src/BenchmarkDotNet/Configs/WakeLockType.cs +++ b/src/BenchmarkDotNet/Configs/WakeLockType.cs @@ -3,14 +3,14 @@ public enum WakeLockType { /// - /// Allows the system to enter sleep and/or turn off the display while benchmarks are running. + /// Forces the system to be in the working state while benchmarks are running. /// - No, + RequireSystem, /// - /// Forces the system to be in the working state while benchmarks are running. + /// Allows the system to enter sleep and/or turn off the display while benchmarks are running. /// - RequireSystem, + None, /// /// Forces the display to be on while benchmarks are running. diff --git a/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs b/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs index 63090f68f2..745f2e9520 100644 --- a/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs +++ b/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs @@ -180,7 +180,7 @@ public bool UseDisassemblyDiagnoser [Option("buildTimeout", Required = false, HelpText = "Build timeout in seconds.")] public int? TimeOutInSeconds { get; set; } - [Option("preventSleep", Required = false, HelpText = "Prevents the system from entering sleep or turning off the display. No/RequireSystem/RequireDisplay.")] + [Option("preventSleep", Required = false, HelpText = "Prevents the system from entering sleep or turning off the display. None/RequireSystem/RequireDisplay.")] public WakeLockType? WakeLock { get; set; } [Option("stopOnFirstError", Required = false, Default = false, HelpText = "Stop on first error.")] diff --git a/src/BenchmarkDotNet/Running/WakeLock.cs b/src/BenchmarkDotNet/Running/WakeLock.cs index 12e115e2e3..87bc205735 100644 --- a/src/BenchmarkDotNet/Running/WakeLock.cs +++ b/src/BenchmarkDotNet/Running/WakeLock.cs @@ -11,8 +11,12 @@ internal partial class WakeLock public static WakeLockType GetWakeLockType(BenchmarkRunInfo[] benchmarkRunInfos) => benchmarkRunInfos.Select(static i => i.Config.WakeLock).Max(); + private static readonly bool OsVersionIsSupported = + // Must be windows 7 or greater + OsDetector.IsWindows() && Environment.OSVersion.Version >= new Version(6, 1); + public static IDisposable Request(WakeLockType wakeLockType, string reason) => - wakeLockType == WakeLockType.No || !OsDetector.IsWindows() ? null : new WakeLockSentinel(wakeLockType, reason); + wakeLockType == WakeLockType.None || !OsVersionIsSupported ? null : new WakeLockSentinel(wakeLockType, reason); private class WakeLockSentinel : DisposeAtProcessTermination { diff --git a/tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs b/tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs index bfe27c8370..3a0933a12d 100644 --- a/tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs +++ b/tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs @@ -25,13 +25,13 @@ public class WakeLockTests(ITestOutputHelper output) : BenchmarkTestExecutor(out [Fact] public void ConfigurationDefaultValue() { - Assert.Equal(WakeLockType.No, DefaultConfig.Instance.WakeLock); - Assert.Equal(WakeLockType.No, new DebugBuildConfig().WakeLock); - Assert.Equal(WakeLockType.No, new DebugInProcessConfig().WakeLock); + Assert.Equal(WakeLockType.RequireSystem, DefaultConfig.Instance.WakeLock); + Assert.Equal(WakeLockType.RequireSystem, new DebugBuildConfig().WakeLock); + Assert.Equal(WakeLockType.RequireSystem, new DebugInProcessConfig().WakeLock); } [TheoryEnvSpecific(EnvRequirement.NonWindows)] - [InlineData(WakeLockType.No)] + [InlineData(WakeLockType.None)] [InlineData(WakeLockType.RequireSystem)] [InlineData(WakeLockType.RequireDisplay)] public void WakeLockIsWindowsOnly(WakeLockType wakeLockType) @@ -43,7 +43,7 @@ public void WakeLockIsWindowsOnly(WakeLockType wakeLockType) [FactEnvSpecific(EnvRequirement.WindowsOnly)] public void WakeLockSleepOrDisplayIsAllowed() { - using IDisposable wakeLock = WakeLock.Request(WakeLockType.No, "dummy"); + using IDisposable wakeLock = WakeLock.Request(WakeLockType.None, "dummy"); Assert.Null(wakeLock); } @@ -83,7 +83,8 @@ [Benchmark] public void Sleep() { } [SupportedOSPlatform("windows")] #endif [TheoryEnvSpecific(EnvRequirement.WindowsOnly)] - [InlineData(typeof(Sleepy), "")] + [InlineData(typeof(Default), "SYSTEM")] + [InlineData(typeof(None), "")] [InlineData(typeof(RequireSystem), "SYSTEM")] [InlineData(typeof(RequireDisplay), "DISPLAY, SYSTEM")] public async Task BenchmarkRunnerAcquiresWakeLock(Type type, string expected) @@ -106,7 +107,9 @@ async Task WaitForBenchmarkRunningAndGetPowerRequests() } } - public class Sleepy : Base { } + public class Default : Base { } + + [WakeLock(WakeLockType.None)] public class None : Base { } [WakeLock(WakeLockType.RequireSystem)] public class RequireSystem : Base { } diff --git a/tests/BenchmarkDotNet.Tests/ConfigParserTests.cs b/tests/BenchmarkDotNet.Tests/ConfigParserTests.cs index 45caf9e603..c47b3ac000 100644 --- a/tests/BenchmarkDotNet.Tests/ConfigParserTests.cs +++ b/tests/BenchmarkDotNet.Tests/ConfigParserTests.cs @@ -367,9 +367,9 @@ public void WhenUserDoesNotSpecifyTimeoutTheDefaultValueIsUsed() [Fact] public void UserCanSpecifyWakeLock() { - var config = ConfigParser.Parse(["--preventSleep", "RequireSystem"], new OutputLogger(Output)).config; + var config = ConfigParser.Parse(["--preventSleep", "RequireDisplay"], new OutputLogger(Output)).config; - Assert.Equal(WakeLockType.RequireSystem, config.WakeLock); + Assert.Equal(WakeLockType.RequireDisplay, config.WakeLock); } [Fact] @@ -377,7 +377,7 @@ public void WhenUserDoesNotSpecifyWakeLockTheDefaultValueIsUsed() { var config = ConfigParser.Parse([], new OutputLogger(Output)).config; - Assert.Equal(WakeLockType.No, config.WakeLock); + Assert.Equal(DefaultConfig.Instance.WakeLock, config.WakeLock); } [Theory] diff --git a/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs b/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs index cdd7c06a0b..ec16a3be0c 100644 --- a/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs +++ b/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs @@ -387,10 +387,10 @@ public void WhenWakeLockIsNotSpecifiedTheDefaultValueIsUsed() [Fact] public void CustomWakeLockHasPrecedenceOverDefaultWakeLock() { - WakeLockType customTimeout = WakeLockType.RequireDisplay; - var mutable = ManualConfig.CreateEmpty().WithWakeLock(customTimeout); + WakeLockType customWakeLock = WakeLockType.RequireDisplay; + var mutable = ManualConfig.CreateEmpty().WithWakeLock(customWakeLock); var final = ImmutableConfigBuilder.Create(mutable); - Assert.Equal(customTimeout, final.WakeLock); + Assert.Equal(customWakeLock, final.WakeLock); } [Theory] @@ -398,15 +398,15 @@ public void CustomWakeLockHasPrecedenceOverDefaultWakeLock() [InlineData(true)] public void WhenTwoCustomWakeLocksAreProvidedTheLongerOneIsUsed(bool direction) { - var system = ManualConfig.CreateEmpty().WithWakeLock(WakeLockType.RequireSystem); + var none = ManualConfig.CreateEmpty().WithWakeLock(WakeLockType.None); var display = ManualConfig.CreateEmpty().WithWakeLock(WakeLockType.RequireDisplay); if (direction) - system.Add(display); + none.Add(display); else - display.Add(system); + display.Add(none); - var final = ImmutableConfigBuilder.Create(direction ? system : display); + var final = ImmutableConfigBuilder.Create(direction ? none : display); Assert.Equal(WakeLockType.RequireDisplay, final.WakeLock); }