This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ActionMeasurement.cs
95 lines (79 loc) · 2.57 KB
/
ActionMeasurement.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
using System;
using System.Collections.Generic;
using Unity.PerformanceTesting;
namespace Improbable.Gdk.TestUtils
{
public class ActionMeasurement
{
private readonly Action action;
private readonly List<SampleGroup> sampleGroups = new List<SampleGroup>();
private int warmupCount = 5;
private int measurementCount = 10;
private int iterationsPerMeasurement = 1;
private ActionMeasurement(Action action)
{
this.action = action;
}
public static ActionMeasurement Measure(Action action)
{
return new ActionMeasurement(action);
}
public ActionMeasurement ProfilerMarkers(IEnumerable<string> profilerMarkers)
{
foreach (var marker in profilerMarkers)
{
var sampleGroup = new SampleGroup(marker, Unity.PerformanceTesting.SampleUnit.Nanosecond);
sampleGroup.GetRecorder().enabled = false;
sampleGroups.Add(sampleGroup);
}
return this;
}
public ActionMeasurement WarmupCount(int warmupCount)
{
this.warmupCount = warmupCount;
return this;
}
public ActionMeasurement MeasurementCount(int measurementCount)
{
this.measurementCount = measurementCount;
return this;
}
public ActionMeasurement IterationsPerMeasurement(int iterationsPerMeasurement)
{
this.iterationsPerMeasurement = iterationsPerMeasurement;
return this;
}
private void StartMeasurements()
{
foreach (var sampleGroup in sampleGroups)
{
sampleGroup.GetRecorder().enabled = true;
}
}
private void EndMeasurements()
{
foreach (var sampleGroup in sampleGroups)
{
var recorder = sampleGroup.GetRecorder();
recorder.enabled = false;
Unity.PerformanceTesting.Measure.Custom(sampleGroup, (double) recorder.elapsedNanoseconds / recorder.sampleBlockCount);
}
}
public void Run()
{
for (var i = 0; i < warmupCount; i++)
{
action();
}
for (var i = 0; i < measurementCount; i++)
{
StartMeasurements();
for (var j = 0; j < iterationsPerMeasurement; j++)
{
action();
}
EndMeasurements();
}
}
}
}