Created
September 21, 2023 08:42
-
-
Save havarnov/219005e6d151e3932aacc0f885b331cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Author
havarnov
commented
Sep 21, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment