Skip to content
theburningmonk edited this page Aug 21, 2012 · 1 revision

All of the examples are checked into the source code, simply download the trunk, open the solution with Visual Studio and you'll find the examples in the SimpleSpeedTester.Example project.

Example 1 - Plan but not execute test

This examples illustrates how to plan a test but not execute it straight away.

// initialize a new test group
var testGroup = new TestGroup("Example1");

// create a test (in the form of an Action delegate) but don't run it yet
// when executed, the test will invoke the Action delegate 5 times and time how long it 
// took each time
var test = testGroup.Plan("Test1", () => { }, 5);

// calling GetResult() executes the test
var testResult = test.GetResult();

// you can use the TestResult object to analyse results of your test, but if you just 
// want the average execution time, then use GetSummary
var testResultSummary = testResult.GetSummary();

Console.WriteLine(testResultSummary);
/* prints out something along the line of
 *
 * Test Group [Example1], Test [Test1] results summary:
 * Successes   [5]
 * Failures    [0] 
 * Average Exec Time [...] milliseconds
 *
 */

Example 2 - Plan and execute test

This example illustrates the most basic use case where you just want to plan and execute your test in one go.

// initialize a new test group
var testGroup = new TestGroup("Example2");

// PlanAndExecute executes the Action delegate 5 times and returns the result summary
var testResultSummary = testGroup.PlanAndExecute("Test1", () => { }, 5);

Console.WriteLine(testResultSummary);
/* prints out something along the line of
 *
 * Test Group [Example2], Test [Test1] results summary:
 * Successes   [5]
 * Failures    [0] 
 * Average Exec Time [...] milliseconds
 *
 */

Example 3 - Closure

You can use closure to execute your test against a piece of data, which can be used to track some arbitrary metric as your run your tests.

// initialize a new test group
var testGroup = new TestGroup("Example3");

// create a random number generate
var randomGenerator = new Random(DateTime.UtcNow.Millisecond);

// plan a test that runs against some piece of data, it provides you with a way to track some 
// arbitrary metric as you run your tests
var numbers = new List<int>();
var testAction = testGroup.Plan("Test1", lst => lst.Add(randomGenerator.Next(100)), numbers, 5);

// when executed, this test will add 5 random numbers between 0 and 99 to the 'numbers' list
testAction.GetResult();

// this will print the 5 random number, e.g. 15, 7, 4, 9, 38
Console.WriteLine(string.Join(",", numbers.Select(n => n.ToString())));

Example 4 - Use custom test outcome filter

When conducting tests, in order to get a more accurate result it's common practice to run the same test multiple times and then work out the average. Sometimes one or two runs can give wildly different readings and throw your average off, so statisticians often exclude the top and bottom results from the sample set before using the rest to calculate the average.

The same can be achieved by using the overloaded TestResult.GetSummary method with an object that implements the ITestOutcomeFilter interface, two implementations are included in the checked in source code:

  1. DefaultTestOutcomeFilter - includes all successful test outcomes
  2. ExcludeMinAndMaxTestOutcomeFilter - all successful test outcomes excludes the shortest and longest test runs

The following example shows you how to use the ExcludeMinAndMaxTestOutcomeFilter with GetSummary.

// initialize a new test group
var testGroup = new TestGroup("Example4");

var sleepIntervals = new[] { 2, 3, 4, 5, 11 };
var index = 0;

// plans a test which puts the thread to sleep for 2, 3, 4, 5 and then 11 seconds, zzz....
var test = testGroup.Plan("Test1", () => Thread.Sleep(TimeSpan.FromSeconds(sleepIntervals[index++])), 5);

// execute the test runs and get the result
var testResult = test.GetResult();

// summaries the result whilst considering all the test outcomes
var resultSummaryDefault = testResult.GetSummary();

// alternatively, provide a filter so that when the average execution time is calculated, 
// the min and max times are not considered
var resultSummaryExcludeMinAndMax = testResult.GetSummary(new ExcludeMinAndMaxTestOutcomeFilter());

Console.WriteLine(resultSummaryDefault);
/* prints out something along the line of:
 * 
 * Test Group [Example4], Test [Test1] results summary:
 * Successes  [5]
 * Failures   [0] 
 * Average Exec Time [5000.05438] milliseconds
 * 
 */

Console.WriteLine(resultSummaryExcludeMinAndMax);
/* prints out something along the line of:
 * 
 * Test Group [Example4], Test [Test1] results summary:
 * Successes  [5]
 * Failures   [0] 
 * Average Exec Time [4000.1766] milliseconds
 * 
 */

Example 5 - Implementing a custom test outcome filter

This example follows on from the previous example and illustrates how you might implement your own filter, e.g. a filter which only considers the test runs which ended in exception:

public sealed class ExceptionOnlyTestOutcomeFilter : ITestOutcomeFilter
{
    public List<TestOutcome> Filter(List<TestOutcome> resultOutcomes)
    {
        return resultOutcomes.Where(o => o.Exception != null).ToList();
    }
}

Simple, right?

Clone this wiki locally