andrew4582

RunTimer Testing

Aug 15th, 2010
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. namespace System.Diagnostics
  5. {
  6.     using Threading;
  7.     public class RunTimerTest
  8.     {
  9.         public static string Test ()
  10.         {
  11.             RunTimerBatch timers = new RunTimerBatch ();
  12.             using (timers.Start ("Total")) {
  13.                 using (timers.Start ("Fetch")) {
  14.                     Thread.Sleep (2000);
  15.                 }
  16.                 using (timers.Start ("Load")) {
  17.                     Thread.Sleep (1000);
  18.                 }
  19.                 using (timers.Start ("Process")) {
  20.                     Thread.Sleep (500);
  21.                 }
  22.                 using (timers.Start ()) {
  23.                     Thread.Sleep (5);
  24.                 }
  25.                 using (timers.Start ("Exit")) {
  26.                     Thread.Sleep (TimeSpan.FromMilliseconds (.51));
  27.                 }
  28.             }
  29.             return timers.ToString ();
  30.         }
  31.     }
  32.     public class RunTimerBatch
  33.     {
  34.         private Dictionary<string, RunTimer> _RunTimers;
  35.         private Stack<string> _NoNameStack;
  36.  
  37.         public RunTimer Start ()
  38.         {
  39.             string name = "Run # " + (_RunTimers.Count + 1).ToString ();
  40.             _NoNameStack.Push (name);
  41.             return Start (name);
  42.         }
  43.         public RunTimer Start (string timerName)
  44.         {
  45.             RunTimer tmr = null;
  46.             if (_RunTimers.ContainsKey (timerName)) {
  47.                 tmr = _RunTimers[timerName];
  48.             } else {
  49.                 tmr = new RunTimer (timerName, false);
  50.                 _RunTimers.Add (timerName, tmr);
  51.             }
  52.             tmr.Start ();
  53.             return tmr;
  54.         }
  55.         public RunTimer Stop ()
  56.         {
  57.             if (_NoNameStack.Count == 0)
  58.                 return null;
  59.             string name = _NoNameStack.Pop ();
  60.             return Stop (name);
  61.         }
  62.         public RunTimer Stop (string timerName)
  63.         {
  64.             RunTimer tmr = _RunTimers[timerName];
  65.             tmr.Stop ();
  66.             return tmr;
  67.         }
  68.         public override string ToString ()
  69.         {
  70.             return ToString (string.Empty);
  71.         }
  72.         /// <summary>
  73.         /// Reqired two tokens for the 'RunName' and the 'TotalMilliseconds' of the RunTimer object
  74.         /// </summary>
  75.         /// <param name="format">{0}=this.RunName,{1}=this.RunTime.TotalMilliseconds</param>
  76.         /// <returns>Formated string</returns>
  77.         public string ToString (string format)
  78.         {
  79.             string message = "";
  80.             foreach (RunTimer rn in _RunTimers.Values) {
  81.                 message += rn.ToString (format);
  82.                 if (string.IsNullOrEmpty (format))
  83.                     message += Environment.NewLine;
  84.             }
  85.             return message;
  86.         }
  87.         public RunTimerBatch ()
  88.         {
  89.             _RunTimers = new Dictionary<string, RunTimer> ();
  90.             _NoNameStack = new Stack<string> ();
  91.         }
  92.     }
  93.     public class RunTimer : IDisposable
  94.     {
  95.         private bool _IsStopped;
  96.         public string RunName = "RunTime";
  97.         public DateTime StartTime;
  98.         public DateTime EndTime;
  99.         public TimeSpan RunTime;
  100.         public string MessageInMilliseconds ()
  101.         {
  102.             return GetTotalMessage (RunTime.TotalMilliseconds, "ms");
  103.         }
  104.         public string MessageInSeconds ()
  105.         {
  106.             return GetTotalMessage (RunTime.TotalSeconds, "sec");
  107.         }
  108.         public string MessageInMinutes ()
  109.         {
  110.             return GetTotalMessage (RunTime.TotalMinutes, "min");
  111.         }
  112.         private string GetTotalMessage (double amount, string term)
  113.         {
  114.             string rn = RunName;
  115.             rn += ":\t";
  116.             if (rn.Length < 10)
  117.                 rn += "\t";
  118.             return rn + amount.ToString ("n") + " " + term;
  119.         }
  120.         public override string ToString ()
  121.         {
  122.             if (RunTime.TotalMilliseconds > 1000 && RunTime.TotalSeconds > 60)
  123.                 return MessageInMinutes (); else if (RunTime.TotalMilliseconds > 1000 && RunTime.TotalSeconds < 60)
  124.                 return MessageInSeconds ();
  125.             else
  126.                 return MessageInMilliseconds ();
  127.         }
  128.         /// <summary>
  129.         /// Reqired two tokens for the 'RunName' and the 'TotalMilliseconds'
  130.         /// </summary>
  131.         /// <param name="format">{0}=this.RunName,{1}=this.RunTime.TotalMilliseconds</param>
  132.         /// <returns>Formated string</returns>
  133.         public string ToString (string format)
  134.         {
  135.             if (string.IsNullOrEmpty (format))
  136.                 return this.ToString ();
  137.             return string.Format (format, RunName, RunTime.TotalMilliseconds);
  138.         }
  139.         public void Start ()
  140.         {
  141.             _IsStopped = false;
  142.             StartTime = DateTime.Now;
  143.         }
  144.         public void Stop ()
  145.         {
  146.             if (_IsStopped)
  147.                 return;
  148.             EndTime = DateTime.Now;
  149.             RunTime = EndTime.Subtract (StartTime);
  150.             _IsStopped = true;
  151.         }
  152.         public RunTimer () : this(null, true)
  153.         {
  154.         }
  155.         public RunTimer (string runName) : this(runName, true)
  156.         {
  157.         }
  158.         public RunTimer (string runName, bool autoStart)
  159.         {
  160.             RunName = runName;
  161.             if (autoStart)
  162.                 Start ();
  163.         }
  164.         public void Dispose ()
  165.         {
  166.             Stop ();
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment