feat: Util methods for profiling event subscribers

This commit is contained in:
Chris Cameron
2018-07-18 21:34:51 -04:00
parent 25ebb4b43d
commit 073c231ef1

View File

@@ -146,6 +146,58 @@ namespace ICD.Common.Utils.Timers
}
}
/// <summary>
/// Executes the event handler and profiles each of the attached subscribers.
/// </summary>
/// <param name="eventHandler"></param>
/// <param name="sender"></param>
/// <param name="name"></param>
public static void Profile(EventHandler eventHandler, object sender, string name)
{
if (eventHandler == null)
{
PrintProfile(new IcdStopwatch(), string.Format("{0} - No invocations", name));
return;
}
// ReSharper disable PossibleInvalidCastExceptionInForeachLoop
foreach (EventHandler subscriber in eventHandler.GetInvocationList())
// ReSharper restore PossibleInvalidCastExceptionInForeachLoop
{
string subscriberName = string.Format("{0} - {1}", name, subscriber.Target.GetType().Name);
EventHandler subscriber1 = subscriber;
Profile(() => subscriber1(sender, EventArgs.Empty), subscriberName);
}
}
/// <summary>
/// Executes the event handler and profiles each of the attached subscribers.
/// </summary>
/// <param name="eventHandler"></param>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
/// <param name="name"></param>
public static void Profile<T>(EventHandler<T> eventHandler, object sender, T eventArgs, string name)
where T : EventArgs
{
if (eventHandler == null)
{
PrintProfile(new IcdStopwatch(), string.Format("{0} - No invocations", name));
return;
}
// ReSharper disable PossibleInvalidCastExceptionInForeachLoop
foreach (EventHandler<T> subscriber in eventHandler.GetInvocationList())
// ReSharper restore PossibleInvalidCastExceptionInForeachLoop
{
string subscriberName = string.Format("{0} - {1}", name, subscriber.Target.GetType().Name);
EventHandler<T> subscriber1 = subscriber;
Profile(() => subscriber1(sender, eventArgs), subscriberName);
}
}
private static void PrintProfile(IcdStopwatch stopwatch, string name)
{
long elapsed = stopwatch.ElapsedMilliseconds;