Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace System.Diagnostics
- {
- using Threading;
- public class RunTimerTest
- {
- public static string Test ()
- {
- RunTimerBatch timers = new RunTimerBatch ();
- using (timers.Start ("Total")) {
- using (timers.Start ("Fetch")) {
- Thread.Sleep (2000);
- }
- using (timers.Start ("Load")) {
- Thread.Sleep (1000);
- }
- using (timers.Start ("Process")) {
- Thread.Sleep (500);
- }
- using (timers.Start ()) {
- Thread.Sleep (5);
- }
- using (timers.Start ("Exit")) {
- Thread.Sleep (TimeSpan.FromMilliseconds (.51));
- }
- }
- return timers.ToString ();
- }
- }
- public class RunTimerBatch
- {
- private Dictionary<string, RunTimer> _RunTimers;
- private Stack<string> _NoNameStack;
- public RunTimer Start ()
- {
- string name = "Run # " + (_RunTimers.Count + 1).ToString ();
- _NoNameStack.Push (name);
- return Start (name);
- }
- public RunTimer Start (string timerName)
- {
- RunTimer tmr = null;
- if (_RunTimers.ContainsKey (timerName)) {
- tmr = _RunTimers[timerName];
- } else {
- tmr = new RunTimer (timerName, false);
- _RunTimers.Add (timerName, tmr);
- }
- tmr.Start ();
- return tmr;
- }
- public RunTimer Stop ()
- {
- if (_NoNameStack.Count == 0)
- return null;
- string name = _NoNameStack.Pop ();
- return Stop (name);
- }
- public RunTimer Stop (string timerName)
- {
- RunTimer tmr = _RunTimers[timerName];
- tmr.Stop ();
- return tmr;
- }
- public override string ToString ()
- {
- return ToString (string.Empty);
- }
- /// <summary>
- /// Reqired two tokens for the 'RunName' and the 'TotalMilliseconds' of the RunTimer object
- /// </summary>
- /// <param name="format">{0}=this.RunName,{1}=this.RunTime.TotalMilliseconds</param>
- /// <returns>Formated string</returns>
- public string ToString (string format)
- {
- string message = "";
- foreach (RunTimer rn in _RunTimers.Values) {
- message += rn.ToString (format);
- if (string.IsNullOrEmpty (format))
- message += Environment.NewLine;
- }
- return message;
- }
- public RunTimerBatch ()
- {
- _RunTimers = new Dictionary<string, RunTimer> ();
- _NoNameStack = new Stack<string> ();
- }
- }
- public class RunTimer : IDisposable
- {
- private bool _IsStopped;
- public string RunName = "RunTime";
- public DateTime StartTime;
- public DateTime EndTime;
- public TimeSpan RunTime;
- public string MessageInMilliseconds ()
- {
- return GetTotalMessage (RunTime.TotalMilliseconds, "ms");
- }
- public string MessageInSeconds ()
- {
- return GetTotalMessage (RunTime.TotalSeconds, "sec");
- }
- public string MessageInMinutes ()
- {
- return GetTotalMessage (RunTime.TotalMinutes, "min");
- }
- private string GetTotalMessage (double amount, string term)
- {
- string rn = RunName;
- rn += ":\t";
- if (rn.Length < 10)
- rn += "\t";
- return rn + amount.ToString ("n") + " " + term;
- }
- public override string ToString ()
- {
- if (RunTime.TotalMilliseconds > 1000 && RunTime.TotalSeconds > 60)
- return MessageInMinutes (); else if (RunTime.TotalMilliseconds > 1000 && RunTime.TotalSeconds < 60)
- return MessageInSeconds ();
- else
- return MessageInMilliseconds ();
- }
- /// <summary>
- /// Reqired two tokens for the 'RunName' and the 'TotalMilliseconds'
- /// </summary>
- /// <param name="format">{0}=this.RunName,{1}=this.RunTime.TotalMilliseconds</param>
- /// <returns>Formated string</returns>
- public string ToString (string format)
- {
- if (string.IsNullOrEmpty (format))
- return this.ToString ();
- return string.Format (format, RunName, RunTime.TotalMilliseconds);
- }
- public void Start ()
- {
- _IsStopped = false;
- StartTime = DateTime.Now;
- }
- public void Stop ()
- {
- if (_IsStopped)
- return;
- EndTime = DateTime.Now;
- RunTime = EndTime.Subtract (StartTime);
- _IsStopped = true;
- }
- public RunTimer () : this(null, true)
- {
- }
- public RunTimer (string runName) : this(runName, true)
- {
- }
- public RunTimer (string runName, bool autoStart)
- {
- RunName = runName;
- if (autoStart)
- Start ();
- }
- public void Dispose ()
- {
- Stop ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment