-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathJitOptimizationsValidator.cs
More file actions
53 lines (47 loc) · 3.01 KB
/
JitOptimizationsValidator.cs
File metadata and controls
53 lines (47 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Extensions;
namespace BenchmarkDotNet.Validators
{
public class JitOptimizationsValidator : IValidator
{
public static readonly IValidator DontFailOnError = new JitOptimizationsValidator(false);
public static readonly IValidator FailOnError = new JitOptimizationsValidator(true);
private JitOptimizationsValidator(bool failOnErrors) => TreatsWarningsAsErrors = failOnErrors;
public bool TreatsWarningsAsErrors { get; }
public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters)
{
foreach (var group in validationParameters.Benchmarks.GroupBy(benchmark => benchmark.Descriptor.Type.GetTypeInfo().Assembly))
{
foreach (var referencedAssemblyName in group.Key.GetReferencedAssemblies())
{
var referencedAssembly = Assembly.Load(referencedAssemblyName);
// LINQPad.exe is non-optimized on purpose, see https://github.com/dotnet/BenchmarkDotNet/issues/580#issuecomment-345484889 for more details
// we don't warn about non-optimized dependency to LINQPad
// but we give extra hint if the dll with benchmark itself was build without optimization by LINQPad
if (referencedAssembly.IsJitOptimizationDisabled().IsTrue() && !referencedAssembly.IsLinqPad())
{
yield return new ValidationError(
TreatsWarningsAsErrors,
$"Assembly {group.Key.GetName().Name} which defines benchmarks references non-optimized {referencedAssemblyName.Name}" +
$"{Environment.NewLine} If you own this dependency, please, build it in RELEASE." +
$"{Environment.NewLine} If you don't, you can disable this policy by using 'config.WithOptions(ConfigOptions.DisableOptimizationsValidator)'.");
}
}
if (group.Key.IsJitOptimizationDisabled().IsTrue())
{
yield return new ValidationError(
TreatsWarningsAsErrors,
$"Assembly {group.Key.GetName().Name} which defines benchmarks is non-optimized" +
$"{Environment.NewLine} Benchmark was built without optimization enabled (most probably a DEBUG configuration). Please, build it in RELEASE." +
$"{Environment.NewLine} If you want to debug the benchmarks, please see https://benchmarkdotnet.org/articles/guides/troubleshooting.html#debugging-benchmarks."
+ (group.Key.IsLinqPad()
? $"{Environment.NewLine} Please enable optimizations in your LINQPad. Go to Preferences -> Query and select \"compile with /optimize+\""
: string.Empty));
}
}
}
}
}