-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (92 loc) · 2.34 KB
/
Program.cs
File metadata and controls
106 lines (92 loc) · 2.34 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
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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Dapper;
using System.Data;
using System.Reflection;
namespace MapDataReader.Benchmarks
{
internal class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Benchy>();
}
}
[ShortRunJob, MemoryDiagnoser]
public class Benchy
{
static TestClass _o = new TestClass();
static PropertyInfo _prop = _o.GetType().GetProperty("String1", BindingFlags.Public | BindingFlags.Instance);
static PropertyInfo _nullableprop = _o.GetType().GetProperty("IntNullable", BindingFlags.Public | BindingFlags.Instance);
[Benchmark]
public void SetProp_Reflection()
{
PropertyInfo prop = _o.GetType().GetProperty("String1", BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
prop.SetValue(_o, "Value");
}
}
[Benchmark]
public void SetProp_ReflectionCached()
{
_prop.SetValue(_o, "Value");
}
[Benchmark]
public void SetProp_MapDataReader()
{
_o.SetPropertyByName("String1", "Value");
}
[Benchmark]
public void SetNullableProp_ReflectionCached()
{
_nullableprop.SetValue(_o, 123);
}
[Benchmark]
public void SetNullableProp_MapDataReader()
{
_o.SetPropertyByName("IntNullable", 123);
}
[Benchmark]
public void MapDatareader_ViaDapper()
{
var dr = _dt.CreateDataReader();
var list = dr.Parse<TestClass>().ToList();
}
[Benchmark]
public void MapDataReader_ViaMapaDataReader()
{
var dr = _dt.CreateDataReader();
var list = dr.To<TestClass>();
}
static DataTable _dt;
[GlobalSetup]
public static void Setup()
{
//create datatable with test data
_dt = new DataTable();
_dt.Columns.AddRange(new[] {
new DataColumn("String1", typeof(string)),
new DataColumn("String2", typeof(string)),
new DataColumn("String3", typeof(string)),
new DataColumn("Int", typeof(int)),
new DataColumn("Int2", typeof(int)),
new DataColumn("IntNullable", typeof(int))
});
for (int i = 0; i < 1000; i++)
{
_dt.Rows.Add("xxx", "yyy", "zzz", 123, 321, 3211);
}
}
}
[GenerateDataReaderMapper]
public class TestClass
{
public string String1 { get; set; }
public string String2 { get; set; }
public string String3 { get; set; }
public string Int { get; set; }
public string Int2 { get; set; }
public int? IntNullable { get; set; }
}
}