Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PerformanceStateTrackerUnitTests.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #if NUGET_MOQ_AVAILABLE
  2. using System;
  3. using Moq;
  4. using NUnit.Framework;
  5. using UnityEngine;
  6. using UnityEngine.AdaptivePerformance;
  7. using UnityEngine.AdaptivePerformance.Provider;
  8. using FrameTiming = UnityEngine.AdaptivePerformance.FrameTiming;
  9. namespace UnityEditor.AdaptivePerformance.Editor.Tests
  10. {
  11. public class PerformanceStateTrackerUnitTests
  12. {
  13. IAdaptivePerformance m_ap;
  14. IPerformanceStatus m_perfStat;
  15. FrameTiming ft;
  16. [SetUp]
  17. public void InitializeTest()
  18. {
  19. m_ap = Mock.Of<IAdaptivePerformance>();
  20. m_perfStat = Mock.Of<IPerformanceStatus>();
  21. ft = new FrameTiming();
  22. }
  23. [Test]
  24. public void StateAction_Stale_When_SampleCapacityZero()
  25. {
  26. BuildTestConditions(0f);
  27. Assert.AreEqual(StateAction.Stale, new MyPerformanceStateTracker(0).Update());
  28. }
  29. [TestCase((1f / 120), StateAction.Stale)]
  30. [TestCase((1f / 60), StateAction.Stale)]
  31. [TestCase((1f / 50), StateAction.Decrease)]
  32. [TestCase((1f / 45), StateAction.FastDecrease)]
  33. [TestCase((1f / 30), StateAction.FastDecrease)]
  34. [TestCase((1f / 15), StateAction.FastDecrease)]
  35. public void StateAction_When_AverageFrameTime(float averageFrameTime, StateAction stateAction)
  36. {
  37. BuildTestConditions(averageFrameTime);
  38. Assert.AreEqual(stateAction, new MyPerformanceStateTracker(100).Update());
  39. }
  40. [Test]
  41. public void CheckTrendValue_WithSingleUpdateCall_AndSampleCapacityIsZero()
  42. {
  43. BuildTestConditions(0f);
  44. PerformanceStateTracker testSubject = new MyPerformanceStateTracker(0);
  45. testSubject.Update();
  46. Assert.AreEqual(Double.NaN, testSubject.Trend);
  47. }
  48. [TestCase((1f / 120), 3, 5, -0.5f)]
  49. [TestCase((1f / 60), 3, 5, 0f)]
  50. [TestCase((1f / 50), 3, 5, 0.199999914f)]
  51. [TestCase((1f / 45), 3, 5, 0.333333284f)]
  52. [TestCase((1f / 30), 3, 5, 1f)]
  53. [TestCase((1f / 15), 3, 5, 3f)]
  54. public void CheckTrendValuesForVariousFrameTimes_WhenNumberOfUpdates_FewerThan_SampleCapacity_(float averageFrameTime, int numberOfUpdates, int sampleCapacity, float expectedTrendValue)
  55. {
  56. BuildTestConditions(averageFrameTime);
  57. PerformanceStateTracker testSubject = new MyPerformanceStateTracker(sampleCapacity);
  58. for (int i = 0; i < numberOfUpdates; i++)
  59. {
  60. testSubject.Update();
  61. }
  62. Assert.AreEqual(expectedTrendValue, testSubject.Trend);
  63. }
  64. [TestCase((1f / 120), 6, 5, -0.5f)]
  65. [TestCase((1f / 60), 6, 5, 0f)]
  66. [TestCase((1f / 50), 6, 5, 0.199999914f)]
  67. [TestCase((1f / 45), 6, 5, 0.333333284f)]
  68. [TestCase((1f / 30), 6, 5, 1f)]
  69. [TestCase((1f / 15), 6, 5, 3f)]
  70. [TestCase((1f / 120), 100, 97, -0.5f)]
  71. [TestCase((1f / 60), 700, 350, 0f)]
  72. [TestCase((1f / 50), 1000, 100, 0.200000018f)]
  73. [TestCase((1f / 45), 5000, 3000, 0.333325475f)]
  74. public void CheckTrendValuesForFrameTimesGreaterThanZero_AndNumberOfUpdates_GreaterThan_SampleCapacity_(float averageFrameTime, int numberOfUpdates, int sampleCapacity, float expectedTrendValue)
  75. {
  76. BuildTestConditions(averageFrameTime);
  77. PerformanceStateTracker testSubject = new MyPerformanceStateTracker(sampleCapacity);
  78. for (int i = 0; i < numberOfUpdates; i++)
  79. {
  80. testSubject.Update();
  81. }
  82. Assert.AreEqual(expectedTrendValue, testSubject.Trend);
  83. }
  84. [TestCase(1f, StateAction.Decrease, 10)]
  85. [TestCase(1000f, StateAction.FastDecrease, 10)]
  86. [TestCase(0.0000001f, StateAction.Stale, 10)]
  87. [TestCase(-0.999999f, StateAction.Stale, 10)]
  88. [TestCase(-0.999999f, StateAction.Stale, 0)] //sample capacity has no effect on StateAction outcome if avgFT is less than zero
  89. [TestCase(-0.999999f, StateAction.Stale, 500)] //sample capacity has no effect on StateAction outcome if avgFT is less than zero
  90. [TestCase(0f, StateAction.Stale, 10)]
  91. [TestCase(-9999.999999f, StateAction.Stale, 10)]
  92. public void CheckStateAction_When_AverageFrameTimeVaries(float averageFrameTime, StateAction stateAction, int sampleCapacity)
  93. {
  94. BuildTestConditions(averageFrameTime);
  95. Assert.AreEqual(stateAction, new SmallTFRPerformanceStateTracker(sampleCapacity).Update());
  96. }
  97. void BuildTestConditions(float averageFrameTime)
  98. {
  99. ft.AverageFrameTime = averageFrameTime;
  100. Mock.Get(m_perfStat).Setup(s => s.FrameTiming).Returns(ft);
  101. Mock.Get(m_ap).Setup(p => p.PerformanceStatus).Returns(m_perfStat);
  102. Holder.Instance = m_ap;
  103. }
  104. }
  105. partial class MyPerformanceStateTracker : PerformanceStateTracker
  106. {
  107. public MyPerformanceStateTracker(int sampleCapacity)
  108. : base(sampleCapacity) { }
  109. protected override float GetEffectiveTargetFrameRate()
  110. {
  111. return 60f;
  112. }
  113. }
  114. class SmallTFRPerformanceStateTracker : PerformanceStateTracker
  115. {
  116. public SmallTFRPerformanceStateTracker(int sampleCapacity)
  117. : base(sampleCapacity) { }
  118. protected override float GetEffectiveTargetFrameRate()
  119. {
  120. return 1.2f;
  121. }
  122. }
  123. }
  124. #endif