using System; using System.Diagnostics; using Unity.PerformanceTesting.Runtime; namespace Unity.PerformanceTesting.Measurements { /// /// Measures execution time for the given scope as a single time. /// public struct ScopeMeasurement : IDisposable { private readonly SampleGroup m_SampleGroup; private readonly long m_StartTicks; /// /// Initializes a scope measurement. /// /// Sample group used to save measurements. public ScopeMeasurement(SampleGroup sampleGroup) { m_SampleGroup = PerformanceTest.GetSampleGroup(sampleGroup.Name); if (m_SampleGroup == null) { m_SampleGroup = sampleGroup; PerformanceTest.Active.SampleGroups.Add(m_SampleGroup); } m_StartTicks = Stopwatch.GetTimestamp(); PerformanceTest.Disposables.Add(this); } /// /// Initializes a scope measurement. /// /// Sample group name used for measurements. public ScopeMeasurement(string name) : this(new SampleGroup(name)) { } /// /// Stops scope measurement and adds it to provided sample group. /// public void Dispose() { var elapsedTicks = Stopwatch.GetTimestamp() - m_StartTicks; PerformanceTest.Disposables.Remove(this); var delta = TimeSpan.FromTicks(elapsedTicks).TotalMilliseconds; delta = Utils.ConvertSample(SampleUnit.Millisecond, m_SampleGroup.Unit, delta); Measure.Custom(m_SampleGroup, delta); } } }