napland

StopwatchRecorder.cs

Oct 5th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace NG.Diagnostics
  7. {
  8.     /// <summary>
  9.     /// Derives from System.Diagnostics.Stopwatch
  10.     /// Adds the ability to stop and restart the stopwatch multiple times
  11.     /// and record the total time elapsed, minimum & maximum time elapsed,
  12.     /// and the average time elapsed from all calls to this stopwatch.
  13.     /// </summary>
  14.     public class StopwatchRecorder : Stopwatch
  15.     {
  16.         private List<long> capturedMilliseconds = new List<long>();
  17.         private List<KeyValuePair<long, int>> sumCountsPairs = new List<KeyValuePair<long, int>>();
  18.         private int bufferSize = int.MaxValue - 1;
  19.  
  20.         /// <summary>
  21.         /// The total time recorded by this stopwatch even after restarts.
  22.         /// </summary>
  23.         public ulong TotalElapsedMilliseconds { get; private set; }
  24.        
  25.         /// <summary>
  26.         /// This minimum amount of time elapsed during a run of this stopwatch.
  27.         /// </summary>
  28.         public long MinimumElapsedMilliseconds { get; private set; }
  29.        
  30.         /// <summary>
  31.         /// This maximum amount of time elapsed during a run of this stopwatch.
  32.         /// </summary>
  33.         public long MaximumElapsedMilliseconds { get; private set; }
  34.  
  35.         /// <summary>
  36.         /// The average time elapsed during a run of this stopwatch.
  37.         /// </summary>
  38.         public double AverageElapsedMilliseconds
  39.         {
  40.             get
  41.             {
  42.                 ulong count = (uint)capturedMilliseconds.Count;
  43.                 double sum = 0;
  44.  
  45.                 if (count > 0)
  46.                     sum = capturedMilliseconds.Sum();
  47.  
  48.                 foreach (var item in sumCountsPairs)
  49.                 {
  50.                     ulong maxCheck = ulong.MaxValue - count;
  51.                     if ((uint)item.Value < maxCheck)
  52.                         count += (uint)item.Value;
  53.                     else
  54.                         break;
  55.                     sum += item.Key;
  56.                 }
  57.  
  58.                 if (count > 0)
  59.                     return (sum / count);
  60.                 else
  61.                     return 0;
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// The amount of time elapsed in the last run of this stopwatch.
  67.         /// </summary>
  68.         public long LastElapsedMilliseconds
  69.         {
  70.             get
  71.             {
  72.                 if (capturedMilliseconds.Count > 0)
  73.                     return capturedMilliseconds.Last();
  74.                 else
  75.                     return -1;
  76.             }
  77.         }
  78.  
  79.  
  80.         public new void Stop()
  81.         {
  82.             base.Stop();
  83.  
  84.             if (MinimumElapsedMilliseconds == 0)
  85.                 MinimumElapsedMilliseconds = ElapsedMilliseconds;
  86.             else
  87.                 MinimumElapsedMilliseconds = Math.Min(ElapsedMilliseconds, MinimumElapsedMilliseconds);
  88.  
  89.             MaximumElapsedMilliseconds = Math.Max(ElapsedMilliseconds, MaximumElapsedMilliseconds);
  90.  
  91.             if (capturedMilliseconds.Count >= bufferSize)
  92.             {
  93.                 sumCountsPairs.Add(
  94.                     new KeyValuePair<long, int>(
  95.                         capturedMilliseconds.Sum(), capturedMilliseconds.Count));
  96.                 capturedMilliseconds.Clear();
  97.             }
  98.             capturedMilliseconds.Add(ElapsedMilliseconds);
  99.             TotalElapsedMilliseconds += (ulong)ElapsedMilliseconds;
  100.  
  101.             Reset();
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment