-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyInterceptorBenchmark.cs
More file actions
73 lines (68 loc) · 2.32 KB
/
Copy pathProxyInterceptorBenchmark.cs
File metadata and controls
73 lines (68 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using BenchmarkDotNet.Attributes;
using ProxyGenerator.Core;
namespace ProxyGenerator.Benchmark
{
public class ProxyInterceptorBenchmark
{
private ITest _manualCreatedInstance;
private ITest _generatedProxyInstance;
private ITest _windsorGeneratedProxy;
[GlobalSetup]
public void Setup()
{
_manualCreatedInstance = new Proxy(new DefaultImpl(),new PassThroughInterceptor());
_generatedProxyInstance = Activator.CreateInstance(ProxyMaker.CreateProxyType(typeof(ITest)),
new DefaultImpl(), new IInterceptor[] {new PassThroughInterceptor()}) as ITest;
_windsorGeneratedProxy = new Castle.DynamicProxy.ProxyGenerator().CreateInterfaceProxyWithTarget<ITest>(new DefaultImpl(),new WindsorPassThroughInterceptor());
}
public class PassThroughInterceptor : IInterceptor
{
public virtual object Intercept(IInvocation invocation, Func<object> next)
{
return next();
}
}
public class WindsorPassThroughInterceptor : Castle.DynamicProxy.IInterceptor
{
public void Intercept(Castle.DynamicProxy.IInvocation invocation)
{
invocation.Proceed();
}
}
public interface ITest
{
void Test();
}
public class DefaultImpl : ITest
{
public void Test()
{
}
}
public class Proxy : ITest
{
private readonly ITest _instance;
private readonly IInterceptor _interceptor;
public Proxy(ITest instance,IInterceptor interceptor)
{
_instance = instance;
_interceptor = interceptor;
}
public void Test()
{
_interceptor.Intercept(null, () =>
{
_instance.Test();
return null;
});
}
}
[Benchmark]
public void CompileTimeProxyCall() => _manualCreatedInstance.Test();
[Benchmark]
public void ProxyCall() => _generatedProxyInstance.Test();
[Benchmark]
public void WindsorProxyCall() => _windsorGeneratedProxy.Test();
}
}