-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapPerfomanceTests.cs
74 lines (64 loc) · 2.17 KB
/
MapPerfomanceTests.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
using System;
using System.Diagnostics;
using Photosphere.Mapping.Static;
using Photosphere.Mapping.Tests.TestClasses;
using Xunit;
using Xunit.Abstractions;
namespace Photosphere.Mapping.Tests
{
public class MapPerfomanceTests
{
private readonly ITestOutputHelper _output;
public MapPerfomanceTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
internal void MeasurePerfomanceAndCompareWithNativeMapping()
{
PerformMeasuring(1000);
PerformMeasuring(2000);
PerformMeasuring(3000);
PerformMeasuring(4000);
PerformMeasuring(5000);
PerformMeasuring(6000);
PerformMeasuring(7000);
PerformMeasuring(8000);
PerformMeasuring(9000);
PerformMeasuring(10000);
}
private void PerformMeasuring(int timesOfMeasure)
{
var result = new MeasureResult
{
Mapping = RepeatAndAccumulateMilliseconds(timesOfMeasure, StaticMapper<LargeFoo, LargeFoo>.Map),
Native = RepeatAndAccumulateMilliseconds(timesOfMeasure, LargeFoo.NativeMap2)
};
_output.WriteLine($"Amount of mapping iterations: {timesOfMeasure}\n" + result);
}
private static long RepeatAndAccumulateMilliseconds(int timesToRepeat, Action<LargeFoo, LargeFoo> action)
{
var index = 0;
var stopWatch = new Stopwatch();
while (index < timesToRepeat)
{
var source = LargeFoo.GetRandomNew();
var target = LargeFoo.GetRandomNew();
stopWatch.Start();
action(source, target);
stopWatch.Stop();
index++;
}
return stopWatch.ElapsedMilliseconds;
}
private class MeasureResult
{
public long Mapping { get; set; }
public long Native { get; set; }
public override string ToString()
{
return $"\n{nameof(Mapping)}:\t{Mapping} ms\n{nameof(Native)}:\t\t{Native} ms\n";
}
}
}
}