-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameProfiling.cs
More file actions
249 lines (213 loc) · 7.21 KB
/
FrameProfiling.cs
File metadata and controls
249 lines (213 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Windows.Threading;
namespace lifeviz;
internal sealed class FrameProfiler
{
private readonly Dictionary<string, List<double>> _samples = new(StringComparer.Ordinal);
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
public bool IsActive { get; private set; }
public string SessionName { get; private set; } = "profile";
public DateTime StartedUtc { get; private set; }
public DateTime EndedUtc { get; private set; }
public void Start(string sessionName)
{
_samples.Clear();
SessionName = string.IsNullOrWhiteSpace(sessionName) ? "profile" : sessionName.Trim();
StartedUtc = DateTime.UtcNow;
EndedUtc = StartedUtc;
IsActive = true;
}
public FrameProfileReport Stop()
{
EndedUtc = DateTime.UtcNow;
IsActive = false;
var metrics = _samples
.Select(pair => BuildMetric(pair.Key, pair.Value))
.OrderByDescending(metric => metric.Average)
.ToList();
return new FrameProfileReport
{
SessionName = SessionName,
StartedUtc = StartedUtc,
EndedUtc = EndedUtc,
DurationMs = Math.Max(0, (EndedUtc - StartedUtc).TotalMilliseconds),
Metrics = metrics
};
}
public void RecordSample(string metricName, double value)
{
if (!IsActive || string.IsNullOrWhiteSpace(metricName) || double.IsNaN(value) || double.IsInfinity(value))
{
return;
}
if (!_samples.TryGetValue(metricName, out var values))
{
values = new List<double>(512);
_samples.Add(metricName, values);
}
values.Add(value);
}
public string Export(FrameProfileReport report, string outputDirectory)
{
Directory.CreateDirectory(outputDirectory);
string safeSessionName = string.Concat(report.SessionName.Select(ch =>
char.IsLetterOrDigit(ch) || ch == '-' || ch == '_' ? ch : '-')).Trim('-');
if (string.IsNullOrWhiteSpace(safeSessionName))
{
safeSessionName = "profile";
}
string path = Path.Combine(outputDirectory, $"{safeSessionName}-{report.StartedUtc:yyyyMMdd-HHmmss}.json");
File.WriteAllText(path, JsonSerializer.Serialize(report, JsonOptions));
return path;
}
public static double ElapsedMilliseconds(long startTimestamp, long endTimestamp)
=> (endTimestamp - startTimestamp) * 1000.0 / Stopwatch.Frequency;
public static double ElapsedMilliseconds(long startTimestamp)
=> ElapsedMilliseconds(startTimestamp, Stopwatch.GetTimestamp());
private static FrameProfileMetricReport BuildMetric(string name, List<double> values)
{
values.Sort();
double total = values.Sum();
return new FrameProfileMetricReport
{
Name = name,
Count = values.Count,
Minimum = values[0],
Average = total / values.Count,
Median = Percentile(values, 0.50),
P95 = Percentile(values, 0.95),
P99 = Percentile(values, 0.99),
Maximum = values[^1],
Total = total
};
}
private static double Percentile(IReadOnlyList<double> sortedValues, double percentile)
{
if (sortedValues.Count == 0)
{
return 0;
}
if (sortedValues.Count == 1)
{
return sortedValues[0];
}
double clamped = Math.Clamp(percentile, 0, 1);
double index = clamped * (sortedValues.Count - 1);
int lower = (int)Math.Floor(index);
int upper = (int)Math.Ceiling(index);
if (lower == upper)
{
return sortedValues[lower];
}
double blend = index - lower;
return sortedValues[lower] + ((sortedValues[upper] - sortedValues[lower]) * blend);
}
}
internal sealed class UiThreadLatencyProbe : IDisposable
{
private readonly Dispatcher _dispatcher;
private readonly FrameProfiler _profiler;
private Timer? _timer;
private int _inputPending;
private int _backgroundPending;
public UiThreadLatencyProbe(Dispatcher dispatcher, FrameProfiler profiler)
{
_dispatcher = dispatcher;
_profiler = profiler;
}
public void Start(TimeSpan interval)
{
Stop();
_timer = new Timer(OnTimerTick, null, interval, interval);
}
public void Stop()
{
Interlocked.Exchange(ref _inputPending, 0);
Interlocked.Exchange(ref _backgroundPending, 0);
_timer?.Dispose();
_timer = null;
}
public void Dispose() => Stop();
private void OnTimerTick(object? state)
{
if (!_profiler.IsActive)
{
return;
}
QueueInputLatencyProbe();
QueueBackgroundLatencyProbe();
}
private void QueueInputLatencyProbe()
{
if (Interlocked.CompareExchange(ref _inputPending, 1, 0) != 0)
{
return;
}
long queuedAt = Stopwatch.GetTimestamp();
_dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
_profiler.RecordSample("ui_input_latency_ms", FrameProfiler.ElapsedMilliseconds(queuedAt));
Interlocked.Exchange(ref _inputPending, 0);
}));
}
private void QueueBackgroundLatencyProbe()
{
if (Interlocked.CompareExchange(ref _backgroundPending, 1, 0) != 0)
{
return;
}
long queuedAt = Stopwatch.GetTimestamp();
_dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
_profiler.RecordSample("ui_background_latency_ms", FrameProfiler.ElapsedMilliseconds(queuedAt));
Interlocked.Exchange(ref _backgroundPending, 0);
}));
}
}
internal sealed class FrameProfileReport
{
public string SessionName { get; set; } = "profile";
public DateTime StartedUtc { get; set; }
public DateTime EndedUtc { get; set; }
public double DurationMs { get; set; }
public List<FrameProfileMetricReport> Metrics { get; set; } = new();
}
internal sealed class FrameProfileMetricReport
{
public string Name { get; set; } = string.Empty;
public int Count { get; set; }
public double Minimum { get; set; }
public double Average { get; set; }
public double Median { get; set; }
public double P95 { get; set; }
public double P99 { get; set; }
public double Maximum { get; set; }
public double Total { get; set; }
}
internal sealed class RollingMetricWindow
{
private readonly Queue<double> _values;
private readonly int _capacity;
private double _sum;
public RollingMetricWindow(int capacity)
{
_capacity = Math.Max(1, capacity);
_values = new Queue<double>(_capacity);
}
public double Average => _values.Count == 0 ? 0.0 : _sum / _values.Count;
public void Add(double value)
{
_values.Enqueue(value);
_sum += value;
while (_values.Count > _capacity)
{
_sum -= _values.Dequeue();
}
}
}