-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark.linq
106 lines (86 loc) · 2.57 KB
/
Benchmark.linq
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
<Query Kind="Program">
<Reference Relative="MAB.SimpleMapper\bin\Debug\MAB.SimpleMapper.dll">C:\Src\MAB.SimpleMapper\MAB.SimpleMapper\bin\Debug\MAB.SimpleMapper.dll</Reference>
<Namespace>MAB.SimpleMapper</Namespace>
</Query>
void Main()
{
// Cha
Mapper.ClearMappings();
var entities = GetEntities(10000);
var results = new List<Model>();
var result1 = Benchmark.Perform(() => {
foreach (var entity in entities)
{
var model = entity.MapToConstructorCreateInstance<Model>();
results.Add(model);
}
});
Mapper.ClearMappings();
var results2 = new List<Model>();
var result2 = Benchmark.Perform(() => {
foreach (var entity in entities)
{
var model = entity.MapToConstructorCustomDelegate<Model>();
results2.Add(model);
}
});
result1.Dump("MapToConstructorCreateInstance");
results.Take(10).Dump();
result2.Dump("MapToConstructorCustomDelegate");
results.Take(10).Dump();
}
public IEnumerable<Entity> GetEntities(int count)
{
var rnd = new Random();
for (var i = 0; i < count; i++)
{
var email = new String(Enumerable.Range(1, 5).Select(x => (char)rnd.Next(66, 91)).ToArray())
+ "@" + new String(Enumerable.Range(1, 10).Select(x => (char)rnd.Next(66, 91)).ToArray())
+ "." + new String(Enumerable.Range(1, 3).Select(x => (char)rnd.Next(66, 91)).ToArray());
yield return new Entity
{
ID = rnd.Next(1, 500),
Email = email.ToLower(),
Created = DateTime.Now.AddDays((rnd.Next(1, 10000) * -1)),
Active = (rnd.Next(1, 100) > 50)
};
}
}
public class Entity
{
public int ID { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
public bool Active { get; set; }
}
public class Model
{
public int ID { get; private set; }
public string Email { get; private set; }
public DateTime Created { get; private set; }
public bool Active { get; private set; }
public Model(int id, string email, DateTime created, bool active)
{
ID = id;
Email = email;
Created = created;
Active = active;
}
}
public class Benchmark
{
public static long Perform(Action action)
{
return Benchmark.Perform(action, 1);
}
public static long Perform(Action action, int iterations)
{
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
action();
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
}