using System;
using Unity.PerformanceTesting.Runtime;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Unity.PerformanceTesting.Measurements
{
///
/// Provides a way to measure profiler markers.
///
public struct ProfilerMeasurement : IDisposable
{
private readonly ProfilerMarkerMeasurement m_Test;
///
/// Initializes a profiler marker measurement.
///
/// List of sample groups with name set to match profiler markers to be measured.
public ProfilerMeasurement(SampleGroup[] sampleGroups)
{
if (sampleGroups == null)
{
m_Test = null;
return;
}
if (sampleGroups.Length == 0)
{
m_Test = null;
return;
}
var go = new GameObject("Recorder");
if (Application.isPlaying) Object.DontDestroyOnLoad(go);
go.hideFlags = HideFlags.HideAndDontSave;
m_Test = go.AddComponent();
m_Test.AddProfilerSampleGroup(sampleGroups);
PerformanceTest.Disposables.Add(this);
}
///
/// Initializes a profiler marker measurement.
///
/// List of profiler markers to be measured.
public ProfilerMeasurement(string[] profilerMarkers): this(Utils.CreateSampleGroupsFromMarkerNames(profilerMarkers))
{
}
///
/// Stops profiler marker measurement and adds them to the provided sample group.
///
public void Dispose()
{
PerformanceTest.Disposables.Remove(this);
if (m_Test == null) return;
m_Test.StopAndSampleRecorders();
Object.DestroyImmediate(m_Test.gameObject);
}
}
}