How can I profile domain reload times in Unity 6?
EDIT: Not sure how valid this is but seems to work:
using UnityEditor;
using System.Diagnostics;
[InitializeOnLoad]
public class DomainReloadProfiler
{
private static readonly Stopwatch s_stopwatch;
static DomainReloadProfiler()
{
// Start timing on domain reload start
s_stopwatch = new Stopwatch();
s_stopwatch.Start();
// Listen for the first editor update after reload
EditorApplication.update += OnDomainReloadCompleted;
}
private static void OnDomainReloadCompleted()
{
// Stop timing when the first update occurs after reload
s_stopwatch.Stop();
UnityEngine.Debug.Log($"Domain reload time: {s_stopwatch.ElapsedMilliseconds} ms");
// Unsubscribe from the update event to avoid repeat logging
EditorApplication.update -= OnDomainReloadCompleted;
}
}
That’s not when domain reload ends!
Use AssemblyReloadEvents instead.
And consider using the profiler too. You could use the reload events to set profiler markers if the Profiler doesn’t already do so.
Thanks.
Current iteration:
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class DomainReloadProfiler
{
private const string ReloadStartTimeKey = "DomainReloadProfiler.StartTime";
static DomainReloadProfiler()
{
// Subscribe to assembly reload events
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
}
private static void OnBeforeAssemblyReload()
{
// Store the current time in EditorPrefs before the reload
EditorPrefs.SetString(ReloadStartTimeKey, System.DateTime.UtcNow.Ticks.ToString());
}
private static void OnAfterAssemblyReload()
{
// Retrieve the start time after the reload
if (EditorPrefs.HasKey(ReloadStartTimeKey))
{
long startTimeTicks = long.Parse(EditorPrefs.GetString(ReloadStartTimeKey));
var startTime = new System.DateTime(startTimeTicks, System.DateTimeKind.Utc);
double elapsedMilliseconds = (System.DateTime.UtcNow - startTime).TotalMilliseconds;
Debug.Log($"Domain reload time: {elapsedMilliseconds} ms");
// Remove the stored start time from EditorPrefs
EditorPrefs.DeleteKey(ReloadStartTimeKey);
}
else
{
Debug.LogWarning("Domain reload start time was not found.");
}
}
}
1 Like
That’s almost there, except you should use StopWatch not DateTime as it is more precise. Though it may not make much of a difference since you are likely profiling not ms but seconds.
1 Like
And final one. Thank you for the help.
using UnityEditor;
using System.Diagnostics;
[InitializeOnLoad]
public class DomainReloadProfiler
{
private const string ReloadStartTimeKey = "DomainReloadProfiler.StartTime";
static DomainReloadProfiler()
{
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
}
private static void OnBeforeAssemblyReload()
{
long startTimestamp = Stopwatch.GetTimestamp();
EditorPrefs.SetString(ReloadStartTimeKey, startTimestamp.ToString());
}
private static void OnAfterAssemblyReload()
{
if (EditorPrefs.HasKey(ReloadStartTimeKey))
{
long startTimestamp = long.Parse(EditorPrefs.GetString(ReloadStartTimeKey));
long endTimestamp = Stopwatch.GetTimestamp();
long elapsedTicks = endTimestamp - startTimestamp;
double elapsedSeconds = (double)elapsedTicks / Stopwatch.Frequency;
UnityEngine.Debug.Log($"Domain reload time: {elapsedSeconds:F2} seconds");
EditorPrefs.DeleteKey(ReloadStartTimeKey);
}
else
{
UnityEngine.Debug.LogWarning("Domain reload start time was not found.");
}
}
}
You don’t need to profile it, all the information on how long each part takes gets logged in your editor.log.
1 Like
Way quicker to see a quick console message than performing a couple clicks and then searching this massive text file for domain reload timings and then repeat that manual work multiple times:

Results appear to match so that’s great. Unity used to have a package for this but it appears unmaintained for some time now.
I agree, for monitoring a custom script is great. I used to have a script that kept a record of the last n domain reload times over the past couple days. So that every time there was a significant increase in domain reload times (say +20%) I would log the message in orange.
This would tell me that possibly someone did something recently that may cause everyone to suffer from longer domain reloads, and then I would investigate.
Usually those longer domain reloads are a creeping process, like the frog in the pot and the water starts boiling and he’ll just be fine with it until it’s too late to hop out.
3 Likes