|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using BenchmarkDotNet.Environments; |
| 4 | +using BenchmarkDotNet.Jobs; |
| 5 | +using BenchmarkDotNet.Running; |
| 6 | +using BenchmarkDotNet.Toolchains.CsProj; |
| 7 | +using BenchmarkDotNet.Validators; |
| 8 | +using System.Linq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace BenchmarkDotNet.Tests.Validators; |
| 12 | + |
| 13 | +public class RuntimeValidatorTests |
| 14 | +{ |
| 15 | + [Fact] |
| 16 | + public void SameRuntime_Should_Success() |
| 17 | + { |
| 18 | + // Arrange |
| 19 | + var config = new TestConfig1().CreateImmutableConfig(); |
| 20 | + var runInfo = BenchmarkConverter.TypeToBenchmarks(typeof(DummyBenchmark), config); |
| 21 | + var parameters = new ValidationParameters(runInfo.BenchmarksCases, config); |
| 22 | + |
| 23 | + // Act |
| 24 | + var errors = RuntimeValidator.DontFailOnError.Validate(parameters).Select(e => e.Message).ToArray(); |
| 25 | + |
| 26 | + // Assert |
| 27 | + Assert.Empty(errors); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void NullRuntimeMixed_Should_Failed() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var config = new TestConfig2().CreateImmutableConfig(); |
| 35 | + var runInfo = BenchmarkConverter.TypeToBenchmarks(typeof(DummyBenchmark), config); |
| 36 | + var parameters = new ValidationParameters(runInfo.BenchmarksCases, config); |
| 37 | + |
| 38 | + // Act |
| 39 | + var errors = RuntimeValidator.DontFailOnError.Validate(parameters).Select(e => e.Message).ToArray(); |
| 40 | + |
| 41 | + // Assert |
| 42 | + { |
| 43 | + var expectedMessage = "Job(Dry) doesn't have a Runtime characteristic. It's recommended to specify runtime by using WithRuntime explicitly."; |
| 44 | + Assert.Contains(expectedMessage, errors); |
| 45 | + } |
| 46 | + { |
| 47 | + var expectedMessage = "Job(Toolchain=.NET 10.0) doesn't have a Runtime characteristic. It's recommended to specify runtime by using WithRuntime explicitly."; |
| 48 | + Assert.Contains(expectedMessage, errors); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void NotNullRuntimeOnly_Should_Success() |
| 54 | + { |
| 55 | + // Arrange |
| 56 | + var config = new TestConfig3().CreateImmutableConfig(); |
| 57 | + var runInfo = BenchmarkConverter.TypeToBenchmarks(typeof(DummyBenchmark), config); |
| 58 | + var parameters = new ValidationParameters(runInfo.BenchmarksCases, config); |
| 59 | + |
| 60 | + // Act |
| 61 | + var errors = RuntimeValidator.DontFailOnError.Validate(parameters).Select(e => e.Message).ToArray(); |
| 62 | + |
| 63 | + // Assert |
| 64 | + Assert.Empty(errors); |
| 65 | + } |
| 66 | + |
| 67 | + public class DummyBenchmark |
| 68 | + { |
| 69 | + [Benchmark] |
| 70 | + public void Benchmark() |
| 71 | + { |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // TestConfig that expicitly specify runtime. |
| 76 | + private class TestConfig1 : ManualConfig |
| 77 | + { |
| 78 | + public TestConfig1() |
| 79 | + { |
| 80 | + var baseJob = Job.Dry; |
| 81 | + |
| 82 | + WithOption(ConfigOptions.DisableOptimizationsValidator, true); |
| 83 | + AddColumnProvider(DefaultConfig.Instance.GetColumnProviders().ToArray()); |
| 84 | + |
| 85 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp80)); |
| 86 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp90)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // TestConfig that contains job that don't specify runtime. |
| 91 | + private class TestConfig2 : ManualConfig |
| 92 | + { |
| 93 | + public TestConfig2() |
| 94 | + { |
| 95 | + var baseJob = Job.Dry; |
| 96 | + |
| 97 | + WithOption(ConfigOptions.DisableOptimizationsValidator, true); |
| 98 | + AddColumnProvider(DefaultConfig.Instance.GetColumnProviders().ToArray()); |
| 99 | + |
| 100 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp80)); |
| 101 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp90) |
| 102 | + .WithRuntime(CoreRuntime.Core90)); |
| 103 | + |
| 104 | + // Validate error message for auto generated jobid. |
| 105 | + AddJob(Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp10_0)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // TestConfig that expicitly specify runtime. |
| 110 | + private class TestConfig3 : ManualConfig |
| 111 | + { |
| 112 | + public TestConfig3() |
| 113 | + { |
| 114 | + var baseJob = Job.Dry; |
| 115 | + |
| 116 | + WithOption(ConfigOptions.DisableOptimizationsValidator, true); |
| 117 | + AddColumnProvider(DefaultConfig.Instance.GetColumnProviders().ToArray()); |
| 118 | + |
| 119 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp80) |
| 120 | + .WithRuntime(CoreRuntime.Core80)); ; |
| 121 | + AddJob(baseJob.WithToolchain(CsProjCoreToolchain.NetCoreApp90) |
| 122 | + .WithRuntime(CoreRuntime.Core90)); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments