Skip to content

Commit 290a53c

Browse files
author
Unity Technologies
committed
com.unity.test-framework.performance@0.1.36-preview
## [0.1.36] - 2018-8-27 ### ProfilerMarkers now take params as arguments ## [0.1.35] - 2018-8-27 ### Measure.Method improvements Add GC allocation to Measure.Method Add setup/cleanup for Measure.Method Move order of calls for Measure.Scope ## [0.1.34] - 2018-8-16 ### Obsolete warnings
1 parent c5240aa commit 290a53c

File tree

7 files changed

+101
-27
lines changed

7 files changed

+101
-27
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## [0.1.36] - 2018-8-27
4+
5+
### ProfilerMarkers now take params as arguments
6+
7+
## [0.1.35] - 2018-8-27
8+
9+
### Measure.Method improvements
10+
11+
Add GC allocation to Measure.Method
12+
Add setup/cleanup for Measure.Method
13+
Move order of calls for Measure.Scope
14+
315
## [0.1.34] - 2018-8-16
416

517
### Obsolete warnings

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ YourProject/Packages/manifest.json
1111
``` json
1212
{
1313
"dependencies": {
14-
"com.unity.test-framework.performance": "0.1.34-preview",
14+
"com.unity.test-framework.performance": "0.1.36-preview",
1515
"com.unity.modules.jsonserialize": "1.0.0",
16-
"com.unity.modules.unitywebrequestwww": "1.0.0",
16+
"com.unity.modules.unitywebrequest": "1.0.0",
1717
"com.unity.modules.vr": "1.0.0"
1818
},
1919
"testables": [
20-
"com.unity.test-framework.performance"
20+
"com.unity.test-framework.performance"
2121
],
2222
"registry": "https://staging-packages.unity.com"
2323
}
@@ -97,6 +97,7 @@ In cases where you feel the default values are not ideal you can specify custom
9797
WarmupCount - how many iterations to run without measuring for warmup
9898
MeasurementCount - how many measurements to take
9999
IterationsPerMeasurement - how many iterations per measurement to take
100+
GC - measures the amount of GC allocations
100101

101102
```
102103
[PerformanceTest]
@@ -106,6 +107,7 @@ public void Test()
106107
.WarmupCount(10)
107108
.MeasurementCount(10)
108109
.IterationsPerMeasurement(5)
110+
.GC()
109111
.Run();
110112
}
111113
```

Runtime/Measure.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static ScopeMeasurement Scope(SampleGroupDefinition sampleGroupDefinition
2626
return new ScopeMeasurement(sampleGroupDefinition);
2727
}
2828

29-
public static ProfilerMeasurement ProfilerMarkers(SampleGroupDefinition[] sampleGroupDefinitions)
29+
public static ProfilerMeasurement ProfilerMarkers(params SampleGroupDefinition[] sampleGroupDefinitions)
3030
{
3131
return new ProfilerMeasurement(sampleGroupDefinitions);
3232
}
@@ -65,8 +65,8 @@ public ScopeMeasurement(SampleGroupDefinition sampleGroupDefinition)
6565

6666
public void Dispose()
6767
{
68-
PerformanceTest.Disposables.Remove(this);
6968
var elapsedTicks = Stopwatch.GetTimestamp() - m_StartTicks;
69+
PerformanceTest.Disposables.Remove(this);
7070
var delta = TimeSpan.FromTicks(elapsedTicks).TotalMilliseconds;
7171

7272
Measure.Custom(m_TimeSampleGroup.Definition,

Runtime/Measurements/FramesMeasurement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class FramesMeasurement
1919
private int m_Warmup = -1;
2020
private bool m_RecordFrametime = true;
2121

22-
public FramesMeasurement ProfilerMarkers(SampleGroupDefinition[] profilerDefinitions)
22+
public FramesMeasurement ProfilerMarkers(params SampleGroupDefinition[] profilerDefinitions)
2323
{
2424
m_ProfilerDefinitions = profilerDefinitions;
2525
return this;

Runtime/Measurements/MethodMeasurement.cs

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using Unity.PerformanceTesting.Runtime;
44
using UnityEngine;
5+
using UnityEngine.Profiling;
56

67

78
namespace Unity.PerformanceTesting.Measurements
@@ -15,25 +16,30 @@ public class MethodMeasurement
1516
private const int k_MaxIterations = 10000;
1617
private readonly Action m_Action;
1718
private readonly List<SampleGroup> m_SampleGroups = new List<SampleGroup>();
19+
private readonly Recorder m_GCRecorder;
1820

21+
private Action m_Setup;
22+
private Action m_Cleanup;
1923
private SampleGroupDefinition m_Definition;
2024
private int m_WarmupCount;
2125
private int m_MeasurementCount;
2226
private int m_IterationCount = 1;
27+
private bool m_GC;
2328

2429
public MethodMeasurement(Action action)
2530
{
2631
m_Action = action;
32+
m_GCRecorder = Recorder.Get("GC.Alloc");
2733
}
2834

29-
public MethodMeasurement ProfilerMarkers(SampleGroupDefinition[] profilerDefinitions)
35+
public MethodMeasurement ProfilerMarkers(params SampleGroupDefinition[] profilerDefinitions)
3036
{
3137
if (profilerDefinitions == null) return this;
3238
AddProfilerMarkers(profilerDefinitions);
3339
return this;
3440
}
3541

36-
private void AddProfilerMarkers(SampleGroupDefinition[] samplesGroup)
42+
private void AddProfilerMarkers(params SampleGroupDefinition[] samplesGroup)
3743
{
3844
foreach (var sample in samplesGroup)
3945
{
@@ -82,7 +88,25 @@ public MethodMeasurement MeasurementCount(int count)
8288
m_MeasurementCount = count;
8389
return this;
8490
}
85-
91+
92+
public MethodMeasurement CleanUp(Action action)
93+
{
94+
m_Cleanup = action;
95+
return this;
96+
}
97+
98+
public MethodMeasurement SetUp(Action action)
99+
{
100+
m_Setup = action;
101+
return this;
102+
}
103+
104+
public MethodMeasurement GC()
105+
{
106+
m_GC = true;
107+
return this;
108+
}
109+
86110
public void Run()
87111
{
88112
if (m_MeasurementCount > 0)
@@ -91,7 +115,7 @@ public void Run()
91115
RunForIterations(m_IterationCount, m_MeasurementCount);
92116
return;
93117
}
94-
118+
95119
var iterations = Probing();
96120
RunForIterations(iterations, k_MeasurementCount);
97121
}
@@ -101,15 +125,15 @@ private void RunForIterations(int iterations, int measurements)
101125
UpdateSampleGroupDefinition();
102126

103127
EnableMarkers();
104-
for (int j = 0; j < measurements; j++)
128+
for (var j = 0; j < measurements; j++)
105129
{
106130
var executionTime = Time.realtimeSinceStartup;
107-
for (var i = 0; i < iterations; i++)
108-
{
109-
m_Action.Invoke();
110-
}
131+
132+
ExecuteForIterations(iterations);
133+
111134
executionTime = (Time.realtimeSinceStartup - executionTime) * 1000f / iterations;
112-
Measure.Custom(m_Definition, Utils.ConvertSample(SampleUnit.Millisecond, m_Definition.SampleUnit, executionTime));
135+
Measure.Custom(m_Definition,
136+
Utils.ConvertSample(SampleUnit.Millisecond, m_Definition.SampleUnit, executionTime));
113137
}
114138

115139
DisableAndMeasureMarkers();
@@ -155,8 +179,8 @@ private int Probing()
155179

156180
if (iterations == 1)
157181
{
158-
m_Action.Invoke();
159-
m_Action.Invoke();
182+
ExecuteAction();
183+
ExecuteAction();
160184

161185
return 1;
162186
}
@@ -171,7 +195,7 @@ private void Warmup(int iterations)
171195
{
172196
for (var i = 0; i < iterations; i++)
173197
{
174-
m_Action.Invoke();
198+
ExecuteAction();
175199
}
176200
}
177201

@@ -182,5 +206,38 @@ private void UpdateSampleGroupDefinition()
182206
m_Definition = new SampleGroupDefinition("Time");
183207
}
184208
}
209+
210+
private void ExecuteAction()
211+
{
212+
if (m_Setup != null) m_Setup.Invoke();
213+
m_Action.Invoke();
214+
if (m_Cleanup != null) m_Cleanup.Invoke();
215+
}
216+
217+
private void ExecuteForIterations(int iterations)
218+
{
219+
if (m_GC) StartGCRecorder();
220+
for (var i = 0; i < iterations; i++)
221+
{
222+
ExecuteAction();
223+
}
224+
if (m_GC) EndGCRecorderAndMeasure(iterations);
225+
}
226+
227+
private void StartGCRecorder()
228+
{
229+
System.GC.Collect();
230+
231+
m_GCRecorder.enabled = false;
232+
m_GCRecorder.enabled = true;
233+
}
234+
235+
private void EndGCRecorderAndMeasure(int iterations)
236+
{
237+
m_GCRecorder.enabled = false;
238+
239+
var definiton = new SampleGroupDefinition(m_Definition.Name + ".GC()", SampleUnit.None);
240+
Measure.Custom(definiton, m_GCRecorder.sampleBlockCount / iterations);
241+
}
185242
}
186243
}

Runtime/PerformanceTest.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PerformanceTest
2323
public List<SampleGroup> SampleGroups = new List<SampleGroup>();
2424

2525
public static PerformanceTest Active { get; private set; }
26-
internal static List<IDisposable> Disposables = new List<IDisposable>();
26+
internal static List<IDisposable> Disposables = new List<IDisposable>(1024);
2727

2828
public delegate void Callback();
2929

@@ -50,13 +50,13 @@ internal static void StartTest(ITest currentTest)
5050
internal static void EndTest(Test test)
5151
{
5252
if (test.IsSuite) return;
53-
if(test.FullName != Active.TestName) return;
53+
if (test.FullName != Active.TestName) return;
5454
DisposeMeasurements();
5555
Active.CalculateStatisticalValues();
5656
Active.EndTime = Utils.DateToInt(DateTime.Now);
5757
if (OnTestEnded != null) OnTestEnded();
5858
Active.LogOutput();
59-
59+
6060
TestContext.Out.WriteLine("##performancetestresult:" + JsonUtility.ToJson(Active));
6161
Active = null;
6262
}
@@ -67,6 +67,7 @@ private static void DisposeMeasurements()
6767
{
6868
Disposables[i].Dispose();
6969
}
70+
7071
Disposables.Clear();
7172
}
7273

@@ -99,7 +100,7 @@ public static void Compare(string oldGroup, string newGroup, float percentage)
99100
{
100101
throw new PerformanceTestException("At least on of the provided sample groups has no values.");
101102
}
102-
103+
103104
CalculateStatisticalValue(group);
104105
CalculateStatisticalValue(group2);
105106

@@ -115,8 +116,9 @@ public static void Compare(string oldGroup, string newGroup, float percentage)
115116
else if (group.Definition.IncreaseIsBetter || group2.Definition.IncreaseIsBetter)
116117
{
117118
throw new PerformanceTestException(
118-
string.Format("Sample groups {0} and {1} have incompatible definitions. When comparing, sample groups should have a matching SampleGroupDefinition.IncreaseIsBetter value.",
119-
group.Definition.Name, group2.Definition.Name));
119+
string.Format(
120+
"Sample groups {0} and {1} have incompatible definitions. When comparing, sample groups should have a matching SampleGroupDefinition.IncreaseIsBetter value.",
121+
group.Definition.Name, group2.Definition.Name));
120122
}
121123

122124
if (diff > percentage)
@@ -187,7 +189,8 @@ public override string ToString()
187189

188190
if (sampleGroup.Samples.Count == 1)
189191
{
190-
logString.AppendFormat(" {0:0.00} {1}", sampleGroup.Samples[0], sampleGroup.Definition.SampleUnit);
192+
logString.AppendFormat(" {0:0.00} {1}", sampleGroup.Samples[0],
193+
sampleGroup.Definition.SampleUnit);
191194
}
192195
else
193196
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.unity.test-framework.performance",
33
"displayName":"Performance testing API",
4-
"version": "0.1.34-preview",
4+
"version": "0.1.36-preview",
55
"unity": "2018.2",
66
"description": "Performance testing API.",
77
"keywords": ["performance", "test"],

0 commit comments

Comments
 (0)