NotifyIfTakesMoreThan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sometimes it's easier to make assumptions about performance as a trade off for simplified code, but why not make it easy to notify us when that assumption is violated? Next step might be StopAndNotify | |
// Usage | |
using (new NotifyIfTakesMoreThan(TimeSpan.FromMinutes(5), "XYZ Import - checking all data instead of more complex code to optimize it")) | |
{ | |
//operation | |
} | |
// Implementation | |
public class NotifyIfTakesMoreThan : IDisposable | |
{ | |
private readonly TimeSpan _NotificationThreshold; | |
private readonly string _Description; | |
private readonly Stopwatch _StopWatch; | |
private static readonly ILog Log = LogManager.GetLogger(typeof (NotifyIfTakesMoreThan)); | |
public NotifyIfTakesMoreThan(TimeSpan notificationThreshold, string description) | |
{ | |
_NotificationThreshold = notificationThreshold; | |
_Description = description; | |
_StopWatch = new Stopwatch(); | |
_StopWatch.Start(); | |
} | |
public void Dispose() | |
{ | |
_StopWatch.Stop(); | |
if (_StopWatch.Elapsed > _NotificationThreshold) | |
{ | |
Log.Error("Operation exceeded threshold: " + new {_Description, _NotificationThreshold, _StopWatch.Elapsed}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment