No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScopeMeasurement.cs 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Diagnostics;
  3. using Unity.PerformanceTesting.Runtime;
  4. namespace Unity.PerformanceTesting.Measurements
  5. {
  6. /// <summary>
  7. /// Measures execution time for the given scope as a single time.
  8. /// </summary>
  9. public struct ScopeMeasurement : IDisposable
  10. {
  11. private readonly SampleGroup m_SampleGroup;
  12. private readonly long m_StartTicks;
  13. /// <summary>
  14. /// Initializes a scope measurement.
  15. /// </summary>
  16. /// <param name="sampleGroup">Sample group used to save measurements.</param>
  17. public ScopeMeasurement(SampleGroup sampleGroup)
  18. {
  19. m_SampleGroup = PerformanceTest.GetSampleGroup(sampleGroup.Name);
  20. if (m_SampleGroup == null)
  21. {
  22. m_SampleGroup = sampleGroup;
  23. PerformanceTest.Active.SampleGroups.Add(m_SampleGroup);
  24. }
  25. m_StartTicks = Stopwatch.GetTimestamp();
  26. PerformanceTest.Disposables.Add(this);
  27. }
  28. /// <summary>
  29. /// Initializes a scope measurement.
  30. /// </summary>
  31. /// <param name="name">Sample group name used for measurements.</param>
  32. public ScopeMeasurement(string name) : this(new SampleGroup(name))
  33. {
  34. }
  35. /// <summary>
  36. /// Stops scope measurement and adds it to provided sample group.
  37. /// </summary>
  38. public void Dispose()
  39. {
  40. var elapsedTicks = Stopwatch.GetTimestamp() - m_StartTicks;
  41. PerformanceTest.Disposables.Remove(this);
  42. var delta = TimeSpan.FromTicks(elapsedTicks).TotalMilliseconds;
  43. delta = Utils.ConvertSample(SampleUnit.Millisecond, m_SampleGroup.Unit, delta);
  44. Measure.Custom(m_SampleGroup, delta);
  45. }
  46. }
  47. }