Skip to content

Commit

Permalink
Fix async GlobalSetup/GlobalCleanup not being awaited with InProcessE…
Browse files Browse the repository at this point in the history
…mit toolchain.
  • Loading branch information
timcassell committed Feb 17, 2023
1 parent 43138da commit a0ebccf
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ private static void EmitNoArgsMethodCallPopReturn(
private TypeBuilder runnableBuilder;
private ConsumableTypeInfo consumableInfo;
private ConsumeEmitter consumeEmitter;
private ConsumableTypeInfo globalSetupReturnInfo;
private ConsumableTypeInfo globalCleanupReturnInfo;
private ConsumableTypeInfo iterationSetupReturnInfo;
private ConsumableTypeInfo iterationCleanupReturnInfo;

private FieldBuilder globalSetupActionField;
private FieldBuilder globalCleanupActionField;
Expand Down Expand Up @@ -356,13 +360,22 @@ private void InitForEmitRunnable(BenchmarkBuildInfo newBenchmark)

consumableInfo = new ConsumableTypeInfo(benchmark.BenchmarkCase.Descriptor.WorkloadMethod.ReturnType);
consumeEmitter = ConsumeEmitter.GetConsumeEmitter(consumableInfo);
globalSetupReturnInfo = GetConsumableTypeInfo(benchmark.BenchmarkCase.Descriptor.GlobalSetupMethod?.ReturnType);
globalCleanupReturnInfo = GetConsumableTypeInfo(benchmark.BenchmarkCase.Descriptor.GlobalCleanupMethod?.ReturnType);
iterationSetupReturnInfo = GetConsumableTypeInfo(benchmark.BenchmarkCase.Descriptor.IterationSetupMethod?.ReturnType);
iterationCleanupReturnInfo = GetConsumableTypeInfo(benchmark.BenchmarkCase.Descriptor.IterationCleanupMethod?.ReturnType);

// Init types
runnableBuilder = DefineRunnableTypeBuilder(benchmark, moduleBuilder);
overheadDelegateType = EmitOverheadDelegateType();
workloadDelegateType = EmitWorkloadDelegateType();
}

private static ConsumableTypeInfo GetConsumableTypeInfo(Type methodReturnType)
{
return methodReturnType == null ? null : new ConsumableTypeInfo(methodReturnType);
}

