Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Threading.Tasks;
using BenchmarkDotNet;
using BenchmarkDotNet.Tasks;
namespace Exceptions
{
internal class Program
{
private static void Main()
{
new BenchmarkRunner().RunCompetition(new AsyncExceptions());
}
}
[BenchmarkTask(processCount:1, targetIterationCount: 3, warmupIterationCount:3)]
public class AsyncExceptions
{
private const int DepthToThrowException = 1;
[Benchmark]
public async void AsyncWithStackTrace()
{
try
{
await AsyncDeepMethod();
}
catch (Exception ex)
{
var s = ex.StackTrace;
}
}
[Benchmark]
public async void AsyncWithoutTrace()
{
try
{
await AsyncDeepMethod();
}
catch (Exception ex)
{
}
}
[Benchmark]
public async void AsyncWithoutException()
{
try
{
await AsyncDeepMethodWithoutException();
}
catch (Exception ex)
{
}
}
[Benchmark]
public void NormalWithStackTrace()
{
try
{
NormalDeepMethod();
}
catch (Exception ex)
{
var s = ex.StackTrace;
}
}
[Benchmark]
public void NormalWithoutTrace()
{
try
{
NormalDeepMethod();
}
catch (Exception ex)
{
}
}
private static void NormalDeepMethod(int depth = 0)
{
if (depth == DepthToThrowException)
{
throw new Exception("Deep exception");
}
else
NormalDeepMethod(depth + 1);
}
private static async Task AsyncDeepMethod(int depth = 0)
{
if (depth == DepthToThrowException)
{
throw new Exception("Deep exception");
}
else
await AsyncDeepMethod(depth + 1);
}
private static async Task AsyncDeepMethodWithoutException(int depth = 0)
{
if (depth == DepthToThrowException)
{
return;
}
else
await AsyncDeepMethod(depth + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment