|
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Aga.Controls
{
/// <summary>
/// Is used to analyze code performance
/// </summary>
public static class PerformanceAnalyzer
{
public class PerformanceInfo
{
private string _name;
public string Name
{
get { return _name; }
}
private int _count = 0;
public int Count
{
get { return _count; }
set { _count = value; }
}
private double _totalTime = 0;
public double TotalTime
{
get { return _totalTime; }
set { _totalTime = value; }
}
private Int64 _start;
public Int64 Start
{
get { return _start; }
set { _start = value; }
}
public PerformanceInfo(string name)
{
_name = name;
}
}
private static Dictionary<string, PerformanceInfo> _performances = new Dictionary<string, PerformanceInfo>();
public static IEnumerable<PerformanceInfo> Performances
{
get
{
return _performances.Values;
}
}
[Conditional("DEBUG")]
public static void Start(string pieceOfCode)
{
PerformanceInfo info = null;
lock(_performances)
{
if (_performances.ContainsKey(pieceOfCode))
info = _performances[pieceOfCode];
else
{
info = new PerformanceInfo(pieceOfCode);
_performances.Add(pieceOfCode, info);
}
info.Count++;
info.Start = TimeCounter.GetStartValue();
}
}
[Conditional("DEBUG")]
public static void Finish(string pieceOfCode)
{
lock (_performances)
{
if (_performances.ContainsKey(pieceOfCode))
{
PerformanceInfo info = _performances[pieceOfCode];
info.Count++;
info.TotalTime += TimeCounter.Finish(info.Start);
}
}
}
public static void Reset()
{
_performances.Clear();
}
public static string GenerateReport()
{
return GenerateReport(0);
}
public static string GenerateReport(string mainPieceOfCode)
{
if (_performances.ContainsKey(mainPieceOfCode))
return GenerateReport(_performances[mainPieceOfCode].TotalTime);
else
return GenerateReport(0);
}
public static string GenerateReport(double totalTime)
{
StringBuilder sb = new StringBuilder();
int len = 0;
foreach (PerformanceInfo info in Performances)
len = Math.Max(info.Name.Length, len);
sb.AppendLine("Name".PadRight(len) + " Count Time, ms Percentage, %");
sb.AppendLine("---------------------------------------------------------------------------------");
foreach (PerformanceInfo info in Performances)
{
sb.Append(info.Name.PadRight(len));
double p = 0;
if (totalTime != 0)
p = info.TotalTime / totalTime;
string c = info.Count.ToString("0,0").PadRight(20);
string t = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20);
string sp = (p * 100).ToString("###").PadRight(20);
sb.AppendFormat(" " + c + t + sp + "\n");
}
return sb.ToString();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Philip is a Software Architect with severaly years of experience. He studied business IT in the University of Wedel, Germany, and has reached his Masters degree in 2010. Back in 2002, he started his so far best-known software PDFCreator. The second developer joined two years later and since then, both constantly improve this piece of open source software.