private Type EmitOverheadDelegateType()
{
// .class public auto ansi sealed BenchmarkDotNet.Autogenerated.Runnable_0OverheadDelegate
Expand Down Expand Up @@ -887,34 +900,84 @@ private void EmitSetupCleanupMethods()
{
// Emit Setup/Cleanup methods
// We emit empty method instead of EmptyAction = "() => { }"
globalSetupMethod = EmitWrapperMethod(
GlobalSetupMethodName,
Descriptor.GlobalSetupMethod);
globalCleanupMethod = EmitWrapperMethod(
GlobalCleanupMethodName,
Descriptor.GlobalCleanupMethod);
iterationSetupMethod = EmitWrapperMethod(
IterationSetupMethodName,
Descriptor.IterationSetupMethod);
iterationCleanupMethod = EmitWrapperMethod(
IterationCleanupMethodName,
Descriptor.IterationCleanupMethod);
globalSetupMethod = EmitWrapperMethod(GlobalSetupMethodName, Descriptor.GlobalSetupMethod, globalSetupReturnInfo);
globalCleanupMethod = EmitWrapperMethod(GlobalCleanupMethodName, Descriptor.GlobalCleanupMethod, globalCleanupReturnInfo);
iterationSetupMethod = EmitWrapperMethod(IterationSetupMethodName, Descriptor.IterationSetupMethod, iterationSetupReturnInfo);
iterationCleanupMethod = EmitWrapperMethod(IterationCleanupMethodName, Descriptor.IterationCleanupMethod, iterationCleanupReturnInfo);
}

private MethodBuilder EmitWrapperMethod(string methodName, MethodInfo optionalTargetMethod)
private MethodBuilder EmitWrapperMethod(string methodName, MethodInfo optionalTargetMethod, ConsumableTypeInfo returnTypeInfo)
{
var methodBuilder = runnableBuilder.DefinePrivateVoidInstanceMethod(methodName);

var ilBuilder = methodBuilder.GetILGenerator();

if (optionalTargetMethod != null)
EmitNoArgsMethodCallPopReturn(methodBuilder, optionalTargetMethod, ilBuilder, forceDirectCall: true);
{
if (returnTypeInfo?.IsAwaitable == true)
{
EmitAwaitableSetupTeardown(methodBuilder, optionalTargetMethod, ilBuilder, returnTypeInfo);
}
else
{
EmitNoArgsMethodCallPopReturn(methodBuilder, optionalTargetMethod, ilBuilder, forceDirectCall: true);
}
}

ilBuilder.EmitVoidReturn(methodBuilder);

return methodBuilder;
}

private void EmitAwaitableSetupTeardown(
MethodBuilder methodBuilder,
MethodInfo targetMethod,
ILGenerator ilBuilder,
ConsumableTypeInfo returnTypeInfo)
{
if (targetMethod == null)
throw new ArgumentNullException(nameof(targetMethod));

if (returnTypeInfo.WorkloadMethodReturnType == typeof(void))
{
ilBuilder.Emit(OpCodes.Ldarg_0);
}
/*
// call for instance
// GlobalSetup();
IL_0006: ldarg.0
IL_0007: call instance void [BenchmarkDotNet]BenchmarkDotNet.Samples.SampleBenchmark::GlobalSetup()
*/
/*
// call for static
// GlobalSetup();
IL_0006: call string [BenchmarkDotNet]BenchmarkDotNet.Samples.SampleBenchmark::GlobalCleanup()
*/
if (targetMethod.IsStatic)
{
ilBuilder.Emit(OpCodes.Call, targetMethod);

}
else if (methodBuilder.IsStatic)
{
throw new InvalidOperationException(
$"[BUG] Static method {methodBuilder.Name} tries to call instance member {targetMethod.Name}");
}
else
{
ilBuilder.Emit(OpCodes.Ldarg_0);
ilBuilder.Emit(OpCodes.Call, targetMethod);
}

/*
// BenchmarkDotNet.Helpers.AwaitHelper.GetResult(...);
IL_000e: call !!0 BenchmarkDotNet.Helpers.AwaitHelper::GetResult<int32>(valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<!!0>)
*/

ilBuilder.Emit(OpCodes.Call, returnTypeInfo.GetResultMethod);
ilBuilder.Emit(OpCodes.Pop);
}

private void EmitCtorBody()
{
var ilBuilder = ctorMethod.GetILGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Reports.Summary CanExecute<TBenchmark>(IConfig config = null, bool fullVa
/// <param name="config">Optional custom config to be used instead of the default</param>
/// <param name="fullValidation">Optional: disable validation (default = true/enabled)</param>
/// <returns>The summary from the benchmark run</returns>
protected Reports.Summary CanExecute(Type type, IConfig config = null, bool fullValidation = true)
public Reports.Summary CanExecute(Type type, IConfig config = null, bool fullValidation = true)
{
// Add logging, so the Benchmark execution is in the TestRunner output (makes Debugging easier)
if (config == null)
Expand Down
114 changes: 101 additions & 13 deletions tests/BenchmarkDotNet.IntegrationTests/InProcessEmitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,35 +239,123 @@ public static ValueTask<decimal> InvokeOnceStaticValueTaskOfT()
}
}

[Fact]
public void InProcessEmitToolchainSupportsIterationSetupAndCleanup()
[Theory]
[InlineData(typeof(IterationSetupCleanup))]
[InlineData(typeof(GlobalSetupCleanupTask))]
[InlineData(typeof(GlobalSetupCleanupValueTask))]
[InlineData(typeof(GlobalSetupCleanupValueTaskSource))]
public void InProcessEmitToolchainSupportsSetupAndCleanup(Type benchmarkType)
{
var logger = new OutputLogger(Output);
var config = CreateInProcessConfig(logger);

WithIterationSetupAndCleanup.SetupCounter = 0;
WithIterationSetupAndCleanup.BenchmarkCounter = 0;
WithIterationSetupAndCleanup.CleanupCounter = 0;
Counters.SetupCounter = 0;
Counters.BenchmarkCounter = 0;
Counters.CleanupCounter = 0;

var summary = CanExecute<WithIterationSetupAndCleanup>(config);
var summary = CanExecute(benchmarkType, config);

Assert.Equal(1, WithIterationSetupAndCleanup.SetupCounter);
Assert.Equal(16, WithIterationSetupAndCleanup.BenchmarkCounter);
Assert.Equal(1, WithIterationSetupAndCleanup.CleanupCounter);
Assert.Equal(1, Counters.SetupCounter);
Assert.Equal(16, Counters.BenchmarkCounter);
Assert.Equal(1, Counters.CleanupCounter);
}

public class WithIterationSetupAndCleanup
private static class Counters
{
public static int SetupCounter, BenchmarkCounter, CleanupCounter;
}

public class IterationSetupCleanup
{
[IterationSetup]
public void Setup() => Interlocked.Increment(ref SetupCounter);
public void Setup() => Interlocked.Increment(ref Counters.SetupCounter);

[Benchmark]
public void Benchmark() => Interlocked.Increment(ref BenchmarkCounter);
public void Benchmark() => Interlocked.Increment(ref Counters.BenchmarkCounter);

[IterationCleanup]
public void Cleanup() => Interlocked.Increment(ref CleanupCounter);
public void Cleanup() => Interlocked.Increment(ref Counters.CleanupCounter);
}

public class GlobalSetupCleanupTask
{
[GlobalSetup]
public static async Task GlobalSetup()
{
await Task.Yield();
Interlocked.Increment(ref Counters.SetupCounter);
}

[GlobalCleanup]
public async Task GlobalCleanup()
{
await Task.Yield();
Interlocked.Increment(ref Counters.CleanupCounter);
}

[Benchmark]
public void InvokeOnceVoid()
{
Interlocked.Increment(ref Counters.BenchmarkCounter);
}
}

public class GlobalSetupCleanupValueTask
{
[GlobalSetup]
public static async ValueTask GlobalSetup()
{
await Task.Yield();
Interlocked.Increment(ref Counters.SetupCounter);
}

[GlobalCleanup]
public async ValueTask GlobalCleanup()
{
await Task.Yield();
Interlocked.Increment(ref Counters.CleanupCounter);
}

[Benchmark]
public void InvokeOnceVoid()
{
Interlocked.Increment(ref Counters.BenchmarkCounter);
}
}

public class GlobalSetupCleanupValueTaskSource
{
private readonly static ValueTaskSource<int> valueTaskSource = new ();

[GlobalSetup]
public static ValueTask GlobalSetup()
{
valueTaskSource.Reset();
Task.Delay(1).ContinueWith(_ =>
{
Interlocked.Increment(ref Counters.SetupCounter);
valueTaskSource.SetResult(42);
});
return new ValueTask(valueTaskSource, valueTaskSource.Token);
}

[GlobalCleanup]
public ValueTask GlobalCleanup()
{
valueTaskSource.Reset();
Task.Delay(1).ContinueWith(_ =>
{
Interlocked.Increment(ref Counters.CleanupCounter);
valueTaskSource.SetResult(42);
});
return new ValueTask(valueTaskSource, valueTaskSource.Token);
}

[Benchmark]
public void InvokeOnceVoid()
{
Interlocked.Increment(ref Counters.BenchmarkCounter);
}
}
}
}

0 comments on commit a0ebccf

Please sign in to comment.