2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.Diagnostics;
|
2020-08-07 17:26:28 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:25:32 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class Profiler.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
public class Profiler : IDisposable
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The name.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
readonly string _name;
|
2020-08-07 17:26:28 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The stopwatch.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
readonly Stopwatch _stopwatch;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The _logger.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<Profiler> _logger;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Profiler" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
2020-03-03 22:07:10 +00:00
|
|
|
public Profiler(string name, ILogger<Profiler> logger)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
this._name = name;
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
_stopwatch = new Stopwatch();
|
|
|
|
_stopwatch.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2020-08-07 17:26:28 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
string message;
|
|
|
|
if (_stopwatch.ElapsedMilliseconds > 300000)
|
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
message = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"{0} took {1} minutes.",
|
|
|
|
_name,
|
|
|
|
((float)_stopwatch.ElapsedMilliseconds / 60000).ToString("F", CultureInfo.InvariantCulture));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
message = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"{0} took {1} seconds.",
|
|
|
|
_name,
|
|
|
|
((float)_stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000", CultureInfo.InvariantCulture));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation(message);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|