1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System.Collections.Generic;
24: using System.Text;
25: using Utilities.DataTypes.Patterns.BaseClasses;
26: #endregion
27:
28: namespace Utilities.Profiler
29: {
30: /// <summary>
31: /// Actual location that profiler information is stored
32: /// </summary>
33: public class ProfilerManager : Singleton<ProfilerManager>
34: {
35: #region Constuctors
36:
37: protected ProfilerManager()
38: {
39: Profilers = new List<ProfilerInfo>();
40: }
41:
42: #endregion
43:
44: #region Public Functions
45:
46: /// <summary>
47: /// Adds an item to the manager (used by Profiler class)
48: /// </summary>
49: /// <param name="FunctionName">Function name/identifier</param>
50: /// <param name="StartTime">Start time (in ms)</param>
51: /// <param name="StopTime">Stop time (in ms)</param>
52: public void AddItem(string FunctionName, int StartTime, int StopTime)
53: {
54: ProfilerInfo Profiler = Profilers.Find(x => x.FunctionName == FunctionName);
55: if (Profiler != null)
56: {
57: int Time = (StopTime - StartTime);
58: Profiler.TotalTime += Time;
59: if (Profiler.MaxTime < Time)
60: Profiler.MaxTime = Time;
61: else if (Profiler.MinTime > Time)
62: Profiler.MinTime = Time;
63: ++Profiler.TimesCalled;
64: return;
65: }
66: ProfilerInfo Info = new ProfilerInfo();
67: Info.FunctionName = FunctionName;
68: Info.TotalTime = Info.MaxTime = Info.MinTime = StopTime - StartTime;
69: Info.TimesCalled = 1;
70: Profilers.Add(Info);
71: }
72:
73: /// <summary>
74: /// Outputs the information to a table
75: /// </summary>
76: /// <returns>an html string containing the information</returns>
77: public override string ToString()
78: {
79: StringBuilder Builder = new StringBuilder();
80: Builder.Append("<table><tr><th>Function Name</th><th>Total Time</th><th>Max Time</th><th>Min Time</th><th>Average Time</th><th>Times Called</th></tr>");
81: foreach (ProfilerInfo Info in Profilers)
82: {
83: Builder.Append("<tr><td>").Append(Info.FunctionName).Append("</td><td>")
84: .Append(Info.TotalTime.ToString()).Append("</td><td>").Append(Info.MaxTime)
85: .Append("</td><td>").Append(Info.MinTime).Append("</td><td>")
86: .Append(((double)Info.TotalTime / (double)Info.TimesCalled)).Append("</td><td>")
87: .Append(Info.TimesCalled).Append("</td></tr>");
88: }
89: Builder.Append("</table>");
90: return Builder.ToString();
91: }
92:
93: #endregion
94:
95: #region Private Properties
96: private List<ProfilerInfo> Profilers { get; set; }
97: #endregion
98:
99: #region Private Classes
100:
101: /// <summary>
102: /// Holds the profiler information
103: /// </summary>
104: private class ProfilerInfo
105: {
106: public string FunctionName;
107: public int TotalTime;
108: public int TimesCalled;
109: public int MaxTime;
110: public int MinTime;
111: }
112:
113: #endregion
114: }
115: }