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.

ProfilerMeasurement.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using Unity.PerformanceTesting.Runtime;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace Unity.PerformanceTesting.Measurements
  6. {
  7. /// <summary>
  8. /// Provides a way to measure profiler markers.
  9. /// </summary>
  10. public struct ProfilerMeasurement : IDisposable
  11. {
  12. private readonly ProfilerMarkerMeasurement m_Test;
  13. /// <summary>
  14. /// Initializes a profiler marker measurement.
  15. /// </summary>
  16. /// <param name="sampleGroups">List of sample groups with name set to match profiler markers to be measured.</param>
  17. public ProfilerMeasurement(SampleGroup[] sampleGroups)
  18. {
  19. if (sampleGroups == null)
  20. {
  21. m_Test = null;
  22. return;
  23. }
  24. if (sampleGroups.Length == 0)
  25. {
  26. m_Test = null;
  27. return;
  28. }
  29. var go = new GameObject("Recorder");
  30. if (Application.isPlaying) Object.DontDestroyOnLoad(go);
  31. go.hideFlags = HideFlags.HideAndDontSave;
  32. m_Test = go.AddComponent<ProfilerMarkerMeasurement>();
  33. m_Test.AddProfilerSampleGroup(sampleGroups);
  34. PerformanceTest.Disposables.Add(this);
  35. }
  36. /// <summary>
  37. /// Initializes a profiler marker measurement.
  38. /// </summary>
  39. /// <param name="profilerMarkers">List of profiler markers to be measured.</param>
  40. public ProfilerMeasurement(string[] profilerMarkers): this(Utils.CreateSampleGroupsFromMarkerNames(profilerMarkers))
  41. {
  42. }
  43. /// <summary>
  44. /// Stops profiler marker measurement and adds them to the provided sample group.
  45. /// </summary>
  46. public void Dispose()
  47. {
  48. PerformanceTest.Disposables.Remove(this);
  49. if (m_Test == null) return;
  50. m_Test.StopAndSampleRecorders();
  51. Object.DestroyImmediate(m_Test.gameObject);
  52. }
  53. }
  54. }