Skip to content

Commit e783c01

Browse files
committed
First asmdef debug iteration
0 parents  commit e783c01

14 files changed

+353
-0
lines changed

AsmdefDebug.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using AssemblyDebugger;
6+
using UnityEditor;
7+
using UnityEditor.Compilation;
8+
using UnityEngine;
9+
10+
11+
// Based on https://gist.github.com/karljj1/9c6cce803096b5cd4511cf0819ff517b
12+
[InitializeOnLoad]
13+
public class AsmdefDebug
14+
{
15+
internal const string CompilationReportEditorPref = "CompilationReportKey";
16+
internal const string LogEnabledPref = "AsmdefDebugLogKey";
17+
18+
private static readonly int ScriptAssembliesPathLen = "Library/ScriptAssemblies/".Length;
19+
private static readonly CompilationReport CompilationReport = new CompilationReport();
20+
private static readonly Dictionary<string, DateTime> StartTimes = new Dictionary<string, DateTime>();
21+
internal static TimeSpan AssemblyReloadTime { get; private set; }
22+
23+
private static double compilationTotalTime;
24+
25+
#if !IGNORE_ASMDEF_DEBUG
26+
static AsmdefDebug()
27+
{
28+
CompilationPipeline.assemblyCompilationStarted += CompilationPipelineOnAssemblyCompilationStarted;
29+
CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished;
30+
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEventsOnBeforeAssemblyReload;
31+
AssemblyReloadEvents.afterAssemblyReload += AssemblyReloadEventsOnAfterAssemblyReload;
32+
}
33+
#endif
34+
35+
private static void CompilationPipelineOnAssemblyCompilationStarted(string assembly)
36+
{
37+
StartTimes[assembly] = DateTime.UtcNow;
38+
}
39+
40+
private static void CompilationPipelineOnAssemblyCompilationFinished(string assembly, CompilerMessage[] arg2)
41+
{
42+
var timeSpan = DateTime.UtcNow - StartTimes[assembly];
43+
compilationTotalTime += timeSpan.TotalMilliseconds;
44+
var assemblyTime = (timeSpan.TotalMilliseconds / 1000);
45+
var assemblyName = assembly.Substring(ScriptAssembliesPathLen, assembly.Length - ScriptAssembliesPathLen);
46+
CompilationReport.assemblyCompilations.Add(new AssemblyCompilation(assemblyName, assemblyTime));
47+
}
48+
49+
private static void AssemblyReloadEventsOnBeforeAssemblyReload()
50+
{
51+
var totalCompilationTimeSeconds = compilationTotalTime / 1000f;
52+
CompilationReport.compilationTotalTime = totalCompilationTimeSeconds;
53+
CompilationReport.reloadEventTimes = DateTime.UtcNow.ToBinary();
54+
EditorPrefs.SetString(CompilationReportEditorPref, JsonUtility.ToJson(CompilationReport));
55+
}
56+
57+
private static void AssemblyReloadEventsOnAfterAssemblyReload()
58+
{
59+
var reportJson = EditorPrefs.GetString(CompilationReportEditorPref);
60+
if (string.IsNullOrEmpty(reportJson))
61+
{
62+
return;
63+
}
64+
65+
var report = JsonUtility.FromJson<CompilationReport>(reportJson);
66+
67+
var date = DateTime.FromBinary(report.reloadEventTimes);
68+
AssemblyReloadTime = DateTime.UtcNow - date;
69+
70+
if (!EditorPrefs.GetBool(LogEnabledPref, true))
71+
{
72+
return;
73+
}
74+
75+
var totalTimeSeconds = report.compilationTotalTime + AssemblyReloadTime.TotalSeconds;
76+
if (report.assemblyCompilations != null && report.assemblyCompilations.Any())
77+
{
78+
var builder = new StringBuilder();
79+
builder.AppendLine($"Compilation Report: {totalTimeSeconds:F2} seconds");
80+
var orderedCompilations = report.assemblyCompilations.OrderBy(x => x.compilationTime).Reverse();
81+
foreach (var assCom in orderedCompilations)
82+
{
83+
builder.AppendFormat("{0:0.00}s {1}\n", assCom.compilationTime, assCom.assemblyName);
84+
}
85+
86+
builder.AppendFormat("Assembly Reload Time: {0}\n", AssemblyReloadTime.TotalSeconds);
87+
88+
Debug.Log(builder.ToString());
89+
}
90+
}
91+
}

AsmdefDebug.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AsmdefDebugWindow.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Linq;
3+
using UnityEditor;
4+
using UnityEditor.Callbacks;
5+
using UnityEngine;
6+
7+
namespace AssemblyDebugger
8+
{
9+
public class AsmdefDebugWindow : EditorWindow
10+
{
11+
private static string reportJson;
12+
private static CompilationReport report;
13+
private bool logEnabled;
14+
15+
[MenuItem("Window/Assemblies Debugger")]
16+
private static void Init()
17+
{
18+
var window = (AsmdefDebugWindow) GetWindow(typeof(AsmdefDebugWindow), false, "Assembly Debugger");
19+
window.Show();
20+
}
21+
22+
#if !IGNORE_ASMDEF_DEBUG
23+
[DidReloadScripts]
24+
#endif
25+
private static void OnReload()
26+
{
27+
reportJson = EditorPrefs.GetString(AsmdefDebug.CompilationReportEditorPref);
28+
}
29+
30+
private static CompilationReport GenerateReport(string json)
31+
{
32+
if (string.IsNullOrEmpty(json))
33+
{
34+
return null;
35+
}
36+
37+
return JsonUtility.FromJson<CompilationReport>(json);
38+
}
39+
40+
private void OnEnable()
41+
{
42+
logEnabled = EditorPrefs.GetBool(AsmdefDebug.LogEnabledPref, true);
43+
}
44+
45+
private void OnGUI()
46+
{
47+
if (report == null)
48+
{
49+
report = GenerateReport(reportJson);
50+
}
51+
52+
if (report == null)
53+
{
54+
EditorGUILayout.HelpBox(
55+
"No compilation report found. Modify a script to trigger a recompilation", MessageType.Warning);
56+
return;
57+
}
58+
59+
GUILayout.Label("Post Compilation Report", EditorStyles.boldLabel);
60+
61+
var date = DateTime.FromBinary(report.reloadEventTimes);
62+
var totalTimeSeconds = report.compilationTotalTime + AsmdefDebug.AssemblyReloadTime.TotalSeconds;
63+
64+
EditorGUILayout.TextField("Compilation Report", $"{totalTimeSeconds:F2} seconds", EditorStyles.boldLabel);
65+
var orderedCompilations = report.assemblyCompilations.OrderBy(x => x.compilationTime).Reverse();
66+
foreach (var assCom in orderedCompilations)
67+
{
68+
EditorGUILayout.TextField(assCom.assemblyName, $"{assCom.compilationTime:0.00}s", EditorStyles.label);
69+
}
70+
71+
EditorGUILayout.FloatField("Assembly Reload Time", (float) AsmdefDebug.AssemblyReloadTime.TotalSeconds,
72+
EditorStyles.boldLabel);
73+
74+
75+
GUILayout.Label("Print compilation time after reload", EditorStyles.boldLabel);
76+
var enableLog = EditorGUILayout.Toggle("Use Debug.Log", logEnabled);
77+
78+
if (logEnabled != enableLog)
79+
{
80+
EditorPrefs.SetBool(AsmdefDebug.LogEnabledPref, enableLog);
81+
logEnabled = enableLog;
82+
}
83+
}
84+
}
85+
}

AsmdefDebugWindow.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assembly.Debugger.asmdef

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Assembly.Debugger",
3+
"references": [],
4+
"optionalUnityReferences": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": []
14+
}

Assembly.Debugger.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CompilationReport.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace AssemblyDebugger
5+
{
6+
[Serializable]
7+
internal class CompilationReport
8+
{
9+
public CompilationReport()
10+
{
11+
assemblyCompilations = new List<AssemblyCompilation>();
12+
}
13+
14+
public double compilationTotalTime;
15+
public List<AssemblyCompilation> assemblyCompilations;
16+
public long reloadEventTimes;
17+
}
18+
19+
[Serializable]
20+
internal class AssemblyCompilation
21+
{
22+
public AssemblyCompilation(string assemblyName, double compilationTime)
23+
{
24+
this.assemblyName = assemblyName;
25+
this.compilationTime = compilationTime;
26+
}
27+
28+
public double compilationTime;
29+
public string assemblyName;
30+
}
31+
}

CompilationReport.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [year] [fullname]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Unity Assemblies Debugger
2+
3+
Small utility to show the time it takes to compile each assembly after a reload.
4+
5+
## Usage
6+
7+
Just by installing this in your project it will load itself on every reload.
8+
9+
If you want to see the report, you can open the utility window at `Window > Assemblies Debugger`.
10+
11+
### Logs
12+
13+
By default it will use the console to print the total reloading time.
14+
15+
You can see an example log here:
16+
17+
```
18+
Compilation Report: 4.14 seconds
19+
0.62s Unity.TextMeshPro.dll
20+
0.44s Unity.Analytics.DataPrivacy.dll
21+
0.44s Unity.PackageManagerUI.Editor.dll
22+
0.43s Assembly.Debugger.dll
23+
0.42s Unity.CollabProxy.Editor.dll
24+
0.30s Unity.TextMeshPro.Editor.dll
25+
0.24s Assembly-CSharp.dll
26+
Assembly Reload Time: 1.2500003
27+
```
28+
29+
#### Disabling the logs
30+
31+
If you wish to disable the logs, you can set this in the checkbox in the utility window.
32+
33+
### Disabling whole plugin
34+
35+
If, for some reason, you want to disable the plugin without removing it, you can add the Scripting Define Symbol
36+
`IGNORE_ASMDEF_DEBUG` to your project and it will stop analyzing the reload times.

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "dev.bullrich.asmdef-debug",
3+
"version": "1.0.0",
4+
"displayName": "Asmdef Debug",
5+
"description": "Utility to debug the amount of time each assembly takes to reload",
6+
"unity": "2018.4",
7+
"unityRelease": "8f1",
8+
"keywords": [
9+
"asmdef",
10+
"debug",
11+
"unity"
12+
],
13+
"homepage": "https://github.com/Bullrich/Unity-Assembly-Debugger/",
14+
"bugs": {
15+
"url": "https://github.com/Bullrich/Unity-Assembly-Debugger/issues"
16+
},
17+
"repository": {
18+
"type": "git",
19+
"url": "ssh://[email protected]:Bullrich/Unity-Assembly-Debugger.git"
20+
},
21+
"license": "MIT",
22+
"author": {
23+
"name": "Javier Bullrich",
24+
"email": "[email protected]",
25+
"url": "https://bullrich.dev"
26+
},
27+
"files": [
28+
"*.md",
29+
"*.meta",
30+
"*.asmdef",
31+
"*.xml",
32+
"*.cs"
33+
]
34+
}

package.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)