External/Aga.Controls/PerformanceAnalyzer.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/PerformanceAnalyzer.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,136 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Diagnostics;
     1.8 +
     1.9 +namespace Aga.Controls
    1.10 +{
    1.11 +	/// <summary>
    1.12 +	/// Is used to analyze code performance
    1.13 +	/// </summary>
    1.14 +	public static class PerformanceAnalyzer
    1.15 +	{
    1.16 +		public class PerformanceInfo
    1.17 +		{
    1.18 +			private string _name;
    1.19 +			public string Name
    1.20 +			{
    1.21 +				get { return _name; }
    1.22 +			}
    1.23 +
    1.24 +			private int _count = 0;
    1.25 +			public int Count
    1.26 +			{
    1.27 +				get { return _count; }
    1.28 +				set { _count = value; }
    1.29 +			}
    1.30 +
    1.31 +			private double _totalTime = 0;
    1.32 +			public double TotalTime
    1.33 +			{
    1.34 +				get { return _totalTime; }
    1.35 +				set { _totalTime = value; }
    1.36 +			}
    1.37 +
    1.38 +			private Int64 _start;
    1.39 +			public Int64 Start
    1.40 +			{
    1.41 +				get { return _start; }
    1.42 +				set { _start = value; }
    1.43 +			}
    1.44 +
    1.45 +			public PerformanceInfo(string name)
    1.46 +			{
    1.47 +				_name = name;
    1.48 +			}
    1.49 +		}
    1.50 +
    1.51 +		private static Dictionary<string, PerformanceInfo> _performances = new Dictionary<string, PerformanceInfo>();
    1.52 +
    1.53 +		public static IEnumerable<PerformanceInfo> Performances
    1.54 +		{
    1.55 +			get
    1.56 +			{
    1.57 +				return _performances.Values;
    1.58 +			}
    1.59 +		}
    1.60 +
    1.61 +		[Conditional("DEBUG")]
    1.62 +		public static void Start(string pieceOfCode)
    1.63 +		{
    1.64 +			PerformanceInfo info = null;
    1.65 +			lock(_performances)
    1.66 +			{
    1.67 +				if (_performances.ContainsKey(pieceOfCode))
    1.68 +					info = _performances[pieceOfCode];
    1.69 +				else
    1.70 +				{
    1.71 +					info = new PerformanceInfo(pieceOfCode);
    1.72 +					_performances.Add(pieceOfCode, info);
    1.73 +				}
    1.74 +
    1.75 +				info.Count++;
    1.76 +				info.Start = TimeCounter.GetStartValue();
    1.77 +			}
    1.78 +		}
    1.79 +
    1.80 +		[Conditional("DEBUG")]
    1.81 +		public static void Finish(string pieceOfCode)
    1.82 +		{
    1.83 +			lock (_performances)
    1.84 +			{
    1.85 +				if (_performances.ContainsKey(pieceOfCode))
    1.86 +				{
    1.87 +					PerformanceInfo info = _performances[pieceOfCode];
    1.88 +					info.Count++;
    1.89 +					info.TotalTime += TimeCounter.Finish(info.Start);
    1.90 +				}
    1.91 +			}
    1.92 +		}
    1.93 +
    1.94 +		public static void Reset()
    1.95 +		{
    1.96 +			_performances.Clear();
    1.97 +		}
    1.98 +
    1.99 +		public static string GenerateReport()
   1.100 +		{
   1.101 +			return GenerateReport(0);
   1.102 +		}
   1.103 +
   1.104 +		public static string GenerateReport(string mainPieceOfCode)
   1.105 +		{
   1.106 +			if (_performances.ContainsKey(mainPieceOfCode))
   1.107 +				return GenerateReport(_performances[mainPieceOfCode].TotalTime);
   1.108 +			else
   1.109 +				return GenerateReport(0);
   1.110 +		}
   1.111 +
   1.112 +		public static string GenerateReport(double totalTime)
   1.113 +		{
   1.114 +			StringBuilder sb = new StringBuilder();
   1.115 +			int len = 0;
   1.116 +			foreach (PerformanceInfo info in Performances)
   1.117 +				len = Math.Max(info.Name.Length, len);
   1.118 +
   1.119 +			sb.AppendLine("Name".PadRight(len) + " Count              Total Time, ms    Avg. Time, ms       Percentage, %");
   1.120 +			sb.AppendLine("----------------------------------------------------------------------------------------------");
   1.121 +			foreach (PerformanceInfo info in Performances)
   1.122 +			{
   1.123 +				sb.Append(info.Name.PadRight(len));
   1.124 +				double p = 0;
   1.125 +				double avgt = 0;
   1.126 +				if (totalTime != 0)
   1.127 +					p = info.TotalTime / totalTime;
   1.128 +				if (info.Count > 0)
   1.129 +					avgt = info.TotalTime * 1000 / info.Count;
   1.130 +				string c = info.Count.ToString("0,0").PadRight(20);
   1.131 +				string tt = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20);
   1.132 +				string t = avgt.ToString("0.0000").PadRight(20);
   1.133 +				string sp = (p * 100).ToString("###").PadRight(20);
   1.134 +				sb.AppendFormat(" " + c + tt + t + sp + "\n");
   1.135 +			}
   1.136 +			return sb.ToString();
   1.137 +		}
   1.138 +	}
   1.139 +}