-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizedReflectionObjectMapperBenchMark.cs
118 lines (99 loc) · 3.36 KB
/
OptimizedReflectionObjectMapperBenchMark.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using Moq;
using ObjectMapperBenchMark.Extensions;
namespace ObjectMapperBenchMark.Mappers
{
public class OptimizedReflectionObjectMapperBenchMark
{
private Mock<IDataReader> reader;
private Consumer consumer;
[Params(10, 100, 10000)]
public int Rows { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
this.reader = new Mock<IDataReader>();
this.consumer = new Consumer();
this.reader.Setup();
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.reader = null;
}
[IterationSetup]
public void IterationSetup()
{
this.reader.SetupRows(this.Rows);
}
[Benchmark]
public Model As()
{
var result = this.AsOptimizedReflection<Model>(this.reader.Object);
return result;
}
[Benchmark]
public void AsArray()
{
var result = this.AsArrayOptimizedReflection<Model>(this.reader.Object);
result.Consume(this.consumer);
}
private T AsOptimizedReflection<T>(IDataReader reader) where T : class, new()
{
var properties = this.CacheProperties<T>(reader);
var result = this.MapToProperties(properties, reader);
return result;
}
private IEnumerable<T> AsArrayOptimizedReflection<T>(IDataReader reader) where T : class, new()
{
var results = new List<T>(reader.FieldCount);
var properties = this.CacheProperties<T>(reader);
while (reader.Read())
{
results.Add(this.MapToProperties(properties, reader));
}
return results;
}
private Dictionary<string, Action<T, object>> CacheProperties<T>(IDataReader reader) where T : class, new()
{
var properties = new Dictionary<string, Action<T, object>>();
for (int i = 0; i < reader.FieldCount; i++)
{
var fieldName = reader.GetName(i);
var methodInfo = typeof(T).GetProperty(reader.GetName(i))?.GetSetMethod();
if (methodInfo != null)
{
var setter = ReflectionOptimizer.CreateDowncastDelegate<T>(methodInfo);
properties.Add(fieldName, setter);
}
}
return properties;
}
private T MapToProperties<T>(Dictionary<string, Action<T, object>> properties, IDataReader reader) where T : class, new()
{
var result = new T();
foreach (var property in properties)
{
var value = reader[property.Key];
if (value == DBNull.Value)
{
value = null;
}
property.Value(result, value);
}
return result;
}
internal static class ReflectionOptimizer
{
public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method)
{
return (model, value) => method.Invoke(model, new object[] { value });
}
}
}
}