A game about forced loneliness, made by TACStudios
1using System;
2using System.Diagnostics;
3using Unity.PerformanceTesting.Exceptions;
4using Unity.PerformanceTesting.Measurements;
5using Unity.PerformanceTesting.Runtime;
6using UnityEngine;
7using Object = UnityEngine.Object;
8
9namespace Unity.PerformanceTesting
10{
11 /// <summary>
12 /// Enables measuring of performance metrics during a performance test.
13 /// </summary>
14 public static class Measure
15 {
16 /// <summary>
17 /// Saves provided value as a performance measurement.
18 /// </summary>
19 /// <param name="sampleGroup">The sample group to save the value to.</param>
20 /// <param name="value">Value to be saved.</param>
21 public static void Custom(SampleGroup sampleGroup, double value)
22 {
23 VerifyValue(sampleGroup.Name, value);
24
25 var activeSampleGroup = PerformanceTest.GetSampleGroup(sampleGroup.Name);
26 if (activeSampleGroup == null)
27 {
28 PerformanceTest.AddSampleGroup(sampleGroup);
29 activeSampleGroup = sampleGroup;
30 }
31
32 activeSampleGroup.Samples.Add(value);
33 }
34
35 /// <summary>
36 /// Saves provided value as a performance measurement.
37 /// </summary>
38 /// <param name="name">The name of the sample group to save the value to.</param>
39 /// <param name="value">Value to be saved.</param>
40 public static void Custom(string name, double value)
41 {
42 VerifyValue(name, value);
43
44 var activeSampleGroup = PerformanceTest.GetSampleGroup(name);
45 if (activeSampleGroup == null)
46 {
47 activeSampleGroup = new SampleGroup(name);
48 PerformanceTest.AddSampleGroup(activeSampleGroup);
49 }
50
51 activeSampleGroup.Samples.Add(value);
52 }
53
54 static void VerifyValue(string name, double value)
55 {
56 if (double.IsNaN(value))
57 throw new PerformanceTestException(
58 $"Trying to record value which is not a number for sample group: {name}");
59 }
60
61 /// <summary>
62 /// Measures execution time for the given scope as a single time.
63 /// </summary>
64 /// <param name="name">Name to use for the sample group.</param>
65 /// <returns>IDisposable <see cref="ScopeMeasurement"/> on which you should call the <see cref="ScopeMeasurement.Dispose"/> method to stop measurement.</returns>
66 public static ScopeMeasurement Scope(string name = "Time")
67 {
68 return new ScopeMeasurement(name);
69 }
70
71 /// <summary>
72 /// Measures execution time for the given scope as a single time.
73 /// </summary>
74 /// <param name="sampleGroup">Sample group to use to save samples to.</param>
75 /// <returns>IDisposable <see cref="ScopeMeasurement"/> on which you should call the <see cref="ScopeMeasurement.Dispose"/> method to stop measurement.</returns>
76 public static ScopeMeasurement Scope(SampleGroup sampleGroup)
77 {
78 return new ScopeMeasurement(sampleGroup);
79 }
80
81 /// <summary>
82 /// Measures profiler markers for the given scope.
83 /// </summary>
84 /// <param name="profilerMarkerLabels">List of profiler marker names.</param>
85 /// <returns></returns>
86 public static ProfilerMeasurement ProfilerMarkers(params string[] profilerMarkerLabels)
87 {
88 return new ProfilerMeasurement(profilerMarkerLabels);
89 }
90
91 /// <summary>
92 /// Measures profiler markers for the given scope.
93 /// </summary>
94 /// <param name="sampleGroups">List of SampleGroups where the name matches the profiler marker to measure.</param>
95 /// <returns></returns>
96 public static ProfilerMeasurement ProfilerMarkers(params SampleGroup[] sampleGroups)
97 {
98 return new ProfilerMeasurement(sampleGroups);
99 }
100
101 /// <summary>
102 /// Measures execution time for a method with given parameters.
103 /// </summary>
104 /// <param name="action"></param>
105 /// <returns><see cref="MethodMeasurement"/>using a builder pattern to provide parameters. Call <see cref="ScopeMeasurement.Run"/> to start measurement.</returns>
106 public static MethodMeasurement Method(Action action)
107 {
108 return new MethodMeasurement(action);
109 }
110
111 /// <summary>
112 /// Measures frame times with given parameters.
113 /// </summary>
114 /// <returns><see cref="FramesMeasurement"/> using a builder pattern to provide parameters. Call <see cref="FramesMeasurement.Run"/> method to start measurement.</returns>
115 public static FramesMeasurement Frames()
116 {
117 return new FramesMeasurement();
118 }
119 }
120}