aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Library/Profiler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Controller/Library/Profiler.cs')
-rw-r--r--MediaBrowser.Controller/Library/Profiler.cs69
1 files changed, 69 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Library/Profiler.cs b/MediaBrowser.Controller/Library/Profiler.cs
new file mode 100644
index 000000000..4daa9d654
--- /dev/null
+++ b/MediaBrowser.Controller/Library/Profiler.cs
@@ -0,0 +1,69 @@
+using System;
+using System.Diagnostics;
+using MediaBrowser.Common.Logging;
+
+namespace MediaBrowser.Controller.Library
+{
+ /// <summary>
+ /// Class Profiler
+ /// </summary>
+ public class Profiler : IDisposable
+ {
+ /// <summary>
+ /// The name
+ /// </summary>
+ readonly string name;
+ /// <summary>
+ /// The stopwatch
+ /// </summary>
+ readonly Stopwatch stopwatch;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Profiler" /> class.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ public Profiler(string name)
+ {
+ this.name = name;
+
+ stopwatch = new Stopwatch();
+ stopwatch.Start();
+ }
+ #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();
+ string message;
+ if (stopwatch.ElapsedMilliseconds > 300000)
+ {
+ message = string.Format("{0} took {1} minutes.",
+ name, ((float)stopwatch.ElapsedMilliseconds / 60000).ToString("F"));
+ }
+ else
+ {
+ message = string.Format("{0} took {1} seconds.",
+ name, ((float)stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000"));
+ }
+ Logger.LogInfo(message);
+ }
+ }
+
+ #endregion
+ }
+}