|
| 1 | +using NUnit.Framework; |
| 2 | + |
| 3 | +namespace UnityEngine.Rendering.Tests |
| 4 | +{ |
| 5 | + static class FrameTimingTestExtensions |
| 6 | + { |
| 7 | + public static BottleneckHistogram ToHistogram(this FrameTimeSample s) |
| 8 | + { |
| 9 | + var history = new BottleneckHistory(1); |
| 10 | + history.AddBottleneckFromAveragedSample(s); |
| 11 | + history.ComputeHistogram(); |
| 12 | + return history.Histogram; |
| 13 | + } |
| 14 | + |
| 15 | + public static string ToDebugString(this BottleneckHistogram h) => |
| 16 | + $"GPU={h.GPU}, CPU={h.CPU}, PresentLimited={h.PresentLimited}, Balanced={h.Balanced}"; |
| 17 | + } |
| 18 | + |
| 19 | + class DebugFrameTimingTests |
| 20 | + { |
| 21 | + static FrameTimeSample MakeSample( |
| 22 | + float fullFrameTime, |
| 23 | + float gpu, |
| 24 | + float mainCpu, |
| 25 | + float renderCpu, |
| 26 | + float presentWait = 0f, |
| 27 | + float fps = 0f) |
| 28 | + { |
| 29 | + return new FrameTimeSample |
| 30 | + { |
| 31 | + FullFrameTime = fullFrameTime, |
| 32 | + GPUFrameTime = gpu, |
| 33 | + MainThreadCPUFrameTime = mainCpu, |
| 34 | + RenderThreadCPUFrameTime = renderCpu, |
| 35 | + MainThreadCPUPresentWaitTime = presentWait, |
| 36 | + FramesPerSecond = fps |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void Histogram_ZeroGPUFrameTime_ProducesIndeterminate() |
| 42 | + { |
| 43 | + var h = MakeSample(fullFrameTime: 10, gpu: 0, mainCpu: 5, renderCpu: 5).ToHistogram(); |
| 44 | + Assert.That(h.GPU + h.CPU + h.PresentLimited + h.Balanced, Is.EqualTo(0f), |
| 45 | + $"Zero GPUFrameTime must classify as Indeterminate (all buckets 0). Got {h.ToDebugString()}."); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public void Histogram_ZeroMainThreadCPUFrameTime_ProducesIndeterminate() |
| 50 | + { |
| 51 | + var h = MakeSample(fullFrameTime: 10, gpu: 5, mainCpu: 0, renderCpu: 5).ToHistogram(); |
| 52 | + Assert.That(h.GPU + h.CPU + h.PresentLimited + h.Balanced, Is.EqualTo(0f), |
| 53 | + $"Zero MainThreadCPUFrameTime must classify as Indeterminate (all buckets 0). Got {h.ToDebugString()}."); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void Histogram_GPUBound_ProducesGPU() |
| 58 | + { |
| 59 | + // GPU close to FullFrameTime, CPU times are not. |
| 60 | + var h = MakeSample(fullFrameTime: 10, gpu: 9, mainCpu: 2, renderCpu: 2).ToHistogram(); |
| 61 | + Assert.That(h.GPU, Is.EqualTo(1f), |
| 62 | + $"GPU-dominant sample must put 100% in the GPU bucket. Got {h.ToDebugString()}."); |
| 63 | + } |
| 64 | + |
| 65 | + [Test] |
| 66 | + public void Histogram_MainThreadCPUBound_ProducesCPU() |
| 67 | + { |
| 68 | + var h = MakeSample(fullFrameTime: 10, gpu: 2, mainCpu: 9, renderCpu: 2).ToHistogram(); |
| 69 | + Assert.That(h.CPU, Is.EqualTo(1f), |
| 70 | + $"MainThread-dominant sample must put 100% in the CPU bucket. Got {h.ToDebugString()}."); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public void Histogram_RenderThreadCPUBound_ProducesCPU() |
| 75 | + { |
| 76 | + var h = MakeSample(fullFrameTime: 10, gpu: 2, mainCpu: 2, renderCpu: 9).ToHistogram(); |
| 77 | + Assert.That(h.CPU, Is.EqualTo(1f), |
| 78 | + $"RenderThread-dominant sample must put 100% in the CPU bucket. Got {h.ToDebugString()}."); |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public void Histogram_BalancedWithPresentWait_ProducesPresentLimited() |
| 83 | + { |
| 84 | + var h = MakeSample(fullFrameTime: 10, gpu: 2, mainCpu: 2, renderCpu: 2, presentWait: 1f).ToHistogram(); |
| 85 | + Assert.That(h.PresentLimited, Is.EqualTo(1f), |
| 86 | + $"Non-dominant sample with present wait > 0.5ms must classify as PresentLimited. Got {h.ToDebugString()}."); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void Histogram_BalancedWithoutPresentWait_ProducesBalanced() |
| 91 | + { |
| 92 | + var h = MakeSample(fullFrameTime: 10, gpu: 5, mainCpu: 5, renderCpu: 5, presentWait: 0f).ToHistogram(); |
| 93 | + Assert.That(h.Balanced, Is.EqualTo(1f), |
| 94 | + $"Non-dominant sample with no present wait must classify as Balanced. Got {h.ToDebugString()}."); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void Histogram_MixedSamples_RatiosMatchSampleCounts() |
| 99 | + { |
| 100 | + // 2x GPU, 2x CPU, 1x PresentLimited -> 0.4 / 0.4 / 0.2 / 0 |
| 101 | + var history = new BottleneckHistory(8); |
| 102 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 9, 2, 2)); |
| 103 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 9, 2, 2)); |
| 104 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 2, 9, 2)); |
| 105 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 2, 9, 2)); |
| 106 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 2, 2, 2, presentWait: 1f)); |
| 107 | + history.ComputeHistogram(); |
| 108 | + var h = history.Histogram; |
| 109 | + |
| 110 | + Assert.That(h.GPU, Is.EqualTo(0.4f).Within(1e-6f), $"GPU bucket. Got {h.ToDebugString()}."); |
| 111 | + Assert.That(h.CPU, Is.EqualTo(0.4f).Within(1e-6f), $"CPU bucket. Got {h.ToDebugString()}."); |
| 112 | + Assert.That(h.PresentLimited, Is.EqualTo(0.2f).Within(1e-6f), $"PresentLimited bucket. Got {h.ToDebugString()}."); |
| 113 | + Assert.That(h.Balanced, Is.EqualTo(0f), $"Balanced bucket. Got {h.ToDebugString()}."); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void Histogram_IndeterminateSamples_ReduceVisibleTotalBelowOne() |
| 118 | + { |
| 119 | + // 1 valid GPU sample + 4 indeterminate (GPU=0) -> visible total = 0.2. |
| 120 | + var history = new BottleneckHistory(8); |
| 121 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 9, 2, 2)); |
| 122 | + for (int i = 0; i < 4; i++) |
| 123 | + history.AddBottleneckFromAveragedSample(MakeSample(10, 0, 5, 5)); |
| 124 | + history.ComputeHistogram(); |
| 125 | + var h = history.Histogram; |
| 126 | + |
| 127 | + Assert.That(h.GPU, Is.EqualTo(0.2f).Within(1e-6f), $"GPU bucket. Got {h.ToDebugString()}."); |
| 128 | + Assert.That(h.GPU + h.CPU + h.PresentLimited + h.Balanced, Is.EqualTo(0.2f).Within(1e-6f), |
| 129 | + $"Indeterminate samples must be excluded from visible buckets. Got {h.ToDebugString()}."); |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public void BottleneckHistory_DiscardOldSamples_KeepsAtMostHistorySize() |
| 134 | + { |
| 135 | + // With historySize=3, add 5 GPU-bound then 2 CPU-bound samples. |
| 136 | + // Retained samples should be [GPU, CPU, CPU] -> GPU=1/3, CPU=2/3. |
| 137 | + const int historySize = 3; |
| 138 | + var history = new BottleneckHistory(historySize); |
| 139 | + var gpuSample = MakeSample(10, 9, 2, 2); |
| 140 | + var cpuSample = MakeSample(10, 2, 9, 2); |
| 141 | + |
| 142 | + for (int i = 0; i < 5; i++) |
| 143 | + { |
| 144 | + history.DiscardOldSamples(historySize); |
| 145 | + history.AddBottleneckFromAveragedSample(gpuSample); |
| 146 | + } |
| 147 | + for (int i = 0; i < 2; i++) |
| 148 | + { |
| 149 | + history.DiscardOldSamples(historySize); |
| 150 | + history.AddBottleneckFromAveragedSample(cpuSample); |
| 151 | + } |
| 152 | + history.ComputeHistogram(); |
| 153 | + var h = history.Histogram; |
| 154 | + |
| 155 | + Assert.That(h.GPU, Is.EqualTo(1f / 3f).Within(1e-6f), $"Only 3 newest samples should be retained. Got {h.ToDebugString()}."); |
| 156 | + Assert.That(h.CPU, Is.EqualTo(2f / 3f).Within(1e-6f), $"Only 3 newest samples should be retained. Got {h.ToDebugString()}."); |
| 157 | + } |
| 158 | + |
| 159 | + [Test] |
| 160 | + public void Reset_ClearsBottleneckHistogram() |
| 161 | + { |
| 162 | + var debug = new DebugFrameTiming(); |
| 163 | + debug.m_BottleneckHistory.AddBottleneckFromAveragedSample(MakeSample(10, 9, 2, 2)); |
| 164 | + debug.m_BottleneckHistory.ComputeHistogram(); |
| 165 | + |
| 166 | + debug.Reset(); |
| 167 | + |
| 168 | + var h = debug.m_BottleneckHistory.Histogram; |
| 169 | + Assert.That(h.GPU + h.CPU + h.PresentLimited + h.Balanced, Is.EqualTo(0f), $"Reset must zero the histogram. Got {h.ToDebugString()}."); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments