forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceTestViewer.cs
More file actions
1237 lines (1113 loc) · 48.4 KB
/
Copy pathPerformanceTestViewer.cs
File metadata and controls
1237 lines (1113 loc) · 48.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Unity.PerformanceTesting;
using Unity.PerformanceTesting.Data;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using SampleGroup = Unity.PerformanceTesting.Data.SampleGroup;
namespace Editor.PerformanceTestViewer
{
public class PerformanceTestViewer : EditorWindow, IHasCustomMenu
{
private const float PlotAlpha = 0.4f;
private static Color ResultsColor => EditorGUIUtility.isProSkin ? ResultsColorDark : ResultsColorBright;
private static Color ResultsColorText
{
get
{
var c = ResultsColor;
c.a = 1;
return c;
}
}
private static Color BaselineColor => EditorGUIUtility.isProSkin ? BaselineColorDark : BaselineColorBright;
private static Color BaselineColorText
{
get
{
var c = BaselineColor;
c.a = 1;
return c;
}
}
// Colors are carefully chosen to be color-blind friendly.
private static readonly Color ResultsColorBright = new Color(1, 194/255f, 10/255f, PlotAlpha);
private static readonly Color ResultsColorDark = new Color(254/255f, 254/255f, 98/255f, PlotAlpha);
private static readonly Color BaselineColorBright = new Color(12/255f, 123/255f, 220/255f, PlotAlpha);
private static readonly Color BaselineColorDark = new Color(211/255f, 95/255f, 183/255f, PlotAlpha);
private static readonly Color RegressionColor = Color.red;
private static readonly Color ImprovementColor = Color.green;
private enum Category {
Regressions = 0,
Improvements = 1,
Inconclusive = 2,
All = 3
}
struct HistogramData
{
public double Min;
public double Max;
public SampleGroup X;
public SampleGroup Y;
}
struct ChangeResult
{
public string Name;
public StatisticsUtility.WelchTestResult Test;
public HistogramData Histogram;
/// <summary>
/// The maximum regression percentage in the interval relative to the base value.
/// </summary>
public double LowerChangePercentage => Test.IntervalLower / Test.MeanY;
public double UpperChangePercentage => Test.IntervalUpper / Test.MeanY;
}
#pragma warning disable 0649
[SerializeField]
private UnityEngine.Object _readMeAsset;
#pragma warning restore 0649
private Run _baselineData;
private Run _comparisonData;
private Run _tmpResults;
private string _testFilter;
private readonly List<ChangeResult> _regressions = new List<ChangeResult>();
private readonly List<ChangeResult> _filteredRegressions = new List<ChangeResult>();
private readonly List<ChangeResult> _improvements = new List<ChangeResult>();
private readonly List<ChangeResult> _filteredImprovements = new List<ChangeResult>();
private readonly List<ChangeResult> _inconclusive = new List<ChangeResult>();
private readonly List<ChangeResult> _filteredInconclusive = new List<ChangeResult>();
private readonly List<ChangeResult> _allResults = new List<ChangeResult>();
private readonly List<ChangeResult> _filteredAll = new List<ChangeResult>();
private ListView _resultsList;
private Label _dataStatusLabel;
private Label _regressionCountLabel;
private Label _improvementCountLabel;
private Label _inconclusiveCountLabel;
private Label _totalCountLabel;
private Label _baselineOverviewLabel;
private Label _newResultsOverviewLabel;
private Button _recordBaselineButton;
private Button _recordComparisonButton;
private FileSystemWatcher _fileWatcher;
private bool _isRecordingNewBaseline;
private bool _isRecordingNewComparison;
private List<ChangeResult> _currentData;
private List<ChangeResult> _currentSource;
private static string BaselineDataPath =>
Path.Combine(Application.persistentDataPath, "PerformanceTestResults_baseline.json");
private static string ComparisonDataPath =>
Path.Combine(Application.persistentDataPath, "PerformanceTestResults_comparison.json");
private static string ResultsPath =>
Path.Combine(Application.persistentDataPath, "PerformanceTestResults.json");
private const string HelpText = "Use the window's menu (top right) for more options and to open the ReadMe. Please read the disclaimer in there.";
[MenuItem("Window/Analysis/Performance Test Viewer")]
public static void ShowWindow()
{
var window = GetWindow<PerformanceTestViewer>();
window.titleContent = new GUIContent("Performance Test Viewer");
window.Show();
}
static string FormatPercentage(double p)
=> (p > 0 ? "+" : "") + (p * 100).ToString("F2", CultureInfo.InvariantCulture) + '%';
static string FormatRelativeInterval(ChangeResult r) =>
$"[{FormatPercentage(r.LowerChangePercentage)}, {FormatPercentage(r.UpperChangePercentage)}]";
static string FormatAbsoluteChange(double p, ChangeResult r)
=> (p > 0 ? "-" : "") + p.ToString("F2", CultureInfo.InvariantCulture) + FormatUnit(r.Histogram.X.Unit);
static string FormatAbsoluteInterval(ChangeResult r) =>
$"[{FormatAbsoluteChange(r.Test.IntervalLower, r)}, {FormatAbsoluteChange(r.Test.IntervalUpper, r)}]";
void ToggleBaselineRecording()
{
if (_isRecordingNewBaseline)
StopBaselineRecording();
else
{
StopComparisonRecording();
_isRecordingNewBaseline = true;
_recordBaselineButton.text = "Recording...";
}
}
void ToggleComparisonRecording()
{
if (_isRecordingNewBaseline)
StopComparisonRecording();
else
{
StopBaselineRecording();
_isRecordingNewComparison = true;
_recordComparisonButton.text = "Recording...";
}
}
void StopComparisonRecording()
{
_isRecordingNewComparison = false;
_recordComparisonButton.text = "Record new comparison data";
}
void StopBaselineRecording()
{
_isRecordingNewBaseline = false;
_recordBaselineButton.text = "Record new baseline";
}
void UpdateFilter(string s, bool reset=false)
{
s = s ?? "";
bool Filter(ChangeResult r) => r.Name.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0;
if (!reset && s.StartsWith(_testFilter, StringComparison.Ordinal))
{
_currentData.RemoveAll(t => !Filter(t));
}
else
{
_currentData.Clear();
_currentData.AddRange(_currentSource.Where(Filter));
}
_testFilter = s;
_resultsList.itemsSource = _currentData;
}
static string CleanupTestName(string name)
{
var separator = name.LastIndexOf('.');
if (separator < 0)
return name;
return name.Substring(separator + 1);
}
class ListItem : VisualElement
{
public int Index;
}
private void DrawDistribution(IMGUIContainer self, ListItem item)
{
var rect = self.contentRect;
var histogram = _currentData[item.Index].Histogram;
GUI.Box(rect, "", GUI.skin.textArea);
// add some spaces on the side
double min = histogram.Min * 0.98f;
double max = histogram.Max * 1.02f;
var point = new Rect(0, 0, 3, 3);
var offset = new Vector2(rect.xMin, rect.center.y);
float width = rect.width;
foreach (var sx in histogram.X.Samples)
{
float x = width * (float) ((sx - min) / (max - min));
float y = 5 * UnityEngine.Random.value + 3;
point.center = offset + new Vector2(x, y);
EditorGUI.DrawRect(point, ResultsColor);
}
foreach (var sy in histogram.Y.Samples)
{
float x = width * (float) ((sy - min) / (max - min));
float y = 5 * UnityEngine.Random.value - 3;
point.center = offset + new Vector2(x, y);
EditorGUI.DrawRect(point, BaselineColor);
}
}
VisualElement CreateListItem()
{
var container = new ListItem
{
style =
{
borderBottomWidth = 10,
borderTopWidth = 10,
borderLeftWidth = 4,
borderRightWidth = 4
}
};
var topBar = new VisualElement {
style =
{
height = EditorGUIUtility.singleLineHeight,
flexDirection = FlexDirection.RowReverse,
flexGrow = 0,
},
};
topBar.Add(new Label
{
name = "interval",
tooltip = "This is the 95% confidence interval for the change in means, relative to the baseline mean.",
style = { flexGrow = 0, minWidth = 80, marginLeft = 5}
});
topBar.Add(new Label { name = "name", style =
{
flexBasis = 0,
flexGrow = 1, unityFont = EditorStyles.boldFont,
overflow = Overflow.Hidden
}});
container.Add(topBar);
var plot = new IMGUIContainer
{
style = {flexGrow = 1},
};
plot.onGUIHandler = () => DrawDistribution(plot, container);
plot.tooltip = "This is a plot of the distribution of the results. The metric you are tracking is on the horizontal axis (usually this is time, lower values are to the left). Each sample is represented by a point. The y-axis doesn't have a meaning. The vertical jitter is merely to aid legibility.";
plot.AddManipulator(new ContextualMenuManipulator((ContextualMenuPopulateEvent evt) =>
{
evt.menu.AppendAction("Copy samples as CSV", (a) =>
{
var idx = container.Index;
var d = _currentData[idx];
CsvExportUtility.CopySamplesCsv(d.Histogram.X, d.Histogram.Y);
} );
}));
container.Add(plot);
var bottomBar = new VisualElement {
style =
{
height = EditorGUIUtility.singleLineHeight,
flexDirection = FlexDirection.Row,
flexGrow = 0
},
};
bottomBar.Add(new Label
{
name = "min",
style = {flexGrow = 1, unityTextAlign = TextAnchor.MiddleLeft},
tooltip = "Minimum value within the two samples."
});
bottomBar.Add(new Label
{
name = "middle",
style = {flexGrow = 1, unityTextAlign = TextAnchor.MiddleCenter},
});
bottomBar.Add(new Label
{
name = "max",
style = {flexGrow = 1, unityTextAlign = TextAnchor.MiddleRight},
tooltip = "Maximum value within the two samples."
});
container.Add(bottomBar);
return container;
}
private void BindListItem(VisualElement c, int idx)
{
(c as ListItem).Index = idx;
var r = _currentData[idx];
var name = c.Q<Label>("name");
name.text = CleanupTestName(r.Name);
name.tooltip = r.Name;
c.Q<Label>("interval").text = FormatRelativeInterval(r);
double min = r.Histogram.Min;
double max = r.Histogram.Max;
int unitLevel = DeconstructUnit(r.Histogram.X.Unit, out bool isMemory);
int newLevel = AdjustUnit(unitLevel, max, isMemory);
for (int i = unitLevel - newLevel; i > 0; i--)
{
min *= 1000;
max *= 1000;
}
for (int i = newLevel - unitLevel; i > 0; i--)
{
min /= 1000;
max /= 1000;
}
var unit = ConstructUnit(newLevel, isMemory);
var unitName = FormatUnit(unit);
c.Q<Label>("min").text = min.ToString("F2") + unitName;
c.Q<Label>("middle").text = ((min + max) / 2).ToString("F2") + unitName;
c.Q<Label>("max").text = max.ToString("F2") + unitName;
}
static int AdjustUnit(int unit, double value, bool isForMemory)
{
double factor = isForMemory ? 1024 : 1000;
while (value < 0.1 && unit > 0)
{
value *= factor;
unit -= 1;
}
while (value >= 1000 && unit < 3)
{
value /= factor;
unit += 1;
}
return unit;
}
static SampleUnit ConstructUnit(int unit, bool isMemoryMeasure)
{
switch (unit)
{
case 0: return isMemoryMeasure ? SampleUnit.Byte : SampleUnit.Nanosecond;
case 1: return isMemoryMeasure ? SampleUnit.Kilobyte : SampleUnit.Microsecond;
case 2: return isMemoryMeasure ? SampleUnit.Megabyte : SampleUnit.Millisecond;
case 3: return isMemoryMeasure ? SampleUnit.Gigabyte : SampleUnit.Second;
default: throw new ArgumentException(nameof(unit), nameof(unit));
}
}
static int DeconstructUnit(SampleUnit unit, out bool isMemoryMeasure)
{
isMemoryMeasure = false;
switch (unit)
{
case SampleUnit.Nanosecond: return 0;
case SampleUnit.Microsecond: return 1;
case SampleUnit.Millisecond: return 2;
case SampleUnit.Second: return 3;
case SampleUnit.Byte: isMemoryMeasure = true; return 0;
case SampleUnit.Kilobyte: isMemoryMeasure = true; return 1;
case SampleUnit.Megabyte: isMemoryMeasure = true; return 2;
case SampleUnit.Gigabyte: isMemoryMeasure = true; return 3;
default:
throw new ArgumentOutOfRangeException(nameof(unit), unit, null);
}
}
static string FormatUnit(SampleUnit unit)
{
switch (unit)
{
case SampleUnit.Nanosecond: return "ns";
case SampleUnit.Microsecond: return "μs";
case SampleUnit.Millisecond: return "ms";
case SampleUnit.Second: return "s";
case SampleUnit.Byte: return "b";
case SampleUnit.Kilobyte: return "kb";
case SampleUnit.Megabyte: return "mb";
case SampleUnit.Gigabyte: return "gb";
default:
throw new ArgumentOutOfRangeException(nameof(unit), unit, null);
}
}
private void SearchFilterChanged(ChangeEvent<string> evt)
{
UpdateFilter(evt.newValue);
}
private Category _selectedCategory;
private void SetCategorySelection(Category category)
{
_selectedCategory = category;
_categorySelection.SetValueWithoutNotify(category);
switch (category)
{
case Category.Regressions:
{
_currentData = _filteredRegressions;
_currentSource = _regressions;
break;
}
case Category.Improvements:
{
_currentData = _filteredImprovements;
_currentSource = _improvements;
break;
}
case Category.Inconclusive:
{
_currentData = _filteredInconclusive;
_currentSource = _inconclusive;
break;
}
case Category.All:
{
_currentData = _filteredAll;
_currentSource = _allResults;
break;
}
}
UpdateFilter(_testFilter, true);
}
private void HandleCategorySelection(ChangeEvent<Enum> evt)
{
SetCategorySelection((Category) evt.newValue);
}
private static Color GetDefaultHighlightColor()
{
var c = (EditorGUIUtility.isProSkin ? 42 : 240) / 255f;
return new Color(c, c, c);
}
private EnumField _categorySelection;
private void OnEnable()
{
_isRecordingNewBaseline = false;
_isRecordingNewComparison = false;
rootVisualElement.Add(new HelpBox(HelpText, HelpBoxMessageType.Info)
{
style =
{
marginLeft = 3,
marginRight = 3,
marginBottom = 3,
marginTop = 3
}
});
{
var comparisonBar = new VisualElement
{
style =
{
flexDirection = FlexDirection.Row,
marginLeft = 6,
marginRight = 6,
}
};
rootVisualElement.Add(comparisonBar);
var baseLineBar = new VisualElement {style =
{
flexGrow = 1,
flexBasis = 1,
flexDirection = FlexDirection.Column,
marginLeft = 3,
marginRight = 3
}};
baseLineBar.AddToClassList("unity-help-box");
baseLineBar.Add(new Label
{
text = "Baseline",
style =
{
color = BaselineColorText,
alignSelf = Align.FlexStart,
unityTextAlign = TextAnchor.MiddleLeft,
unityFontStyleAndWeight = FontStyle.Bold,
},
tooltip = "This is the baseline that any new results will be compared to."
});
baseLineBar.Add(_baselineOverviewLabel = new Label
{
style =
{
alignSelf = Align.FlexStart,
whiteSpace = WhiteSpace.Normal,
}
});
baseLineBar.Add(new VisualElement { style = {flexGrow = 1}});
baseLineBar.Add(_recordBaselineButton = new Button(ToggleBaselineRecording)
{
text = "Record new baseline",
style =
{
alignSelf = Align.Stretch
},
tooltip = "Click this before running a set of performance tests to automatically set them as the new baseline."
});
comparisonBar.Add(baseLineBar);
var newResultsBar = new VisualElement {
style =
{
flexGrow = 1,
flexBasis = 1,
flexDirection = FlexDirection.Column,
marginLeft = 3,
marginRight = 3
}
};
newResultsBar.AddToClassList("unity-help-box");
newResultsBar.Add(new Label
{
text = "New results",
style =
{
color = ResultsColorText,
alignSelf = Align.FlexStart,
unityTextAlign = TextAnchor.MiddleLeft,
unityFontStyleAndWeight = FontStyle.Bold
},
tooltip = "These are the results captured from the last time you ran performance tests."
});
newResultsBar.Add(_newResultsOverviewLabel = new Label
{
style =
{
alignSelf = Align.FlexStart,
whiteSpace = WhiteSpace.Normal,
}
});
newResultsBar.Add(new VisualElement { style = {flexGrow = 1}});
newResultsBar.Add(_recordComparisonButton = new Button(ToggleComparisonRecording)
{
text = "Record new comparison data",
style = {
alignSelf = Align.Stretch
},
tooltip = "Click this before running a set of performance tests to automatically set them as the new comparison."
});
comparisonBar.Add(newResultsBar);
}
rootVisualElement.Add(new Label
{
text = "Results",
style = {
fontSize = 12,
unityFontStyleAndWeight = FontStyle.Bold,
marginTop = 15
}
});
{
var toolbar = new VisualElement {style = {height = 22, flexDirection = FlexDirection.Row}};
var toolbarSearchField = new ToolbarSearchField();
toolbarSearchField.RegisterValueChangedCallback(SearchFilterChanged);
toolbar.Add(toolbarSearchField);
toolbar.Add(new VisualElement {style = {flexGrow = 1}});
var m = _categorySelection = new EnumField(_selectedCategory)
{
style = { width = 110 }
};
_categorySelection.RegisterValueChangedCallback(HandleCategorySelection);
toolbar.Add(m);
toolbar.Add(new VisualElement {style = {width = 5}});
_regressionCountLabel = new Label
{
style = {unityTextAlign = TextAnchor.MiddleCenter, color = RegressionColor}
};
_regressionCountLabel.RegisterCallback<ClickEvent>(a=> { SetCategorySelection(Category.Regressions); });
AddMouseOverHighlight(_regressionCountLabel);
toolbar.Add(_regressionCountLabel);
toolbar.Add(new Label(" | ") {style = {unityTextAlign = TextAnchor.MiddleCenter}});
_improvementCountLabel = new Label
{
style = {unityTextAlign = TextAnchor.MiddleCenter, color = ImprovementColor}
};
_improvementCountLabel.RegisterCallback<ClickEvent>(a=> { SetCategorySelection(Category.Improvements); });
AddMouseOverHighlight(_improvementCountLabel);
toolbar.Add(_improvementCountLabel);
toolbar.Add(new Label(" | ") {style = {unityTextAlign = TextAnchor.MiddleCenter}});
_inconclusiveCountLabel = new Label
{
style = {unityTextAlign = TextAnchor.MiddleCenter}
};
_inconclusiveCountLabel.RegisterCallback<ClickEvent>(a=> { SetCategorySelection(Category.Inconclusive); });
AddMouseOverHighlight(_inconclusiveCountLabel);
toolbar.Add(_inconclusiveCountLabel);
toolbar.Add(new Label(" | ") {style = {unityTextAlign = TextAnchor.MiddleCenter}});
_totalCountLabel = new Label
{
style = {unityTextAlign = TextAnchor.MiddleCenter}
};
_totalCountLabel.RegisterCallback<ClickEvent>(a=> { SetCategorySelection(Category.All); });
AddMouseOverHighlight(_totalCountLabel);
toolbar.Add(_totalCountLabel);
void AddMouseOverHighlight(Label l)
{
l.RegisterCallback<MouseEnterEvent>(a => { l.style.backgroundColor = GetDefaultHighlightColor(); });
l.RegisterCallback<MouseLeaveEvent>(a => { l.style.backgroundColor = StyleKeyword.Undefined; });
}
rootVisualElement.Add(toolbar);
}
{
var list = new ListView(_filteredRegressions, 80, CreateListItem, BindListItem)
{
selectionType = SelectionType.None
};
list.style.flexGrow = 1;
rootVisualElement.Add(list);
_resultsList = list;
}
{
var bottomBar = new Toolbar
{
style = {flexDirection = FlexDirection.RowReverse}
};
_dataStatusLabel = new Label
{
style = {unityTextAlign = TextAnchor.MiddleRight},
text = "Test"
};
bottomBar.Add(_dataStatusLabel);
rootVisualElement.Add(bottomBar);
}
var fullPath = Path.GetFullPath(ResultsPath);
_fileWatcher?.Dispose();
_fileWatcher = new FileSystemWatcher(Path.GetDirectoryName(fullPath), Path.GetFileName(fullPath))
{
IncludeSubdirectories = false,
};
_fileWatcher.Changed += TestResultsChanged;
_fileWatcher.Created += TestResultsChanged;
_fileWatcher.EnableRaisingEvents = true;
_baselineData = null;
SetCategorySelection(0);
LoadTestResults();
}
private void OnDestroy()
{
_fileWatcher.Dispose();
}
private bool _reloadFiles;
private void OnInspectorUpdate()
{
if (_reloadFiles)
{
lock (_fileWatcher)
{
_reloadFiles = false;
}
LoadTestResults();
Repaint();
}
}
private void TestResultsChanged(object sender, FileSystemEventArgs e)
{
lock (_fileWatcher)
{
_reloadFiles = true;
}
}
private static DateTime GetRecordingTime(Run r)
{
// For unknown reasons, the recording time is sometimes in milliseconds and sometimes in seconds.
DateTimeOffset timeOffset;
if (r.Date < -62135596800 || r.Date > 253402300799)
timeOffset = DateTimeOffset.FromUnixTimeMilliseconds(r.Date);
else
timeOffset = DateTimeOffset.FromUnixTimeSeconds(r.Date);
return timeOffset.DateTime.ToLocalTime();
}
private void UpdateData()
{
StopBaselineRecording();
StopComparisonRecording();
_allResults.Clear();
_regressions.Clear();
_improvements.Clear();
_inconclusive.Clear();
_filteredAll.Clear();
_filteredRegressions.Clear();
_filteredImprovements.Clear();
_filteredInconclusive.Clear();
if (_comparisonData != null && _baselineData != null)
{
CalculateRegressions(_baselineData, _comparisonData, _regressions, _improvements, _inconclusive);
_regressions.Sort((lhs, rhs) => rhs.UpperChangePercentage.CompareTo(lhs.UpperChangePercentage));
_improvements.Sort((lhs, rhs) => lhs.UpperChangePercentage.CompareTo(rhs.UpperChangePercentage));
_allResults.AddRange(_regressions);
_allResults.AddRange(_improvements);
_allResults.AddRange(_inconclusive);
_allResults.Sort((lhs, rhs) => String.Compare(lhs.Name, rhs.Name, StringComparison.OrdinalIgnoreCase));
_dataStatusLabel.text = $"Baseline from {GetRecordingTime(_baselineData)}, results from {GetRecordingTime(_comparisonData)}";
}
else if (_comparisonData != null)
{
_dataStatusLabel.text = "Set the current data as a baseline and run another test";
}
else
{
_dataStatusLabel.text = "No performance data loaded - run performance tests to get started";
}
if (_baselineData != null)
_baselineOverviewLabel.text = FormatResults(_baselineData);
else
_baselineOverviewLabel.text = "No baseline recorded! Record one to get started. Press the button below, then run some performance tests.";
if (_comparisonData != null)
_newResultsOverviewLabel.text = FormatResults(_comparisonData);
else
_newResultsOverviewLabel.text = "No comparison results recorded! Record some to compare to the baseline. Press the button below, then run some performance tests.";
string FormatResults(Run r) =>
$"{r.Results.Count.ToString()} entries\n{r.Player.Platform}/{r.Player.ScriptingBackend}\n{GetRecordingTime(r)}";
UpdateFilter(_testFilter, true);
_regressionCountLabel.text = $"> {_regressions.Count}";
_regressionCountLabel.tooltip = $"{_regressions.Count} regressions";
_improvementCountLabel.text = $"< {_improvements.Count}";
_improvementCountLabel.tooltip = $"{_improvements.Count} improvements";
_inconclusiveCountLabel.text = $"~ {_inconclusive.Count}";
_inconclusiveCountLabel.tooltip = $"{_inconclusive.Count} inconclusive";
_totalCountLabel.text = $"{_allResults.Count} total";
_totalCountLabel.tooltip = $"{_allResults.Count} total";
}
private void LoadTestResults()
{
_tmpResults = LoadResults(ResultsPath);
if (_isRecordingNewBaseline)
{
_baselineData = _tmpResults;
var json = JsonConvert.SerializeObject(_baselineData);
File.WriteAllText(BaselineDataPath, json);
_comparisonData = LoadResults(ComparisonDataPath);
}
else if (_isRecordingNewComparison)
{
_comparisonData = _tmpResults;
var json = JsonConvert.SerializeObject(_comparisonData);
File.WriteAllText(ComparisonDataPath, json);
_baselineData = LoadResults(BaselineDataPath);
}
else
{
_comparisonData = LoadResults(ComparisonDataPath);
_baselineData = LoadResults(BaselineDataPath);
}
UpdateData();
}
// Changes that are below this threshold will be ignored
private const double PercentageCutOffRegressionMin = 0.01;
private const double PercentageCutOffRegressionMax = 0.1;
private const double PercentageCutOffImprovement = 0.1;
private static void CalculateRegressions(Run oldRun, Run newRun,
List<ChangeResult> regressions,
List<ChangeResult> improvements,
List<ChangeResult> inconclusive)
{
var newResults = newRun.Results.ToDictionary(r => r.Name);
foreach (var oldResult in oldRun.Results)
{
if (!newResults.TryGetValue(oldResult.Name, out var newResult))
continue;
if (newResult.SampleGroups.Count != oldResult.SampleGroups.Count)
{
Debug.LogWarning($"The sample groups for test {oldResult.Name} do not match between the two runs. This is not supported.");
continue;
}
int n = oldResult.SampleGroups.Count;
for (int i = 0; i < n; i++)
{
var newSamples = newResult.SampleGroups[i];
var oldSamples = oldResult.SampleGroups[i];
if (newSamples.Name != oldSamples.Name || newSamples.IncreaseIsBetter != oldSamples.IncreaseIsBetter)
{
Debug.LogWarning($"The sample groups for test {oldResult.Name} do not match between the two runs. This is not supported.");
continue;
}
var testName = oldResult.Name;
if (n > 1)
testName += "_" + oldSamples.Name;
HandleSampleGroups(testName, newSamples, oldSamples, regressions, improvements, inconclusive);
}
}
}
private static void HandleSampleGroups(string testName, SampleGroup newSamples, SampleGroup oldSamples,
List<ChangeResult> regressions, List<ChangeResult> improvements, List<ChangeResult> inconclusive)
{
var samplesA = new List<double>();
var samplesB = new List<double>();
Debug.Assert(newSamples.Unit == oldSamples.Unit, "The two samples are using different units. You are the unlucky person to hit this case, so please add support for it.");
var allSamples = newSamples.Samples.Concat(oldSamples.Samples);
var c = new ChangeResult
{
Name = testName,
Histogram = new HistogramData
{
X = newSamples,
Y = oldSamples,
Max = allSamples.Max(),
Min = allSamples.Min()
}
};
if (newSamples.Samples.Count <= 1 || oldSamples.Samples.Count <= 1)
{
inconclusive.Add(c);
c.Test.p = 1;
c.Test.IntervalCenter = 0;
c.Test.IntervalHalfWidth = double.PositiveInfinity;
return;
}
samplesA.Clear();
samplesA.AddRange(newSamples.Samples);
samplesB.Clear();
samplesB.AddRange(oldSamples.Samples);
StatisticsUtility.RemoveTopOutliers(samplesA);
StatisticsUtility.RemoveTopOutliers(samplesB);
var t = StatisticsUtility.WelchTTest(samplesA, samplesB, StatisticsUtility.ConfidenceLevel.Alpha1);
c.Test = t;
// insignificant or straddling zero?
if (t.p > 0.05 || t.IntervalLower < 0 && 0 < t.IntervalUpper)
{
inconclusive.Add(c);
return;
}
Debug.Assert(newSamples.IncreaseIsBetter == oldSamples.IncreaseIsBetter, $"The test {testName} was modified between runs, please collect a new baseline.");
bool increaseIsBetter = newSamples.IncreaseIsBetter;
double testValue = increaseIsBetter ? -t.IntervalLower : t.IntervalLower;
var interval = (c.LowerChangePercentage, c.UpperChangePercentage);
if (increaseIsBetter)
{
interval.LowerChangePercentage = -interval.LowerChangePercentage;
interval.UpperChangePercentage = -interval.UpperChangePercentage;
}
if (testValue > 0)
{
// Check that the minimum change is at least the given percentage OR
// the maximum change is above a certain threshold
if (interval.LowerChangePercentage >= PercentageCutOffRegressionMin || interval.UpperChangePercentage >= PercentageCutOffRegressionMax)
{
// regression
regressions.Add(c);
return;
}
}
else // t.IntervalUpper < 0
{
// Check that the minimum improvement is at least a given value
if (-interval.UpperChangePercentage >= PercentageCutOffImprovement)
{
// improvement
improvements.Add(c);
return;
}
}
inconclusive.Add(c);
}
private static Run LoadResults(string filePath)
{
if (!File.Exists(filePath))
return null;
var json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<Run>(json);
}
private void CopyChangesAsMarkdown() => MarkdownExportUtility.CopyAsMarkdown(_baselineData, _comparisonData, _regressions, _improvements, null);
private void CopyAllAsMarkdown() => MarkdownExportUtility.CopyAsMarkdown(_baselineData, _comparisonData, _regressions, _improvements, _inconclusive);
private void CopyComparisonAsCsv() => CsvExportUtility.CopyComparisonCsv(_allResults);
private void CopyNewResultsAsCsv() => CsvExportUtility.CopyNewResultsCsv(_allResults);
private void DropBaselineResults()
{
_baselineData = null;
try
{
File.Delete(BaselineDataPath);
}
finally
{
UpdateData();
}
}
private void DropComparisonResults()
{
_comparisonData = null;
try
{
File.Delete(ComparisonDataPath);
}
finally
{
UpdateData();
}
}
private void OpenReadMe()
{
var assetPath = AssetDatabase.GetAssetPath(_readMeAsset);
var path = Path.GetFullPath(assetPath);
EditorUtility.OpenWithDefaultApp(path);
}
void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
{
menu.AddItem(new GUIContent("Open ReadMe"), false, OpenReadMe);
menu.AddItem(new GUIContent("Copy performance changes as Markdown"), false, CopyChangesAsMarkdown);
menu.AddItem(new GUIContent("Copy all results as Markdown"), false, CopyAllAsMarkdown);
menu.AddItem(new GUIContent("Copy comparison data as CSV"), false, CopyComparisonAsCsv);
menu.AddItem(new GUIContent("Copy new results as CSV"), false, CopyNewResultsAsCsv);
menu.AddItem(new GUIContent("Drop baseline data"), false, DropBaselineResults);
menu.AddItem(new GUIContent("Drop comparison data"), false, DropComparisonResults);
}
static class CsvExportUtility
{
const char Separator = ';';
static void FormatSample(StringBuilder sb, SampleGroup sample)
{
sb.Append(sample.Average.ToString(CultureInfo.InvariantCulture));
sb.Append(Separator);
sb.Append(sample.Median.ToString(CultureInfo.InvariantCulture));
sb.Append(Separator);
sb.Append(sample.StandardDeviation.ToString(CultureInfo.InvariantCulture));
sb.Append(Separator);
sb.Append(sample.Samples.Count.ToString(CultureInfo.InvariantCulture));
}