Skip to content

simplify the MockRunner design #2268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/BenchmarkDotNet.Tests/Columns/RatioColumnTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Xml.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Tests.Mocks;
Expand All @@ -21,13 +22,12 @@ public RatioColumnTest(ITestOutputHelper output)
[Fact]
public void RatioColumnTest01()
{
var measurer = MockMeasurer.Create(name => name switch
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 summary = MockRunner.Run<BenchmarkClass>(output, measurer);

var ratioColumn = summary.GetColumns().FirstOrDefault(column => column.ColumnName == "Ratio");
Assert.NotNull(ratioColumn);
Expand Down
11 changes: 10 additions & 1 deletion tests/BenchmarkDotNet.Tests/Mocks/MockRunner.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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, IMockMeasurer measurer)
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")
{
Expand Down
11 changes: 0 additions & 11 deletions tests/BenchmarkDotNet.Tests/Mocks/Toolchain/IMockMeasurer.cs

This file was deleted.

27 changes: 0 additions & 27 deletions tests/BenchmarkDotNet.Tests/Mocks/Toolchain/MockMeasurer.cs

This file was deleted.

This file was deleted.

15 changes: 8 additions & 7 deletions tests/BenchmarkDotNet.Tests/Mocks/Toolchain/MockToolchain.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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;
Expand All @@ -12,14 +14,13 @@ namespace BenchmarkDotNet.Tests.Mocks.Toolchain
{
public class MockToolchain : IToolchain
{
private readonly IMockMeasurer measurer;

public MockToolchain(IMockMeasurer measurer) => this.measurer = measurer;
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 => new MockExecutor(measurer);
public IExecutor Executor { get; private set; }
public bool IsInProcess => false;
public IEnumerable<ValidationError> Validate(BenchmarkCase benchmarkCase, IResolver resolver) => ImmutableArray<ValidationError>.Empty;

Expand All @@ -38,11 +39,11 @@ private class MockBuilder : IBuilder

private class MockExecutor : IExecutor
{
private readonly IMockMeasurer mockMeasurer;
private readonly Func<BenchmarkCase, List<Measurement>> measurer;

public MockExecutor(IMockMeasurer mockMeasurer) => this.mockMeasurer = mockMeasurer;
public MockExecutor(Func<BenchmarkCase, List<Measurement>> measurer) => this.measurer = measurer;

public ExecuteResult Execute(ExecuteParameters executeParameters) => new (mockMeasurer.Measure(executeParameters.BenchmarkCase));
public ExecuteResult Execute(ExecuteParameters executeParameters) => new (measurer(executeParameters.BenchmarkCase));
}
}
}