Skip to content

Commit 39817d3

Browse files
committed
Validate NoInlining in the benchmark process.
1 parent a8c27b4 commit 39817d3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/BenchmarkDotNet/Templates/BenchmarkType.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
host.WriteLine($"// Job: {job.DisplayInfo}");
1818
host.WriteLine();
1919

20-
System.Collections.Generic.IEnumerable<BenchmarkDotNet.Validators.ValidationError> errors = BenchmarkDotNet.Environments.BenchmarkEnvironmentInfo.Validate(job);
20+
System.Collections.Generic.IEnumerable<BenchmarkDotNet.Validators.ValidationError> errors = BenchmarkDotNet.Validators.BenchmarkProcessValidator.Validate(job, instance);
2121
if (BenchmarkDotNet.Validators.ValidationErrorReporter.ReportIfAny(errors, host))
2222
return;
2323

src/BenchmarkDotNet/Toolchains/InProcess/Emit/Implementation/Runnable/RunnableReuse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static (Job, EngineParameters, IEngineFactory) PrepareForRun<T>(T instanc
2424
var job = CreateJob(benchmarkCase);
2525
DumpJob(host, job);
2626

27-
var errors = BenchmarkEnvironmentInfo.Validate(job);
27+
var errors = BenchmarkProcessValidator.Validate(job, instance);
2828
if (ValidationErrorReporter.ReportIfAny(errors, host))
2929
return default;
3030

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Environments;
3+
using BenchmarkDotNet.Extensions;
4+
using BenchmarkDotNet.Jobs;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
9+
namespace BenchmarkDotNet.Validators
10+
{
11+
public static class BenchmarkProcessValidator
12+
{
13+
public static IEnumerable<ValidationError> Validate(Job job, object benchmarkInstance)
14+
{
15+
foreach (var validationError in BenchmarkEnvironmentInfo.Validate(job))
16+
{
17+
yield return validationError;
18+
}
19+
20+
var inlineableBenchmarks = benchmarkInstance.GetType().BaseType
21+
.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
22+
.Where(method => method.HasAttribute<BenchmarkAttribute>() && !method.MethodImplementationFlags.HasFlag(MethodImplAttributes.NoInlining));
23+
foreach (var benchmark in inlineableBenchmarks)
24+
{
25+
yield return new ValidationError(
26+
true,
27+
$"Benchmark method `{benchmark.Name}` does not have MethodImplOptions.NoInlining flag set." +
28+
$" You may need to rebuild your project, or apply it manually if you are not using MSBuild to build your project."
29+
);
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)