External/Aga.Controls/PerformanceAnalyzer.cs
author StephaneLenclud
Sun, 03 Feb 2013 18:01:50 +0100
branchMiniDisplay
changeset 433 090259cfd699
permissions -rw-r--r--
Adding SoundGraphDisplay and SensorFrontView classes.
They were respectively based on SystemTray and SensorNotifyIcon.
SoundGraphDisplay is now able to load iMONDisplay.dll providing it lives on your PATH.
Adding option to sensor context menu for adding it into FrontView.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Diagnostics;
     5 
     6 namespace Aga.Controls
     7 {
     8 	/// <summary>
     9 	/// Is used to analyze code performance
    10 	/// </summary>
    11 	public static class PerformanceAnalyzer
    12 	{
    13 		public class PerformanceInfo
    14 		{
    15 			private string _name;
    16 			public string Name
    17 			{
    18 				get { return _name; }
    19 			}
    20 
    21 			private int _count = 0;
    22 			public int Count
    23 			{
    24 				get { return _count; }
    25 				set { _count = value; }
    26 			}
    27 
    28 			private double _totalTime = 0;
    29 			public double TotalTime
    30 			{
    31 				get { return _totalTime; }
    32 				set { _totalTime = value; }
    33 			}
    34 
    35 			private Int64 _start;
    36 			public Int64 Start
    37 			{
    38 				get { return _start; }
    39 				set { _start = value; }
    40 			}
    41 
    42 			public PerformanceInfo(string name)
    43 			{
    44 				_name = name;
    45 			}
    46 		}
    47 
    48 		private static Dictionary<string, PerformanceInfo> _performances = new Dictionary<string, PerformanceInfo>();
    49 
    50 		public static IEnumerable<PerformanceInfo> Performances
    51 		{
    52 			get
    53 			{
    54 				return _performances.Values;
    55 			}
    56 		}
    57 
    58 		[Conditional("DEBUG")]
    59 		public static void Start(string pieceOfCode)
    60 		{
    61 			PerformanceInfo info = null;
    62 			lock(_performances)
    63 			{
    64 				if (_performances.ContainsKey(pieceOfCode))
    65 					info = _performances[pieceOfCode];
    66 				else
    67 				{
    68 					info = new PerformanceInfo(pieceOfCode);
    69 					_performances.Add(pieceOfCode, info);
    70 				}
    71 
    72 				info.Count++;
    73 				info.Start = TimeCounter.GetStartValue();
    74 			}
    75 		}
    76 
    77 		[Conditional("DEBUG")]
    78 		public static void Finish(string pieceOfCode)
    79 		{
    80 			lock (_performances)
    81 			{
    82 				if (_performances.ContainsKey(pieceOfCode))
    83 				{
    84 					PerformanceInfo info = _performances[pieceOfCode];
    85 					info.Count++;
    86 					info.TotalTime += TimeCounter.Finish(info.Start);
    87 				}
    88 			}
    89 		}
    90 
    91 		public static void Reset()
    92 		{
    93 			_performances.Clear();
    94 		}
    95 
    96 		public static string GenerateReport()
    97 		{
    98 			return GenerateReport(0);
    99 		}
   100 
   101 		public static string GenerateReport(string mainPieceOfCode)
   102 		{
   103 			if (_performances.ContainsKey(mainPieceOfCode))
   104 				return GenerateReport(_performances[mainPieceOfCode].TotalTime);
   105 			else
   106 				return GenerateReport(0);
   107 		}
   108 
   109 		public static string GenerateReport(double totalTime)
   110 		{
   111 			StringBuilder sb = new StringBuilder();
   112 			int len = 0;
   113 			foreach (PerformanceInfo info in Performances)
   114 				len = Math.Max(info.Name.Length, len);
   115 
   116 			sb.AppendLine("Name".PadRight(len) + " Count              Total Time, ms    Avg. Time, ms       Percentage, %");
   117 			sb.AppendLine("----------------------------------------------------------------------------------------------");
   118 			foreach (PerformanceInfo info in Performances)
   119 			{
   120 				sb.Append(info.Name.PadRight(len));
   121 				double p = 0;
   122 				double avgt = 0;
   123 				if (totalTime != 0)
   124 					p = info.TotalTime / totalTime;
   125 				if (info.Count > 0)
   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");
   132 			}
   133 			return sb.ToString();
   134 		}
   135 	}
   136 }