Skip to content

Instantly share code, notes, and snippets.

@ceilingfish
Created May 23, 2011 08:03
Show Gist options
  • Select an option

  • Save ceilingfish/986378 to your computer and use it in GitHub Desktop.

Select an option

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
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