PerformanceAnalyzer.cs :  » GUI » TreeViewAdv » Aga » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » Aga » Controls » PerformanceAnalyzer.cs
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              Total Time, ms    Avg. Time, ms       Percentage, %");
      sb.AppendLine("----------------------------------------------------------------------------------------------");
      foreach (PerformanceInfo info in Performances)
      {
        sb.Append(info.Name.PadRight(len));
        double p = 0;
        double avgt = 0;
        if (totalTime != 0)
          p = info.TotalTime / totalTime;
        if (info.Count > 0)
          avgt = info.TotalTime * 1000 / info.Count;
        string c = info.Count.ToString("0,0").PadRight(20);
        string tt = (info.TotalTime * 1000).ToString("0,0.00").PadRight(20);
        string t = avgt.ToString("0.0000").PadRight(20);
        string sp = (p * 100).ToString("###").PadRight(20);
        sb.AppendFormat(" " + c + tt + t + sp + "\n");
      }
      return sb.ToString();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.