napland

Counter.cs

Oct 5th, 2016
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace NG.Diagnostics
  6. {
  7.     /// <summary>
  8.     /// A diagnostic counter that maintains the Min, Max, and Average counts fed in to it.
  9.     /// </summary>
  10.     public class Counter
  11.     {
  12.         private List<int> values = new List<int>();
  13.         private List<KeyValuePair<int, int>> sumCountsPairs = new List<KeyValuePair<int, int>>();
  14.         private int bufferSize = int.MaxValue - 1;
  15.  
  16.         /// <summary>
  17.         /// The maximum count.
  18.         /// </summary>
  19.         public int Max { get; private set; }
  20.  
  21.         /// <summary>
  22.         /// The minimum count.
  23.         /// </summary>
  24.         public int Min { get; private set; }
  25.  
  26.  
  27.         /// <summary>
  28.         /// The average count.
  29.         /// </summary>
  30.         public int Average
  31.         {
  32.             get
  33.             {
  34.                 long count = (long)values.Count;
  35.  
  36.                 long sum = 0;
  37.                 if (count > 0)
  38.                     sum = values.Sum();
  39.  
  40.                 foreach (var item in sumCountsPairs)
  41.                 {
  42.                     long maxCheck = long.MaxValue - count;
  43.                     if ((uint)item.Value < maxCheck)
  44.                         count += (uint)item.Value;
  45.                     else
  46.                         break;
  47.                     sum += item.Key;
  48.                 }
  49.  
  50.                 if (count > 0)
  51.                     return (int)(sum / count);
  52.                 else
  53.                     return 0;
  54.             }
  55.         }
  56.  
  57.         /// <summary>
  58.         /// The last count added to this counter.
  59.         /// </summary>
  60.         public int Last
  61.         {
  62.             get
  63.             {
  64.                 if (values.Count > 0)
  65.                     return values.Last();
  66.                 else
  67.                     return -1;
  68.             }
  69.         }
  70.  
  71.         public long Sum { get; private set; }
  72.  
  73.         /// <summary>
  74.         /// Constructor ensures initial values for Min and Max.
  75.         /// </summary>
  76.         public Counter()
  77.         {
  78.             Max = int.MinValue;
  79.             Min = int.MaxValue;
  80.         }
  81.  
  82.  
  83.         /// <summary>
  84.         /// Call this to capture a count.
  85.         /// </summary>
  86.         /// <param name="value">The count to record.</param>
  87.         /// <param name="shouldCaptureZero">Should this capture a count of zero? Default is false.</param>
  88.         public void Record(int value, bool shouldCaptureZero = false)
  89.         {
  90.             if (!shouldCaptureZero && value == 0)
  91.                 return;
  92.  
  93.             Max = Math.Max(value, Max);
  94.             Min = Math.Min(value, Min);
  95.  
  96.             if (values.Count >= bufferSize)
  97.             {
  98.                 sumCountsPairs.Add(new KeyValuePair<int, int>(values.Sum(), values.Count));
  99.                 values.Clear();
  100.             }
  101.  
  102.             values.Add(value);
  103.             Sum += value;
  104.         }
  105.     }
  106. }
Add Comment
Please, Sign In to add comment