Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,24 @@ private static Summary Run(BenchmarkRunInfo benchmarkRunInfo,
{
var benchmark = benchmarks[i];

powerManagementApplier.ApplyPerformancePlan(benchmark.Job.Environment.PowerPlanMode
?? benchmark.Job.ResolveValue(EnvironmentMode.PowerPlanModeCharacteristic, EnvironmentResolver.Instance).GetValueOrDefault());
bool userExplicitlySetPlan = benchmark.Job.HasValue(EnvironmentMode.PowerPlanModeCharacteristic);

var requestedPlan = benchmark.Job.Environment.PowerPlanMode
?? benchmark.Job.ResolveValue(EnvironmentMode.PowerPlanModeCharacteristic, EnvironmentResolver.Instance).GetValueOrDefault();

// If the plan is not explicitly set and would downgrade Ultimate, skip changing
if (!userExplicitlySetPlan && requestedPlan == PowerManagementApplier.Map(PowerPlan.HighPerformance))
{
var ultimateGuid = PowerManagementApplier.Map(PowerPlan.UltimatePerformance);
var currentPlan = PowerManagementHelper.CurrentPlan;
if (currentPlan.HasValue && currentPlan.Value == ultimateGuid)
{
logger.WriteLineInfo("Already on Ultimate Performance; not switching to High Performance since no plan explicitly set in Job.");
continue;
Copy link
Collaborator

@timcassell timcassell Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work, you're skipping the entire benchmark here. You should rather invert the branches, or move it to another function.

}
}

powerManagementApplier.ApplyPerformancePlan(requestedPlan);

var info = buildResults[benchmark];
var buildResult = info.buildResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
Expand Down Expand Up @@ -43,43 +45,37 @@ public void TestPowerPlanShouldNotChange()
Assert.Equal(userPlan, PowerManagementHelper.CurrentPlan);
}

[FactEnvSpecific("Should change to High Performance if user requests it and High Performance plan is present, even if currently on Ultimate Performance", EnvRequirement.WindowsOnly)]
public void ShouldSwitchToHighPerformanceIfPresentWhenRequestedEvenIfOnUltimate()
[FactEnvSpecific("Should keep power plan at Ultimate Performance if current power plan is Ultimate when a power plan is not specifically set", EnvRequirement.WindowsOnly)]
public void TestKeepingUltimatePowerPlan()
{
var ultimateGuid = PowerManagementApplier.Map(PowerPlan.UltimatePerformance);
var highGuid = PowerManagementApplier.Map(PowerPlan.HighPerformance);
var userPlan = PowerManagementHelper.CurrentPlan;

var logger = new OutputLogger(Output);
var powerManagementApplier = new PowerManagementApplier(logger);
if (!PowerManagementHelper.PlanExists(ultimateGuid))
{
Output.WriteLine("Ultimate Performance plan does not exist or cannot be activated. Skipping test.");
return;
}

PowerManagementHelper.Set(ultimateGuid);

powerManagementApplier.ApplyPerformancePlan(highGuid);
var job = Job.Default;

Assert.Equal(highGuid.ToString(), PowerManagementHelper.CurrentPlan.ToString());
var config = ManualConfig.CreateEmpty().AddJob(job);

BenchmarkRunner.Run<DummyBenchmark>(config);

Assert.Equal(ultimateGuid.ToString(), PowerManagementHelper.CurrentPlan.ToString());

PowerManagementHelper.Set(userPlan.Value);
powerManagementApplier.Dispose();
}

[FactEnvSpecific("Should not change plan if already High Performance", EnvRequirement.WindowsOnly)]
public void ShouldNotChangeIfAlreadyHighPerformance()
public class DummyBenchmark
{
var highGuid = PowerManagementApplier.Map(PowerPlan.HighPerformance);
var userPlan = PowerManagementHelper.CurrentPlan;

var logger = new OutputLogger(Output);
var powerManagementApplier = new PowerManagementApplier(logger);

PowerManagementHelper.Set(highGuid);

powerManagementApplier.ApplyPerformancePlan(highGuid);

Assert.Equal(highGuid.ToString(), PowerManagementHelper.CurrentPlan.ToString());

PowerManagementHelper.Set(userPlan.Value);
powerManagementApplier.Dispose();
[BenchmarkDotNet.Attributes.Benchmark]
public void DoNothing()
{
}
}
}
}
Loading