-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathExpressionResolverBenchmark.cs
More file actions
67 lines (52 loc) · 3.01 KB
/
ExpressionResolverBenchmark.cs
File metadata and controls
67 lines (52 loc) · 3.01 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
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.Projectables.Benchmarks.Helpers;
using EntityFrameworkCore.Projectables.Services;
namespace EntityFrameworkCore.Projectables.Benchmarks
{
/// <summary>
/// Micro-benchmarks <see cref="ProjectionExpressionResolver.FindGeneratedExpression"/> in
/// isolation (no EF Core overhead) to directly compare the static registry path against
/// the reflection-based path (<see cref="ProjectionExpressionResolver.FindGeneratedExpressionViaReflection"/>).
/// </summary>
[MemoryDiagnoser]
public class ExpressionResolverBenchmark
{
private static readonly MemberInfo _propertyMember =
typeof(TestEntity).GetProperty(nameof(TestEntity.IdPlus1))!;
private static readonly MemberInfo _methodMember =
typeof(TestEntity).GetMethod(nameof(TestEntity.IdPlus1Method))!;
private static readonly MemberInfo _methodWithParamMember =
typeof(TestEntity).GetMethod(nameof(TestEntity.IdPlusDelta), new[] { typeof(int) })!;
private static readonly MemberInfo _copyConstructorMember =
typeof(TestEntity).GetConstructor(new[] { typeof(TestEntity) })!;
private readonly ProjectionExpressionResolver _resolver = new();
// ── Registry (source-generated) path ─────────────────────────────────
[Benchmark(Baseline = true)]
public LambdaExpression? ResolveProperty_Registry()
=> _resolver.FindGeneratedExpression(_propertyMember);
[Benchmark]
public LambdaExpression? ResolveMethod_Registry()
=> _resolver.FindGeneratedExpression(_methodMember);
[Benchmark]
public LambdaExpression? ResolveMethodWithParam_Registry()
=> _resolver.FindGeneratedExpression(_methodWithParamMember);
[Benchmark]
public LambdaExpression? ResolveCopyConstructor_Registry()
=> _resolver.FindGeneratedExpression(_copyConstructorMember);
// ── Reflection path ───────────────────────────────────────────────────
[Benchmark]
public LambdaExpression? ResolveProperty_Reflection()
=> ProjectionExpressionResolver.FindGeneratedExpressionViaReflection(_propertyMember);
[Benchmark]
public LambdaExpression? ResolveMethod_Reflection()
=> ProjectionExpressionResolver.FindGeneratedExpressionViaReflection(_methodMember);
[Benchmark]
public LambdaExpression? ResolveMethodWithParam_Reflection()
=> ProjectionExpressionResolver.FindGeneratedExpressionViaReflection(_methodWithParamMember);
[Benchmark]
public LambdaExpression? ResolveCopyConstructor_Reflection()
=> ProjectionExpressionResolver.FindGeneratedExpressionViaReflection(_copyConstructorMember);
}
}