SHARE
TWEET

DiagnosticsRecorder.cs

napland Oct 5th, 2016 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System;
  4.  
  5. namespace NG.Diagnostics
  6. {
  7.     public class DiagnosticRecorder
  8.     {
  9.         private Dictionary<string, StopwatchRecorder> timers = new Dictionary<string, StopwatchRecorder>();
  10.         private Dictionary<string, Counter> counters = new Dictionary<string, Counter>();
  11.  
  12.         public void StartTimer(string name)
  13.         {
  14.             if (!timers.ContainsKey(name))
  15.                 timers.Add(name, new StopwatchRecorder());
  16.  
  17.             timers[name].Start();
  18.         }
  19.  
  20.         public void StopTimer(string name)
  21.         {
  22.             if (timers.ContainsKey(name))
  23.                 timers[name].Stop();
  24.         }
  25.        
  26.         public void RecordCount(string name, int value, bool shouldCaptureZero = false)
  27.         {
  28.             if (!counters.ContainsKey(name))
  29.                 counters.Add(name, new Counter());
  30.  
  31.             counters[name].Record(value, shouldCaptureZero);
  32.         }
  33.  
  34.         public StopwatchRecorder GetStopwatch(string name)
  35.         {
  36.             if (timers.ContainsKey(name))
  37.                 return timers[name];
  38.             else
  39.                 throw new ArgumentOutOfRangeException(
  40.                     string.Format("No StopWatchRecorder with name: '{0}' found.", name));
  41.         }
  42.  
  43.         public Counter GetCounter(string name)
  44.         {
  45.             if (counters.ContainsKey(name))
  46.                 return counters[name];
  47.             else
  48.                 throw new ArgumentOutOfRangeException(
  49.                     string.Format("No Counter with name: '{0}' found.", name));
  50.         }
  51.  
  52.  
  53.         public override string ToString()
  54.         {
  55.             StringBuilder sb = new StringBuilder();
  56.  
  57.             if (timers.Count > 0)
  58.                 sb.AppendLine("Timers (ms)");
  59.  
  60.             foreach (var item in timers)
  61.             {
  62.                 sb.AppendFormat("{0}\nLast: {1}\tMin: {2}\tMax: {3}\tAvg: {4:N1}\tTotal: {5}\n",
  63.                     item.Key, item.Value.LastElapsedMilliseconds,
  64.                     item.Value.MinimumElapsedMilliseconds,
  65.                     item.Value.MaximumElapsedMilliseconds,
  66.                     item.Value.AverageElapsedMilliseconds,
  67.                     item.Value.TotalElapsedMilliseconds);
  68.             }
  69.  
  70.             if (counters.Count > 0)
  71.                 sb.AppendLine("Counters");
  72.  
  73.             foreach (var item in counters)
  74.             {
  75.                 sb.AppendFormat("{0}\nLast: {1}\tMin: {2}\tMax: {3}\tAvg: {4}\tSum: {5}\n",
  76.                     item.Key, item.Value.Last, item.Value.Min, item.Value.Max, item.Value.Average, item.Value.Sum);
  77.             }
  78.  
  79.             return sb.ToString();
  80.         }
  81.     }
  82. }
RAW Paste Data
Top