using System;
using System.Diagnostics;
using Unity.PerformanceTesting.Exceptions;
using Unity.PerformanceTesting.Measurements;
using Unity.PerformanceTesting.Runtime;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Unity.PerformanceTesting
{
///
/// Enables measuring of performance metrics during a performance test.
///
public static class Measure
{
///
/// Saves provided value as a performance measurement.
///
/// The sample group to save the value to.
/// Value to be saved.
public static void Custom(SampleGroup sampleGroup, double value)
{
VerifyValue(sampleGroup.Name, value);
var activeSampleGroup = PerformanceTest.GetSampleGroup(sampleGroup.Name);
if (activeSampleGroup == null)
{
PerformanceTest.AddSampleGroup(sampleGroup);
activeSampleGroup = sampleGroup;
}
activeSampleGroup.Samples.Add(value);
}
///
/// Saves provided value as a performance measurement.
///
/// The name of the sample group to save the value to.
/// Value to be saved.
public static void Custom(string name, double value)
{
VerifyValue(name, value);
var activeSampleGroup = PerformanceTest.GetSampleGroup(name);
if (activeSampleGroup == null)
{
activeSampleGroup = new SampleGroup(name);
PerformanceTest.AddSampleGroup(activeSampleGroup);
}
activeSampleGroup.Samples.Add(value);
}
static void VerifyValue(string name, double value)
{
if (double.IsNaN(value))
throw new PerformanceTestException(
$"Trying to record value which is not a number for sample group: {name}");
}
///
/// Measures execution time for the given scope as a single time.
///
/// Name to use for the sample group.
/// IDisposable on which you should call the method to stop measurement.
public static ScopeMeasurement Scope(string name = "Time")
{
return new ScopeMeasurement(name);
}
///
/// Measures execution time for the given scope as a single time.
///
/// Sample group to use to save samples to.
/// IDisposable on which you should call the method to stop measurement.
public static ScopeMeasurement Scope(SampleGroup sampleGroup)
{
return new ScopeMeasurement(sampleGroup);
}
///
/// Measures profiler markers for the given scope.
///
/// List of profiler marker names.
///
public static ProfilerMeasurement ProfilerMarkers(params string[] profilerMarkerLabels)
{
return new ProfilerMeasurement(profilerMarkerLabels);
}
///
/// Measures profiler markers for the given scope.
///
/// List of SampleGroups where the name matches the profiler marker to measure.
///
public static ProfilerMeasurement ProfilerMarkers(params SampleGroup[] sampleGroups)
{
return new ProfilerMeasurement(sampleGroups);
}
///
/// Measures execution time for a method with given parameters.
///
///
/// using a builder pattern to provide parameters. Call to start measurement.
public static MethodMeasurement Method(Action action)
{
return new MethodMeasurement(action);
}
///
/// Measures frame times with given parameters.
///
/// using a builder pattern to provide parameters. Call method to start measurement.
public static FramesMeasurement Frames()
{
return new FramesMeasurement();
}
}
}