forked from khalidsalomao/SimpleHelpers.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmark.cs
94 lines (80 loc) · 2.6 KB
/
Benchmark.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
using System;
using System.Diagnostics;
using PerformanceTest.Logging;
namespace PerformanceTest
{
public class Benchmark: IDisposable
{
static ILog logger = LogProvider.For<Benchmark> ();
Stopwatch _stopwatch;
public string Name { get; set; }
public TimeSpan Elapsed
{
get { return _stopwatch.Elapsed; }
}
public Benchmark (string opName)
{
Name = opName;
_stopwatch = new Stopwatch ();
}
public void Dispose ()
{
_stopwatch.Stop ();
logger.Info (ToString ());
}
public override string ToString ()
{
return String.Format (String.Format ("Timing for {0}:\t {1}", Name, _stopwatch.Elapsed));
}
public Benchmark Start (bool skipWarmupGC = false)
{
if (!skipWarmupGC)
PrepareSystemForBenchmark ();
_stopwatch.Start ();
return this;
}
public TimeSpan Stop()
{
_stopwatch.Stop ();
return _stopwatch.Elapsed;
}
/// <summary>
/// Prepares the system for benchmark will force Garbage Collection to run
/// with maximum generation and wait it to finish.
/// </summary>
public static void PrepareSystemForBenchmark ()
{
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers ();
GC.Collect ();
System.Threading.Thread.Sleep (0);
}
public static Benchmark Start (string opName)
{
return new Benchmark (opName).Start();
}
public static Benchmark Start (string opName, params object[] arguments)
{
return new Benchmark (String.Format (opName, arguments)).Start ();
}
public static TimeSpan Time (string opName, Action action)
{
var mark = new Benchmark (opName).Start ();
action ();
mark.Stop ();
logger.Info (mark.ToString ());
return mark.Elapsed;
}
public static TimeSpan Time (string opName, Action action, int loopCount, bool warmup)
{
if (warmup)
action ();
var mark = new Benchmark (opName).Start ();
for (var i = 0; i < loopCount; i++)
action ();
mark.Stop ();
logger.Info (String.Format ("Timing for {0} run {1} times:\t {1}", opName, loopCount, mark.Elapsed));
return mark.Elapsed;
}
}
}