moel@345: using System; moel@345: using System.Collections.Generic; moel@345: using System.Text; moel@345: using System.Diagnostics; moel@345: moel@345: namespace Aga.Controls moel@345: { moel@345: /// moel@345: /// Is used to analyze code performance moel@345: /// moel@345: public static class PerformanceAnalyzer moel@345: { moel@345: public class PerformanceInfo moel@345: { moel@345: private string _name; moel@345: public string Name moel@345: { moel@345: get { return _name; } moel@345: } moel@345: moel@345: private int _count = 0; moel@345: public int Count moel@345: { moel@345: get { return _count; } moel@345: set { _count = value; } moel@345: } moel@345: moel@345: private double _totalTime = 0; moel@345: public double TotalTime moel@345: { moel@345: get { return _totalTime; } moel@345: set { _totalTime = value; } moel@345: } moel@345: moel@345: private Int64 _start; moel@345: public Int64 Start moel@345: { moel@345: get { return _start; } moel@345: set { _start = value; } moel@345: } moel@345: moel@345: public PerformanceInfo(string name) moel@345: { moel@345: _name = name; moel@345: } moel@345: } moel@345: moel@345: private static Dictionary _performances = new Dictionary(); moel@345: moel@345: public static IEnumerable Performances moel@345: { moel@345: get moel@345: { moel@345: return _performances.Values; moel@345: } moel@345: } moel@345: moel@345: [Conditional("DEBUG")] moel@345: public static void Start(string pieceOfCode) moel@345: { moel@345: PerformanceInfo info = null; moel@345: lock(_performances) moel@345: { moel@345: if (_performances.ContainsKey(pieceOfCode)) moel@345: info = _performances[pieceOfCode]; moel@345: else moel@345: { moel@345: info = new PerformanceInfo(pieceOfCode); moel@345: _performances.Add(pieceOfCode, info); moel@345: } moel@345: moel@345: info.Count++; moel@345: info.Start = TimeCounter.GetStartValue(); moel@345: } moel@345: } moel@345: moel@345: [Conditional("DEBUG")] moel@345: public static void Finish(string pieceOfCode) moel@345: { moel@345: lock (_performances) moel@345: { moel@345: if (_performances.ContainsKey(pieceOfCode)) moel@345: { moel@345: PerformanceInfo info = _performances[pieceOfCode]; moel@345: info.Count++; moel@345: info.TotalTime += TimeCounter.Finish(info.Start); moel@345: } moel@345: } moel@345: } moel@345: moel@345: public static void Reset() moel@345: { moel@345: _performances.Clear(); moel@345: } moel@345: moel@345: public static string GenerateReport() moel@345: { moel@345: return GenerateReport(0); moel@345: } moel@345: moel@345: public static string GenerateReport(string mainPieceOfCode) moel@345: { moel@345: if (_performances.ContainsKey(mainPieceOfCode)) moel@345: return GenerateReport(_performances[mainPieceOfCode].TotalTime); moel@345: else moel@345: return GenerateReport(0); moel@345: } moel@345: moel@345: public static string GenerateReport(double totalTime) moel@345: { moel@345: StringBuilder sb = new StringBuilder(); moel@345: int len = 0; moel@345: foreach (PerformanceInfo info in Performances) moel@345: len = Math.Max(info.Name.Length, len); moel@345: moel@345: sb.AppendLine("Name".PadRight(len) + " Count Total Time, ms Avg. Time, ms Percentage, %"); moel@345: sb.AppendLine("----------------------------------------------------------------------------------------------"); moel@345: foreach (PerformanceInfo info in Performances) moel@345: { moel@345: sb.Append(info.Name.PadRight(len)); moel@345: double p = 0; moel@345: double avgt = 0; moel@345: if (totalTime != 0) moel@345: p = info.TotalTime / totalTime; moel@345: if (info.Count > 0) moel@345: avgt = info.TotalTime * 1000 / info.Count; moel@345: string c = info.Count.ToString("0,0").PadRight(20); moel@345: string tt = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20); moel@345: string t = avgt.ToString("0.0000").PadRight(20); moel@345: string sp = (p * 100).ToString("###").PadRight(20); moel@345: sb.AppendFormat(" " + c + tt + t + sp + "\n"); moel@345: } moel@345: return sb.ToString(); moel@345: } moel@345: } moel@345: }