#pragma warning disable SA1611, SA1615
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.InProcess;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using BenchmarkDotNet.Toolchains.InProcess.Emit.Implementation;
using BenchmarkDotNet.Toolchains.Parameters;
using BenchmarkDotNet.Toolchains.Results;
using HarmonyLib;
namespace Reactor.Benchmarks;
public class SingleThreadedInProcessEmitToolchain : Toolchain
{
public static readonly IToolchain Instance = new SingleThreadedInProcessEmitToolchain(true);
public SingleThreadedInProcessEmitToolchain(bool logOutput)
: base(nameof(SingleThreadedInProcessEmitToolchain), new InProcessEmitGenerator(), new InProcessEmitBuilder(), new SingleThreadedInProcessEmitExecutor(logOutput))
{
}
}
public class SingleThreadedInProcessEmitExecutor : IExecutor
{
/// Initializes a new instance of the class.
/// true if the output should be logged.
public SingleThreadedInProcessEmitExecutor(bool logOutput)
{
LogOutput = logOutput;
}
/// Gets a value indicating whether the output should be logged.
/// true if the output should be logged; otherwise, false.
public bool LogOutput { get; }
/// Executes the specified benchmark.
public ExecuteResult Execute(ExecuteParameters executeParameters)
{
var hostLogger = LogOutput ? executeParameters.Logger : NullLogger.Instance;
var host = new InProcessHost(executeParameters.BenchmarkCase, hostLogger, executeParameters.Diagnoser);
var exitCode = ExecuteCore(host, executeParameters);
return ((ExecuteResult) AccessTools.Method(typeof(ExecuteResult), "FromRunResults").Invoke(null, new object[] { host.RunResults, exitCode })!)!;
}
private int ExecuteCore(IHost host, ExecuteParameters parameters)
{
var generatedAssembly = ((InProcessEmitArtifactsPath) parameters.BuildResult.ArtifactsPaths).GeneratedAssembly;
return RunnableProgram.Run(parameters.BenchmarkId, generatedAssembly, parameters.BenchmarkCase, host);
}
}