-
-
Notifications
You must be signed in to change notification settings - Fork 968
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce MockToolchain * simplify the design (#2268) --------- Co-authored-by: Adam Sitnik <[email protected]>
- Loading branch information
1 parent
59e17fc
commit f76c682
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Tests.Mocks; | ||
using BenchmarkDotNet.Tests.Mocks.Toolchain; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace BenchmarkDotNet.Tests.Columns | ||
{ | ||
public class RatioColumnTest | ||
{ | ||
private readonly ITestOutputHelper output; | ||
|
||
public RatioColumnTest(ITestOutputHelper output) | ||
{ | ||
this.output = output; | ||
} | ||
|
||
[Fact] | ||
public void RatioColumnTest01() | ||
{ | ||
var summary = MockRunner.Run<BenchmarkClass>(output, name => name switch | ||
{ | ||
"Foo" => new double[] { 2, 2, 2 }, | ||
"Bar" => new double[] { 4, 4, 4 }, | ||
_ => throw new InvalidOperationException() | ||
}); | ||
|
||
var ratioColumn = summary.GetColumns().FirstOrDefault(column => column.ColumnName == "Ratio"); | ||
Assert.NotNull(ratioColumn); | ||
|
||
var fooCase = summary.BenchmarksCases.First(c => c.Descriptor.WorkloadMethod.Name == "Foo"); | ||
var barCase = summary.BenchmarksCases.First(c => c.Descriptor.WorkloadMethod.Name == "Bar"); | ||
Assert.Equal("1.00", ratioColumn.GetValue(summary, fooCase)); | ||
Assert.Equal("2.00", ratioColumn.GetValue(summary, barCase)); | ||
} | ||
|
||
public class BenchmarkClass | ||
{ | ||
[Benchmark(Baseline = true)] | ||
public void Foo() { } | ||
|
||
[Benchmark] | ||
public void Bar() { } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Engines; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Tests.Mocks.Toolchain; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit.Abstractions; | ||
|
||
namespace BenchmarkDotNet.Tests.Mocks | ||
{ | ||
public static class MockRunner | ||
{ | ||
public static Summary Run<T>(ITestOutputHelper output, Func<string, double[]> measurer) | ||
=> Run<T>(output, benchmarkCase => measurer(benchmarkCase.Descriptor.WorkloadMethod.Name) | ||
.Select((value, i) => new Measurement(1, IterationMode.Workload, IterationStage.Result, i, 1, value)) | ||
.ToList()); | ||
|
||
public static Summary Run<T>(ITestOutputHelper output, Func<BenchmarkCase, List<Measurement>> measurer) | ||
{ | ||
var job = new Job("MockJob") | ||
{ | ||
Infrastructure = | ||
{ | ||
Toolchain = new MockToolchain(measurer) | ||
} | ||
}.Freeze(); | ||
|
||
var logger = new AccumulationLogger(); | ||
|
||
var config = DefaultConfig.Instance | ||
.WithOptions(ConfigOptions.DisableOptimizationsValidator) | ||
.AddJob(job) | ||
.AddLogger(logger); | ||
var summary = BenchmarkRunner.Run<T>(config); | ||
|
||
var exporter = MarkdownExporter.Mock; | ||
exporter.ExportToLog(summary, logger); | ||
output.WriteLine(logger.GetLog()); | ||
|
||
return summary; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/BenchmarkDotNet.Tests/Mocks/Toolchain/MockToolchain.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using BenchmarkDotNet.Characteristics; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Toolchains; | ||
using BenchmarkDotNet.Toolchains.Parameters; | ||
using BenchmarkDotNet.Toolchains.Results; | ||
using BenchmarkDotNet.Validators; | ||
|
||
namespace BenchmarkDotNet.Tests.Mocks.Toolchain | ||
{ | ||
public class MockToolchain : IToolchain | ||
{ | ||
public MockToolchain(Func<BenchmarkCase, List<Measurement>> measurer) | ||
=> Executor = new MockExecutor(measurer); | ||
|
||
public string Name => nameof(MockToolchain); | ||
public IGenerator Generator => new MockGenerator(); | ||
public IBuilder Builder => new MockBuilder(); | ||
public IExecutor Executor { get; private set; } | ||
public bool IsInProcess => false; | ||
public IEnumerable<ValidationError> Validate(BenchmarkCase benchmarkCase, IResolver resolver) => ImmutableArray<ValidationError>.Empty; | ||
|
||
public override string ToString() => GetType().Name; | ||
|
||
private class MockGenerator : IGenerator | ||
{ | ||
public GenerateResult GenerateProject(BuildPartition buildPartition, ILogger logger, string rootArtifactsFolderPath) | ||
=> GenerateResult.Success(ArtifactsPaths.Empty, ImmutableArray<string>.Empty); | ||
} | ||
|
||
private class MockBuilder : IBuilder | ||
{ | ||
public BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger) => BuildResult.Success(generateResult); | ||
} | ||
|
||
private class MockExecutor : IExecutor | ||
{ | ||
private readonly Func<BenchmarkCase, List<Measurement>> measurer; | ||
|
||
public MockExecutor(Func<BenchmarkCase, List<Measurement>> measurer) => this.measurer = measurer; | ||
|
||
public ExecuteResult Execute(ExecuteParameters executeParameters) => new (measurer(executeParameters.BenchmarkCase)); | ||
} | ||
} | ||
} |