Fixed Issue 382.
2 using System.Collections.Generic;
4 using System.Diagnostics;
9 /// Is used to analyze code performance
11 public static class PerformanceAnalyzer
13 public class PerformanceInfo
21 private int _count = 0;
24 get { return _count; }
25 set { _count = value; }
28 private double _totalTime = 0;
29 public double TotalTime
31 get { return _totalTime; }
32 set { _totalTime = value; }
38 get { return _start; }
39 set { _start = value; }
42 public PerformanceInfo(string name)
48 private static Dictionary<string, PerformanceInfo> _performances = new Dictionary<string, PerformanceInfo>();
50 public static IEnumerable<PerformanceInfo> Performances
54 return _performances.Values;
58 [Conditional("DEBUG")]
59 public static void Start(string pieceOfCode)
61 PerformanceInfo info = null;
64 if (_performances.ContainsKey(pieceOfCode))
65 info = _performances[pieceOfCode];
68 info = new PerformanceInfo(pieceOfCode);
69 _performances.Add(pieceOfCode, info);
73 info.Start = TimeCounter.GetStartValue();
77 [Conditional("DEBUG")]
78 public static void Finish(string pieceOfCode)
82 if (_performances.ContainsKey(pieceOfCode))
84 PerformanceInfo info = _performances[pieceOfCode];
86 info.TotalTime += TimeCounter.Finish(info.Start);
91 public static void Reset()
93 _performances.Clear();
96 public static string GenerateReport()
98 return GenerateReport(0);
101 public static string GenerateReport(string mainPieceOfCode)
103 if (_performances.ContainsKey(mainPieceOfCode))
104 return GenerateReport(_performances[mainPieceOfCode].TotalTime);
106 return GenerateReport(0);
109 public static string GenerateReport(double totalTime)
111 StringBuilder sb = new StringBuilder();
113 foreach (PerformanceInfo info in Performances)
114 len = Math.Max(info.Name.Length, len);
116 sb.AppendLine("Name".PadRight(len) + " Count Total Time, ms Avg. Time, ms Percentage, %");
117 sb.AppendLine("----------------------------------------------------------------------------------------------");
118 foreach (PerformanceInfo info in Performances)
120 sb.Append(info.Name.PadRight(len));
124 p = info.TotalTime / totalTime;
126 avgt = info.TotalTime * 1000 / info.Count;
127 string c = info.Count.ToString("0,0").PadRight(20);
128 string tt = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20);
129 string t = avgt.ToString("0.0000").PadRight(20);
130 string sp = (p * 100).ToString("###").PadRight(20);
131 sb.AppendFormat(" " + c + tt + t + sp + "\n");
133 return sb.ToString();