Skip to content

Commit ee248c3

Browse files
authored
feat: Add option to disable parallel build (#2725)
* feat: add option to disable parallel build * chore: fix code pointed out by review * chore: add parallel/sequential build support based on options
1 parent 924ca6d commit ee248c3

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/BenchmarkDotNet/Configs/ConfigOptions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public enum ConfigOptions
4848
/// <summary>
4949
/// Continue the execution if the last run was stopped.
5050
/// </summary>
51-
Resume = 1 << 9
51+
Resume = 1 << 9,
52+
/// <summary>
53+
/// Determines whether parallel build of benchmark projects should be disabled.
54+
/// </summary>
55+
DisableParallelBuild = 1 << 10,
5256
}
5357

5458
internal static class ConfigOptionsExtensions

src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs

+48-5
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,32 @@ internal static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos)
8282
var globalChronometer = Chronometer.Start();
8383

8484

85-
var buildPartitions = BenchmarkPartitioner.CreateForBuild(supportedBenchmarks, resolver);
86-
eventProcessor.OnStartBuildStage(buildPartitions);
87-
var buildResults = BuildInParallel(compositeLogger, rootArtifactsFolderPath, buildPartitions, in globalChronometer, eventProcessor);
85+
var parallelBuildBenchmarks = supportedBenchmarks.Where(x => !x.Config.Options.IsSet(ConfigOptions.DisableParallelBuild)).ToArray();
86+
var parallelBuildPartitions = BenchmarkPartitioner.CreateForBuild(parallelBuildBenchmarks, resolver);
87+
88+
var sequentialBuildBenchmarks = supportedBenchmarks.Where(x => x.Config.Options.IsSet(ConfigOptions.DisableParallelBuild)).ToArray();
89+
var sequentialBuildPartitions = BenchmarkPartitioner.CreateForBuild(sequentialBuildBenchmarks, resolver);
90+
91+
eventProcessor.OnStartBuildStage([.. parallelBuildPartitions, .. sequentialBuildPartitions]);
92+
93+
var buildResults = new Dictionary<BuildPartition, BuildResult>();
94+
if (parallelBuildBenchmarks.Length > 0)
95+
{
96+
var results = BuildInParallel(compositeLogger, rootArtifactsFolderPath, parallelBuildPartitions, in globalChronometer, eventProcessor);
97+
foreach (var kvp in results)
98+
{
99+
buildResults.Add(kvp.Key, kvp.Value);
100+
}
101+
}
102+
103+
if (sequentialBuildBenchmarks.Length > 0)
104+
{
105+
var results = BuildSequential(compositeLogger, rootArtifactsFolderPath, sequentialBuildPartitions, in globalChronometer, eventProcessor);
106+
foreach (var kvp in results)
107+
{
108+
buildResults.Add(kvp.Key, kvp.Value);
109+
}
110+
}
88111

89112
var allBuildsHaveFailed = buildResults.Values.All(buildResult => !buildResult.IsBuildSuccess);
90113

@@ -404,10 +427,30 @@ private static Dictionary<BuildPartition, BuildResult> BuildInParallel(ILogger l
404427
logger.WriteLineHeader($"// ***** Done, took {GetFormattedDifference(afterParallelBuild, afterSequentialBuild)} *****");
405428

406429
return buildResults;
430+
}
431+
432+
private static Dictionary<BuildPartition, BuildResult> BuildSequential(ILogger logger, string rootArtifactsFolderPath, BuildPartition[] buildPartitions, in StartedClock globalChronometer, EventProcessor eventProcessor)
433+
{
434+
logger.WriteLineHeader($"// ***** Building {buildPartitions.Length} exe(s) in Sequential: Start *****");
435+
436+
var beforeBuild = globalChronometer.GetElapsed();
437+
438+
var buildResults = new Dictionary<BuildPartition, BuildResult>();
439+
foreach (var buildPartition in buildPartitions)
440+
{
441+
buildResults[buildPartition] = Build(buildPartition, rootArtifactsFolderPath, logger);
442+
eventProcessor.OnBuildComplete(buildPartition, buildResults[buildPartition]);
443+
}
444+
445+
var afterBuild = globalChronometer.GetElapsed();
446+
447+
logger.WriteLineHeader($"// ***** Done, took {GetFormattedDifference(beforeBuild, afterBuild)} *****");
448+
449+
return buildResults;
450+
}
407451

408-
static string GetFormattedDifference(ClockSpan before, ClockSpan after)
452+
private static string GetFormattedDifference(ClockSpan before, ClockSpan after)
409453
=> (after.GetTimeSpan() - before.GetTimeSpan()).ToFormattedTotalTime(DefaultCultureInfo.Instance);
410-
}
411454

412455
private static BuildResult Build(BuildPartition buildPartition, string rootArtifactsFolderPath, ILogger buildLogger)
413456
{

0 commit comments

Comments
 (0)