Skip to content

Instantly share code, notes, and snippets.

@havarnov
Created September 21, 2023 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havarnov/219005e6d151e3932aacc0f885b331cb to your computer and use it in GitHub Desktop.
Save havarnov/219005e6d151e3932aacc0f885b331cb to your computer and use it in GitHub Desktop.
using AspectCore.Configuration;
using AspectCore.DynamicProxy;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
_ = BenchmarkRunner.Run<Benchmark>();
public class LoggerInterceptor : AbstractInterceptor
{
public override async Task Invoke(AspectContext context, AspectDelegate next)
{
await next(context); // Run the function
}
}
public interface IFoo
{
Task<int> Bar(int input);
}
class Foo : IFoo
{
public async Task<int> Bar(int input)
{
await Task.Delay(TimeSpan.FromMilliseconds(10));
return input * 2;
}
}
public class Benchmark
{
private IFoo _normal = new Foo();
private IFoo _proxied;
public Benchmark()
{
var generator = new ProxyGeneratorBuilder()
.Configure(c =>
{
c.Interceptors.Add(
new TypeInterceptorFactory(
typeof(LoggerInterceptor),
Array.Empty<object>()));
})
.Build();
var actual = new Foo();
_proxied = generator.CreateInterfaceProxy<IFoo>(actual);
}
[Benchmark]
public async Task<int> Normal()
{
var result = await _normal.Bar(42);
return result;
}
[Benchmark]
public async Task<int> Proxied()
{
var result = await _proxied.Bar(42);
return result;
}
}
@havarnov
Copy link
Author

// * Summary *

BenchmarkDotNet=v0.13.4, OS=macOS 13.2.1 (22D68) [Darwin 22.3.0]
Apple M1, 1 CPU, 8 logical and 8 physical cores
.NET SDK=7.0.400
  [Host]     : .NET 7.0.10 (7.0.1023.36312), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 7.0.10 (7.0.1023.36312), Arm64 RyuJIT AdvSIMD


|  Method |     Mean |    Error |   StdDev |
|-------- |---------:|---------:|---------:|
|  Normal | 10.97 ms | 0.033 ms | 0.027 ms |
| Proxied | 10.98 ms | 0.043 ms | 0.040 ms |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment