Skip to content

Commit

Permalink
Merge pull request #638 from AlonTalmi/InitialResolveTimeDiagnostics
Browse files Browse the repository at this point in the history
Resolve time diagnostics
  • Loading branch information
hadashiA authored Mar 6, 2024
2 parents a562f6a + 689f8b3 commit 3eff392
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class DiagnosticsInfoTreeViewItem : TreeViewItem
public RegistrationBuilder RegistrationBuilder => DiagnosticsInfo.RegisterInfo.RegistrationBuilder;
public Registration Registration => DiagnosticsInfo.ResolveInfo.Registration;
public int? RefCount => DiagnosticsInfo.ResolveInfo.RefCount;
public long ResolveTime => DiagnosticsInfo.ResolveInfo.ResolveTime;

public string TypeSummary => TypeNameHelper.GetTypeAlias(Registration.ImplementationType);

Expand Down Expand Up @@ -85,6 +86,7 @@ public sealed class VContainerDiagnosticsInfoTreeView : TreeView
new MultiColumnHeaderState.Column { headerContent = new GUIContent("Register"), width = 15f },
new MultiColumnHeaderState.Column { headerContent = new GUIContent("RefCount"), width = 5f },
new MultiColumnHeaderState.Column { headerContent = new GUIContent("Scope"), width = 20f },
new MultiColumnHeaderState.Column { headerContent = new GUIContent("Time"), width = 20f },
};

static int idSeed;
Expand Down Expand Up @@ -246,6 +248,9 @@ protected override void RowGUI(RowGUIArgs args)
case 5:
EditorGUI.LabelField(cellRect, item.ScopeName, labelStyle);
break;
case 6:
EditorGUI.LabelField(cellRect, item.ResolveTime.ToString(), labelStyle);
break;
default:
throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, null);
}
Expand Down Expand Up @@ -295,6 +300,10 @@ IEnumerable<DiagnosticsInfoTreeViewItem> Sort(
return ascending
? items.OrderBy(x => x.ScopeName)
: items.OrderByDescending(x => x.ScopeName);
case 6:
return ascending
? items.OrderBy(x => x.ResolveTime)
: items.OrderByDescending(x => x.ResolveTime);
default:
throw new ArgumentOutOfRangeException(nameof(sortedColumnIndex), sortedColumnIndex, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using VContainer.Internal;

Expand Down Expand Up @@ -69,9 +70,13 @@ public object TraceResolve(Registration registration, Func<Registration, object>
owner?.Dependencies.Add(current);

resolveCallStack.Value.Push(current);
var watch = Stopwatch.StartNew();
var instance = resolving(registration);
watch.Stop();
resolveCallStack.Value.Pop();

SetResolveTime(current, watch.ElapsedMilliseconds);

if (!current.ResolveInfo.Instances.Contains(instance))
{
current.ResolveInfo.Instances.Add(instance);
Expand All @@ -82,6 +87,28 @@ public object TraceResolve(Registration registration, Func<Registration, object>
return resolving(registration);
}

private static void SetResolveTime(DiagnosticsInfo current, long elapsedMilliseconds)
{
var resolves = current.ResolveInfo.RefCount;
var resolveTime = current.ResolveInfo.ResolveTime;

switch (current.ResolveInfo.Registration.Lifetime)
{
case Lifetime.Transient:
resolveTime = (resolveTime * (resolves - 1) + elapsedMilliseconds) / resolves;
break;
case Lifetime.Scoped:
case Lifetime.Singleton:
if (elapsedMilliseconds > resolveTime)
resolveTime = elapsedMilliseconds;
break;
default:
throw new ArgumentOutOfRangeException();
}

current.ResolveInfo.ResolveTime = resolveTime;
}

public void NotifyContainerBuilt(IObjectResolver container)
=> DiagnositcsContext.NotifyContainerBuilt(container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public sealed class ResolveInfo
public List<object> Instances { get; } = new List<object>();
public int MaxDepth { get; set; } = -1;
public int RefCount { get; set; }
public long ResolveTime { get; set; }

public ResolveInfo(Registration registration)
{
Expand Down

0 comments on commit 3eff392

Please sign in to comment.