forked from steve3003/unity-profiler-data-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfilerDataExporter.cs
189 lines (167 loc) · 6.91 KB
/
ProfilerDataExporter.cs
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
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Linq;
using ByteSizeLib;
namespace ProfilerDataExporter
{
public class ProfilerDataExporter : EditorWindow
{
private static readonly ProfilerColumn[] ColumnsToShow = new ProfilerColumn[]
{
ProfilerColumn.FunctionName,
ProfilerColumn.TotalPercent,
ProfilerColumn.SelfPercent,
ProfilerColumn.Calls,
ProfilerColumn.GCMemory,
ProfilerColumn.TotalTime,
ProfilerColumn.SelfTime,
};
private static readonly Dictionary<ProfilerColumn, string> ColumnHeaders = new Dictionary<ProfilerColumn, string>
{
{ ProfilerColumn.FunctionName, "Function"},
{ ProfilerColumn.TotalPercent, "Total"},
{ ProfilerColumn.SelfPercent, "Self"},
{ ProfilerColumn.Calls, "Calls"},
{ ProfilerColumn.GCMemory, "GC Alloc"},
{ ProfilerColumn.TotalTime, "Time ms"},
{ ProfilerColumn.SelfTime, "Self ms"},
};
private string filePath = @"profiler_data.json";
private Type profilerWindowType;
private FieldInfo currentFrameFieldInfo;
private Vector2 scrollPosition;
private string[][] functionStats;
private FunctionTableState functionStatsTableState;
private StatsType statsType;
private EditorWindow profilerWindow;
[MenuItem("Window/Profiler Data Exporter")]
private static void Init()
{
var window = GetWindow<ProfilerDataExporter>("Profiler Data Exporter");
window.Show();
}
private void OnEnable()
{
Assembly assem = typeof(Editor).Assembly;
profilerWindowType = assem.GetType("UnityEditor.ProfilerWindow");
currentFrameFieldInfo = profilerWindowType.GetField("m_CurrentFrame", BindingFlags.NonPublic | BindingFlags.Instance);
}
private void OnGUI()
{
if (!profilerWindow)
{
profilerWindow = GetWindow(profilerWindowType);
}
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
DrawStats();
DrawFilePath();
DrawExportButtons();
EditorGUILayout.EndScrollView();
}
private void DrawStats()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Statistics", EditorStyles.boldLabel, GUILayout.ExpandWidth(false));
statsType = (StatsType)EditorGUILayout.EnumPopup(statsType, GUILayout.ExpandWidth(false));
var calulateStatistics = GUILayout.Button("Calculate", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
if (calulateStatistics)
{
var statsCalculator = StatsCalculatorProvider.GetStatsCalculator(statsType);
var stats = statsCalculator.CalculateStats(ColumnsToShow);
functionStats = stats.Select<FunctionData, string[]>(f => ColumnsToShow.Select<ProfilerColumn, string>(f.GetValue).ToArray()).ToArray();
}
if (functionStatsTableState == null)
{
functionStatsTableState = new FunctionTableState(ColumnsToShow, ColumnHeaders);
}
if (functionStats != null)
{
TableGUILayout.BeginTable(functionStatsTableState, GUI.skin.GetStyle("OL Box"), GUILayout.MinHeight(100f), GUILayout.MaxHeight(500f));
for (var i = 0; i < functionStats.Length; ++i)
{
var functionData = functionStats[i];
TableGUILayout.AddRow(functionStatsTableState, i, functionData);
}
TableGUILayout.EndTable();
}
}
private void DrawExportButtons()
{
GUILayout.Label("Export", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Profiler Data"))
{
ExportProfilerData();
}
var currentframe = (int)currentFrameFieldInfo.GetValue(profilerWindow);
GUI.enabled = currentframe > 0;
if (GUILayout.Button("Current Frame Data"))
{
ExportCurrentFrameData(currentframe);
}
GUI.enabled = true;
GUI.enabled = !string.IsNullOrEmpty(ProfilerDriver.selectedPropertyPath);
if (GUILayout.Button("Selected Function Data"))
{
ExportSelectedFunctionData();
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
private void DrawFilePath()
{
GUILayout.Label("File path", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
var currentDirectory = Directory.GetCurrentDirectory();
var displayedFilePath = Path.GetFullPath(filePath).Replace(currentDirectory, "");
GUILayout.Label(displayedFilePath, GUILayout.ExpandWidth(false));
if (GUILayout.Button("Open", GUILayout.ExpandWidth(false)))
{
Process.Start(filePath);
}
if (GUILayout.Button("Edit", GUILayout.ExpandWidth(false)))
{
var currentPathDirectory = Path.GetDirectoryName(filePath);
var currentName = Path.GetFileNameWithoutExtension(filePath);
var newfilePath = EditorUtility.SaveFilePanel("Select file path", currentPathDirectory, currentName, "json");
var isPathValid = !string.IsNullOrEmpty(newfilePath);
if (isPathValid)
{
filePath = newfilePath;
}
}
GUILayout.EndHorizontal();
}
private void ExportProfilerData()
{
var firstFrameIndex = ProfilerDriver.firstFrameIndex;
var lastFrameIndex = ProfilerDriver.lastFrameIndex;
ExtractData(firstFrameIndex, lastFrameIndex);
}
private void ExportCurrentFrameData(int currentframe)
{
if (currentframe > -1)
{
ExtractData(currentframe, currentframe);
}
}
private void ExportSelectedFunctionData()
{
var firstFrameIndex = ProfilerDriver.firstFrameIndex;
var lastFrameIndex = ProfilerDriver.lastFrameIndex;
ExtractData(firstFrameIndex, lastFrameIndex, ProfilerDriver.selectedPropertyPath);
}
private void ExtractData(int firstFrameIndex, int lastFrameIndex, string selectedPropertyPath = "")
{
var profilerData = ProfilerData.GetProfilerData(firstFrameIndex, lastFrameIndex, selectedPropertyPath);
File.WriteAllText(filePath, profilerData.ToString());
}
}
}