jellyfin/MediaBrowser.Controller/Library/Profiler.cs

78 lines
2.3 KiB
C#
Raw Normal View History

using MediaBrowser.Model.Logging;
using System;
2013-02-20 18:33:05 -07:00
using System.Diagnostics;
namespace MediaBrowser.Controller.Library
{
/// <summary>
/// Class Profiler
/// </summary>
public class Profiler : IDisposable
{
/// <summary>
/// The name
/// </summary>
readonly string _name;
2013-02-20 18:33:05 -07:00
/// <summary>
/// The stopwatch
/// </summary>
readonly Stopwatch _stopwatch;
2013-02-20 18:33:05 -07:00
2013-02-21 13:26:35 -07:00
/// <summary>
/// The _logger
/// </summary>
private readonly ILogger _logger;
2013-02-21 13:26:35 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="Profiler" /> class.
/// </summary>
/// <param name="name">The name.</param>
2013-02-21 13:26:35 -07:00
/// <param name="logger">The logger.</param>
public Profiler(string name, ILogger logger)
2013-02-20 18:33:05 -07:00
{
this._name = name;
2013-02-20 18:33:05 -07:00
2013-02-21 13:26:35 -07:00
_logger = logger;
_stopwatch = new Stopwatch();
_stopwatch.Start();
2013-02-20 18:33:05 -07:00
}
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
_stopwatch.Stop();
2013-02-20 18:33:05 -07:00
string message;
if (_stopwatch.ElapsedMilliseconds > 300000)
2013-02-20 18:33:05 -07:00
{
message = string.Format("{0} took {1} minutes.",
_name, ((float)_stopwatch.ElapsedMilliseconds / 60000).ToString("F"));
2013-02-20 18:33:05 -07:00
}
else
{
message = string.Format("{0} took {1} seconds.",
_name, ((float)_stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000"));
2013-02-20 18:33:05 -07:00
}
2013-02-21 13:26:35 -07:00
_logger.Info(message);
2013-02-20 18:33:05 -07:00
}
}
#endregion
}
}