-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyInstanceBenchmark.cs
More file actions
52 lines (48 loc) · 1.64 KB
/
Copy pathProxyInstanceBenchmark.cs
File metadata and controls
52 lines (48 loc) · 1.64 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
using System;
using System.Linq.Expressions;
using BenchmarkDotNet.Attributes;
using ProxyGenerator.Core;
namespace ProxyGenerator.Benchmark
{
public class ProxyInstanceBenchmark
{
private Type _generatedProxyType;
private Func<ITest> _expressionTreeConstruct;
[GlobalSetup]
public void Setup()
{
_generatedProxyType = ProxyMaker.CreateProxyType(typeof(ITest));
Expression<Func<ITest>> expression = Expression.Lambda<Func<ITest>>(Expression.New(_generatedProxyType.GetConstructors()[0],
Expression.New(typeof(DefaultImpl)),Expression.Constant(Array.Empty<IInterceptor>())));
_expressionTreeConstruct = expression.Compile();
}
public interface ITest
{
void Test();
}
public class DefaultImpl : ITest
{
public void Test()
{
}
}
public class CompileTimeProxy : ITest
{
private readonly ITest _instance;
public CompileTimeProxy(ITest instance)
{
_instance = instance;
}
public void Test()
{
_instance.Test();
}
}
[Benchmark]
public void ManualCreateObject() => new CompileTimeProxy(new DefaultImpl()).Test();
[Benchmark]
public void ProxyInstantiateByActivator() => (Activator.CreateInstance(_generatedProxyType, new DefaultImpl(),Array.Empty<IInterceptor>()) as ITest)!.Test();
[Benchmark]
public void ProxyInstantiateByExpressionTree() => _expressionTreeConstruct().Test();
}
}