mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 12:07:05 +00:00
feat: Util methods for profiling event subscribers
This commit is contained in:
parent
25ebb4b43d
commit
073c231ef1
1 changed files with 52 additions and 0 deletions
|
|
@ -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)
|
private static void PrintProfile(IcdStopwatch stopwatch, string name)
|
||||||
{
|
{
|
||||||
long elapsed = stopwatch.ElapsedMilliseconds;
|
long elapsed = stopwatch.ElapsedMilliseconds;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue