Skip to content

Instantly share code, notes, and snippets.

@g0t4
Created September 20, 2012 21:27
Show Gist options
  • Save g0t4/3758460 to your computer and use it in GitHub Desktop.
Save g0t4/3758460 to your computer and use it in GitHub Desktop.
NotifyIfTakesMoreThan
// 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