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.

Measure.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Diagnostics;
  3. using Unity.PerformanceTesting.Exceptions;
  4. using Unity.PerformanceTesting.Measurements;
  5. using Unity.PerformanceTesting.Runtime;
  6. using UnityEngine;
  7. using Object = UnityEngine.Object;
  8. namespace Unity.PerformanceTesting
  9. {
  10. /// <summary>
  11. /// Enables measuring of performance metrics during a performance test.
  12. /// </summary>
  13. public static class Measure
  14. {
  15. /// <summary>
  16. /// Saves provided value as a performance measurement.
  17. /// </summary>
  18. /// <param name="sampleGroup">The sample group to save the value to.</param>
  19. /// <param name="value">Value to be saved.</param>
  20. public static void Custom(SampleGroup sampleGroup, double value)
  21. {
  22. VerifyValue(sampleGroup.Name, value);
  23. var activeSampleGroup = PerformanceTest.GetSampleGroup(sampleGroup.Name);
  24. if (activeSampleGroup == null)
  25. {
  26. PerformanceTest.AddSampleGroup(sampleGroup);
  27. activeSampleGroup = sampleGroup;
  28. }
  29. activeSampleGroup.Samples.Add(value);
  30. }
  31. /// <summary>
  32. /// Saves provided value as a performance measurement.
  33. /// </summary>
  34. /// <param name="name">The name of the sample group to save the value to.</param>
  35. /// <param name="value">Value to be saved.</param>
  36. public static void Custom(string name, double value)
  37. {
  38. VerifyValue(name, value);
  39. var activeSampleGroup = PerformanceTest.GetSampleGroup(name);
  40. if (activeSampleGroup == null)
  41. {
  42. activeSampleGroup = new SampleGroup(name);
  43. PerformanceTest.AddSampleGroup(activeSampleGroup);
  44. }
  45. activeSampleGroup.Samples.Add(value);
  46. }
  47. static void VerifyValue(string name, double value)
  48. {
  49. if (double.IsNaN(value))
  50. throw new PerformanceTestException(
  51. $"Trying to record value which is not a number for sample group: {name}");
  52. }
  53. /// <summary>
  54. /// Measures execution time for the given scope as a single time.
  55. /// </summary>
  56. /// <param name="name">Name to use for the sample group.</param>
  57. /// <returns>IDisposable <see cref="ScopeMeasurement"/> on which you should call the <see cref="ScopeMeasurement.Dispose"/> method to stop measurement.</returns>
  58. public static ScopeMeasurement Scope(string name = "Time")
  59. {
  60. return new ScopeMeasurement(name);
  61. }
  62. /// <summary>
  63. /// Measures execution time for the given scope as a single time.
  64. /// </summary>
  65. /// <param name="sampleGroup">Sample group to use to save samples to.</param>
  66. /// <returns>IDisposable <see cref="ScopeMeasurement"/> on which you should call the <see cref="ScopeMeasurement.Dispose"/> method to stop measurement.</returns>
  67. public static ScopeMeasurement Scope(SampleGroup sampleGroup)
  68. {
  69. return new ScopeMeasurement(sampleGroup);
  70. }
  71. /// <summary>
  72. /// Measures profiler markers for the given scope.
  73. /// </summary>
  74. /// <param name="profilerMarkerLabels">List of profiler marker names.</param>
  75. /// <returns></returns>
  76. public static ProfilerMeasurement ProfilerMarkers(params string[] profilerMarkerLabels)
  77. {
  78. return new ProfilerMeasurement(profilerMarkerLabels);
  79. }
  80. /// <summary>
  81. /// Measures profiler markers for the given scope.
  82. /// </summary>
  83. /// <param name="sampleGroups">List of SampleGroups where the name matches the profiler marker to measure.</param>
  84. /// <returns></returns>
  85. public static ProfilerMeasurement ProfilerMarkers(params SampleGroup[] sampleGroups)
  86. {
  87. return new ProfilerMeasurement(sampleGroups);
  88. }
  89. /// <summary>
  90. /// Measures execution time for a method with given parameters.
  91. /// </summary>
  92. /// <param name="action"></param>
  93. /// <returns><see cref="MethodMeasurement"/>using a builder pattern to provide parameters. Call <see cref="ScopeMeasurement.Run"/> to start measurement.</returns>
  94. public static MethodMeasurement Method(Action action)
  95. {
  96. return new MethodMeasurement(action);
  97. }
  98. /// <summary>
  99. /// Measures frame times with given parameters.
  100. /// </summary>
  101. /// <returns><see cref="FramesMeasurement"/> using a builder pattern to provide parameters. Call <see cref="FramesMeasurement.Run"/> method to start measurement.</returns>
  102. public static FramesMeasurement Frames()
  103. {
  104. return new FramesMeasurement();
  105. }
  106. }
  107. }