Skip to content

Commit deb927d

Browse files
committed
Add GenerateAndBuildOnly Option
1 parent ee24d7b commit deb927d

File tree

9 files changed

+57
-2
lines changed

9 files changed

+57
-2
lines changed

docs/articles/configs/configoptions.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The config options let you customize some behavior of BenchmarkDotNet - mainly r
1010
Available config options are:
1111

1212
* `ConfigOptions.Default` - No configuration option is set - this is the default.
13+
* `ConfigOptions.GenerateAndBuildOnly` - Only run toolchain generator and builder without executor. Then performs normal cleanup.
1314
* `ConfigOptions.KeepBenchmarkFiles` - All auto-generated files should be kept after running the benchmarks (by default they are removed).
1415
* `ConfigOptions.JoinSummary` - All benchmarks results should be joined into a single summary (by default we have a summary per type).
1516
* `ConfigOptions.StopOnFirstError` - Benchmarking should be stopped after the first error (by default it's not).

src/BenchmarkDotNet/Analysers/EnvironmentAnalyser.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.Immutable;
23
using System.Linq;
34
using System.Reflection;
45
using System.Text;
@@ -8,6 +9,7 @@
89
using BenchmarkDotNet.Reports;
910
using BenchmarkDotNet.Toolchains.InProcess.Emit;
1011
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;
12+
using BenchmarkDotNet.Toolchains.Results;
1113

1214
namespace BenchmarkDotNet.Analysers
1315
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using BenchmarkDotNet.Configs;
3+
using JetBrains.Annotations;
4+
5+
namespace BenchmarkDotNet.Attributes
6+
{
7+
/// <summary>
8+
/// determines if benchmark should be run after being generated and built
9+
/// </summary>
10+
[PublicAPI]
11+
[AttributeUsage(AttributeTargets.Class)]
12+
public class GenerateAndBuildOnlyAttribute : Attribute, IConfigSource
13+
{
14+
public IConfig Config { get; }
15+
16+
public GenerateAndBuildOnlyAttribute(bool value = true)
17+
{
18+
Config = ManualConfig.CreateEmpty().WithOption(ConfigOptions.GenerateAndBuildOnly, value);
19+
}
20+
}
21+
}

src/BenchmarkDotNet/Configs/ConfigExtensions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public static class ConfigExtensions
8080
/// </summary>
8181
[PublicAPI] public static IConfig KeepBenchmarkFiles(this IConfig config, bool value = true) => config.WithOption(ConfigOptions.KeepBenchmarkFiles, value);
8282

83+
/// <summary>
84+
/// determines if we skip executing after running generator and builder
85+
/// </summary>
86+
[PublicAPI] public static IConfig GenerateAndBuildOnly(this IConfig config, bool value = true) => config.WithOption(ConfigOptions.GenerateAndBuildOnly, value);
87+
8388
/// <summary>
8489
/// determines if the exported result files should not be overwritten (be default they are overwritten)
8590
/// </summary>

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+
/// Don't run the benchmark after it's generated and built.
54+
/// </summary>
55+
GenerateAndBuildOnly = 1 << 10
5256
}
5357

5458
internal static class ConfigOptionsExtensions

src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public bool UseDisassemblyDiagnoser
8686
[Option("keepFiles", Required = false, Default = false, HelpText = "Determines if all auto-generated files should be kept or removed after running the benchmarks.")]
8787
public bool KeepBenchmarkFiles { get; set; }
8888

89+
[Option("generateAndBuildOnly", Required = false, Default = false, HelpText = "Determines if we only run generator and builder without executor.")]
90+
public bool GenerateAndBuildOnly { get; set; }
91+
8992
[Option("noOverwrite", Required = false, Default = false, HelpText = "Determines if the exported result files should not be overwritten (be default they are overwritten).")]
9093
public bool DontOverwriteResults { get; set; }
9194

src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ private static IConfig CreateConfig(CommandLineOptions options, IConfig globalCo
242242

243243
config.WithOption(ConfigOptions.JoinSummary, options.Join);
244244
config.WithOption(ConfigOptions.KeepBenchmarkFiles, options.KeepBenchmarkFiles);
245+
config.WithOption(ConfigOptions.GenerateAndBuildOnly, options.GenerateAndBuildOnly);
245246
config.WithOption(ConfigOptions.DontOverwriteResults, options.DontOverwriteResults);
246247
config.WithOption(ConfigOptions.StopOnFirstError, options.StopOnFirstError);
247248
config.WithOption(ConfigOptions.DisableLogFile, options.DisableLogFile);

src/BenchmarkDotNet/Reports/BenchmarkReport.cs

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public BenchmarkReport(
4747
?? (IReadOnlyDictionary<string, Metric>)ImmutableDictionary<string, Metric>.Empty;
4848
}
4949

50+
public BenchmarkReport(BenchmarkCase benchmarkCase, GenerateResult generateResult, BuildResult buildResult)
51+
{
52+
BenchmarkCase = benchmarkCase;
53+
GenerateResult = generateResult;
54+
BuildResult = buildResult;
55+
ExecuteResults = Array.Empty<ExecuteResult>();
56+
AllMeasurements = ImmutableList<Measurement>.Empty;
57+
Metrics = ImmutableDictionary<string, Metric>.Empty;
58+
}
59+
5060
public override string ToString() => $"{BenchmarkCase.DisplayInfo}, {AllMeasurements.Count} runs";
5161

5262
public IReadOnlyList<Measurement> GetResultRuns() => AllMeasurements.Where(r => r.Is(IterationMode.Workload, IterationStage.Result)).ToList();

src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,15 @@ private static Summary Run(BenchmarkRunInfo benchmarkRunInfo,
196196
if (!config.Options.IsSet(ConfigOptions.KeepBenchmarkFiles))
197197
artifactsToCleanup.AddRange(buildResult.ArtifactsToCleanup);
198198

199-
var report = RunCore(benchmark, info.benchmarkId, logger, resolver, buildResult);
199+
BenchmarkReport report;
200+
if (config.Options.IsSet(ConfigOptions.GenerateAndBuildOnly))
201+
{
202+
report = new BenchmarkReport(benchmark, buildResult, buildResult);
203+
reports.Add(report);
204+
continue;
205+
}
206+
207+
report = RunCore(benchmark, info.benchmarkId, logger, resolver, buildResult);
200208
if (report.AllMeasurements.Any(m => m.Operations == 0))
201209
throw new InvalidOperationException("An iteration with 'Operations == 0' detected");
202210
reports.Add(report);

0 commit comments

Comments
 (0)