External/Aga.Controls/PerformanceAnalyzer.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
moel@345
     1
using System;
moel@345
     2
using System.Collections.Generic;
moel@345
     3
using System.Text;
moel@345
     4
using System.Diagnostics;
moel@345
     5
moel@345
     6
namespace Aga.Controls
moel@345
     7
{
moel@345
     8
	/// <summary>
moel@345
     9
	/// Is used to analyze code performance
moel@345
    10
	/// </summary>
moel@345
    11
	public static class PerformanceAnalyzer
moel@345
    12
	{
moel@345
    13
		public class PerformanceInfo
moel@345
    14
		{
moel@345
    15
			private string _name;
moel@345
    16
			public string Name
moel@345
    17
			{
moel@345
    18
				get { return _name; }
moel@345
    19
			}
moel@345
    20
moel@345
    21
			private int _count = 0;
moel@345
    22
			public int Count
moel@345
    23
			{
moel@345
    24
				get { return _count; }
moel@345
    25
				set { _count = value; }
moel@345
    26
			}
moel@345
    27
moel@345
    28
			private double _totalTime = 0;
moel@345
    29
			public double TotalTime
moel@345
    30
			{
moel@345
    31
				get { return _totalTime; }
moel@345
    32
				set { _totalTime = value; }
moel@345
    33
			}
moel@345
    34
moel@345
    35
			private Int64 _start;
moel@345
    36
			public Int64 Start
moel@345
    37
			{
moel@345
    38
				get { return _start; }
moel@345
    39
				set { _start = value; }
moel@345
    40
			}
moel@345
    41
moel@345
    42
			public PerformanceInfo(string name)
moel@345
    43
			{
moel@345
    44
				_name = name;
moel@345
    45
			}
moel@345
    46
		}
moel@345
    47
moel@345
    48
		private static Dictionary<string, PerformanceInfo> _performances = new Dictionary<string, PerformanceInfo>();
moel@345
    49
moel@345
    50
		public static IEnumerable<PerformanceInfo> Performances
moel@345
    51
		{
moel@345
    52
			get
moel@345
    53
			{
moel@345
    54
				return _performances.Values;
moel@345
    55
			}
moel@345
    56
		}
moel@345
    57
moel@345
    58
		[Conditional("DEBUG")]
moel@345
    59
		public static void Start(string pieceOfCode)
moel@345
    60
		{
moel@345
    61
			PerformanceInfo info = null;
moel@345
    62
			lock(_performances)
moel@345
    63
			{
moel@345
    64
				if (_performances.ContainsKey(pieceOfCode))
moel@345
    65
					info = _performances[pieceOfCode];
moel@345
    66
				else
moel@345
    67
				{
moel@345
    68
					info = new PerformanceInfo(pieceOfCode);
moel@345
    69
					_performances.Add(pieceOfCode, info);
moel@345
    70
				}
moel@345
    71
moel@345
    72
				info.Count++;
moel@345
    73
				info.Start = TimeCounter.GetStartValue();
moel@345
    74
			}
moel@345
    75
		}
moel@345
    76
moel@345
    77
		[Conditional("DEBUG")]
moel@345
    78
		public static void Finish(string pieceOfCode)
moel@345
    79
		{
moel@345
    80
			lock (_performances)
moel@345
    81
			{
moel@345
    82
				if (_performances.ContainsKey(pieceOfCode))
moel@345
    83
				{
moel@345
    84
					PerformanceInfo info = _performances[pieceOfCode];
moel@345
    85
					info.Count++;
moel@345
    86
					info.TotalTime += TimeCounter.Finish(info.Start);
moel@345
    87
				}
moel@345
    88
			}
moel@345
    89
		}
moel@345
    90
moel@345
    91
		public static void Reset()
moel@345
    92
		{
moel@345
    93
			_performances.Clear();
moel@345
    94
		}
moel@345
    95
moel@345
    96
		public static string GenerateReport()
moel@345
    97
		{
moel@345
    98
			return GenerateReport(0);
moel@345
    99
		}
moel@345
   100
moel@345
   101
		public static string GenerateReport(string mainPieceOfCode)
moel@345
   102
		{
moel@345
   103
			if (_performances.ContainsKey(mainPieceOfCode))
moel@345
   104
				return GenerateReport(_performances[mainPieceOfCode].TotalTime);
moel@345
   105
			else
moel@345
   106
				return GenerateReport(0);
moel@345
   107
		}
moel@345
   108
moel@345
   109
		public static string GenerateReport(double totalTime)
moel@345
   110
		{
moel@345
   111
			StringBuilder sb = new StringBuilder();
moel@345
   112
			int len = 0;
moel@345
   113
			foreach (PerformanceInfo info in Performances)
moel@345
   114
				len = Math.Max(info.Name.Length, len);
moel@345
   115
moel@345
   116
			sb.AppendLine("Name".PadRight(len) + " Count              Total Time, ms    Avg. Time, ms       Percentage, %");
moel@345
   117
			sb.AppendLine("----------------------------------------------------------------------------------------------");
moel@345
   118
			foreach (PerformanceInfo info in Performances)
moel@345
   119
			{
moel@345
   120
				sb.Append(info.Name.PadRight(len));
moel@345
   121
				double p = 0;
moel@345
   122
				double avgt = 0;
moel@345
   123
				if (totalTime != 0)
moel@345
   124
					p = info.TotalTime / totalTime;
moel@345
   125
				if (info.Count > 0)
moel@345
   126
					avgt = info.TotalTime * 1000 / info.Count;
moel@345
   127
				string c = info.Count.ToString("0,0").PadRight(20);
moel@345
   128
				string tt = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20);
moel@345
   129
				string t = avgt.ToString("0.0000").PadRight(20);
moel@345
   130
				string sp = (p * 100).ToString("###").PadRight(20);
moel@345
   131
				sb.AppendFormat(" " + c + tt + t + sp + "\n");
moel@345
   132
			}
moel@345
   133
			return sb.ToString();
moel@345
   134
		}
moel@345
   135
	}
moel@345
   136
}