Skip to content

Commit

Permalink
Use average time for transient resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonTalmi committed Feb 29, 2024
1 parent f633c41 commit 689f8b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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 InitialResolveTime => DiagnosticsInfo.ResolveInfo.InitialResolveTime;
public long ResolveTime => DiagnosticsInfo.ResolveInfo.ResolveTime;

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

Expand Down Expand Up @@ -249,7 +249,7 @@ protected override void RowGUI(RowGUIArgs args)
EditorGUI.LabelField(cellRect, item.ScopeName, labelStyle);
break;
case 6:
EditorGUI.LabelField(cellRect, item.InitialResolveTime.ToString(), labelStyle);
EditorGUI.LabelField(cellRect, item.ResolveTime.ToString(), labelStyle);
break;
default:
throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, null);
Expand Down Expand Up @@ -302,8 +302,8 @@ IEnumerable<DiagnosticsInfoTreeViewItem> Sort(
: items.OrderByDescending(x => x.ScopeName);
case 6:
return ascending
? items.OrderBy(x => x.InitialResolveTime)
: items.OrderByDescending(x => x.InitialResolveTime);
? 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
Expand Up @@ -75,9 +75,10 @@ public object TraceResolve(Registration registration, Func<Registration, object>
watch.Stop();
resolveCallStack.Value.Pop();

SetResolveTime(current, watch.ElapsedMilliseconds);

if (!current.ResolveInfo.Instances.Contains(instance))
{
current.ResolveInfo.InitialResolveTime = watch.ElapsedMilliseconds;
current.ResolveInfo.Instances.Add(instance);
}

Expand All @@ -86,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,7 +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 InitialResolveTime { get; set; }
public long ResolveTime { get; set; }

public ResolveInfo(Registration registration)
{
Expand Down

0 comments on commit 689f8b3

Please sign in to comment.