Created
May 23, 2011 08:03
-
-
Save ceilingfish/986378 to your computer and use it in GitHub Desktop.
A basic unit test to compare trace, trace with autoflush off, and appending to a queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Text; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| using System.Diagnostics; | |
| using System.Collections.Concurrent; | |
| namespace DotNetTesting | |
| { | |
| [TestClass] | |
| public class LoggingTest | |
| { | |
| static long TraceWithAutoFlushTime, TraceWithoutAutoFlushTime, QueueTime; | |
| [TestMethod] | |
| public void TraceWithAutoFlushTest() | |
| { | |
| Trace.AutoFlush = true; | |
| Stopwatch sw = new Stopwatch(); | |
| for (int i = 0; i < 1100000; i++) | |
| { | |
| if (i == 100000) | |
| sw.Start(); | |
| Trace.TraceError("hello"); | |
| } | |
| sw.Stop(); | |
| TraceWithAutoFlushTime = sw.ElapsedTicks; | |
| } | |
| [TestMethod] | |
| public void TraceWithoutAutoFlushTest() | |
| { | |
| Trace.AutoFlush = false; | |
| Stopwatch sw = new Stopwatch(); | |
| for (int i = 0; i < 1100000; i++) | |
| { | |
| if (i == 100000) | |
| sw.Start(); | |
| Trace.TraceError("hello"); | |
| } | |
| sw.Stop(); | |
| TraceWithoutAutoFlushTime = sw.ElapsedTicks; | |
| } | |
| [TestMethod] | |
| public void QueueTest() | |
| { | |
| Stopwatch sw = new Stopwatch(); | |
| ConcurrentQueue<string> queue = new ConcurrentQueue<string>(); | |
| for (int i = 0; i < 1100000; i++) | |
| { | |
| if (i == 100000) | |
| sw.Start(); | |
| queue.Enqueue("hello"); | |
| } | |
| sw.Stop(); | |
| QueueTime = sw.ElapsedTicks; | |
| //Don't bother writing to output, as this would be done in a different thread. | |
| } | |
| [TestMethod] | |
| public void ReportTimes() | |
| { | |
| Console.WriteLine("TraceWithAutoFlushTime {0}", TraceWithAutoFlushTime); | |
| Console.WriteLine("TraceWithoutAutoFlushTime {0}", TraceWithoutAutoFlushTime); | |
| Console.WriteLine("QueueTime {0}", QueueTime); | |
| // TraceWithAutoFlushTime 59391901 | |
| // TraceWithoutAutoFlushTime 56578317 | |
| // QueueTime 89110 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment