Profiler.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Diagnostics;
  3. using MediaBrowser.Common.Logging;
  4. namespace MediaBrowser.Controller.Library
  5. {
  6. /// <summary>
  7. /// Class Profiler
  8. /// </summary>
  9. public class Profiler : IDisposable
  10. {
  11. /// <summary>
  12. /// The name
  13. /// </summary>
  14. readonly string name;
  15. /// <summary>
  16. /// The stopwatch
  17. /// </summary>
  18. readonly Stopwatch stopwatch;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="Profiler" /> class.
  21. /// </summary>
  22. /// <param name="name">The name.</param>
  23. public Profiler(string name)
  24. {
  25. this.name = name;
  26. stopwatch = new Stopwatch();
  27. stopwatch.Start();
  28. }
  29. #region IDisposable Members
  30. /// <summary>
  31. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  32. /// </summary>
  33. public void Dispose()
  34. {
  35. Dispose(true);
  36. GC.SuppressFinalize(this);
  37. }
  38. /// <summary>
  39. /// Releases unmanaged and - optionally - managed resources.
  40. /// </summary>
  41. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  42. protected virtual void Dispose(bool dispose)
  43. {
  44. if (dispose)
  45. {
  46. stopwatch.Stop();
  47. string message;
  48. if (stopwatch.ElapsedMilliseconds > 300000)
  49. {
  50. message = string.Format("{0} took {1} minutes.",
  51. name, ((float)stopwatch.ElapsedMilliseconds / 60000).ToString("F"));
  52. }
  53. else
  54. {
  55. message = string.Format("{0} took {1} seconds.",
  56. name, ((float)stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000"));
  57. }
  58. Logger.LogInfo(message);
  59. }
  60. }
  61. #endregion
  62. }
  63. }