aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Library/Profiler.cs
blob: 3ac53f62aaedd160efcc99e62e08742a9c8b2f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Diagnostics;
using MediaBrowser.Common.Logging;
using MediaBrowser.Model.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>
        /// The _logger
        /// </summary>
        private ILogger _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="Profiler" /> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="logger">The logger.</param>
        public Profiler(string name, ILogger logger)
        {
            this.name = name;

            _logger = logger;

            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.Info(message);
            }
        }

        #endregion
    }
}