Wednesday, 1 May 2013

Benchmarking Different Data Structures - Part One (Building the Library)

Introduction

Comparing the performance of data types is a practice all developers should conduct if you are building performance intensive software. By doing so it will help guide you in choosing the best data type to use and will also give you a more in-depth understanding of various data types. These tests are known as benchmarking.

Recently I wanted to learn about the performance of certain data structures to help me with future development.

This article is split into two parts. The purpose of this part is to share a benchmarking class library that I created to assist me with this investigation. The second part will show my results and conclusions of my testing of these data structures.

Test Environment

I wanted a test environment that is able to populate and remove from a collection with a large amount of data. The environment must be able to run a test a specified number of times, so that an average can be calculated producing a trusted set of results.

I have created a class library called BenchmarkHelper.

Usage

Below shows the main method of my test program that uses the BenchmarkHelper:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 private static void DoMultiRunTests()
        {
            var numberOfTestRuns = 100;
            var loopTest = new LoopTest(sizeOfCollection);
            var test = new BenchmarkTest(loopTest);

            PrintTest(String.Format("**Average of LinkedList tests executed {0} times**", numberOfTestRuns),
                test.DoTest(AddToLinkedList, numberOfTestRuns, ClearLinkedList));
            PrintTest(String.Format("**Average of List tests executed {0} times**",numberOfTestRuns),
                test.DoTest(AddToList, numberOfTestRuns, ClearList));
            PrintTest(String.Format("**Average of to Queue tests executed {0} times**",numberOfTestRuns), 
                test.DoTest(AddToQueue, numberOfTestRuns, ClearQueue));

            PrintTest(String.Format("**Average of removing from LinkedList executed {0} times**", numberOfTestRuns),
                test.DoTest(RemoveFromLinkedList, numberOfTestRuns, ResetLinkedList));
            PrintTest(String.Format("**Average of removing from List executed {0} times**", numberOfTestRuns),
                test.DoTest(RemoveFromList, numberOfTestRuns, ResetList));
            PrintTest(String.Format("**Average of removing from Queue executed {0} times**", numberOfTestRuns),
                test.DoTest(RemoveFromQueue, numberOfTestRuns, ResetQueue));
        }

The DoMultiRunTests method creates an instance of a LoopTest and uses it to create a BenchMarkTest instance. The LoopTest implements the interface ITestType which can be used for creating alternative performance tests.

The BenchmarkTest class has a method called DoTest which accepts three arguments:

 public double DoTest(Action method, int numberOfRuns, Action resetMethod)
  1. The method you wish to test.
  2. The number of times you wish to run this method.
  3. The method to clear the state of a test in between each test run.
(Action is a delegate that allows you to inject methods into another method)

DoTest returns a double which will be an average in milliseconds of all the test runs.

BenchmarkTest.DoTest

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    public class BenchmarkTest
    {
        private ITestType testType;

        public BenchmarkTest(ITestType testType)
        {
            this.testType = testType;
        }

        public double DoTest(Action method, int numberOfRuns, Action resetMethod)
        {
            var timeSpan = new TimeSpan[numberOfRuns];
            method();

            for (int i = 0; i < numberOfRuns; i++)
            {
                resetMethod();
                timeSpan[i] = testType.CallVoidMethod(method);
            }

            return timeSpan.Average(x => x.TotalMilliseconds);
        }

The purpose of this method is to run a test type a number of times and returning an average of how long they all took to execute.

It begins by invoking the test method without recording the time it takes.This is due to Just-in-time compiling which means the method initially taking longer upon it's first call and thus making our average unreliable.

During the loop, I invoke a reset method. This is to provide the opportunity to put the state of any data types back into an initial state. For example, repopulating a Queue so that we can test the dequeue functionality.

You may now be asking what does testType.CallVoidMethod do?

ITestType.CallVoidMethod

Below shows the interface ITestType:

1
2
3
4
5
    public interface ITestType
    {
        TimeSpan CallVoidMethod(Action method);
        TimeSpan CallArgumentMethod(Action<int> method);
    }

An implementation of this represents a type of performance test. The implementation that I wished to use for my testing is a simple for loop and so I created a class called LoopTest:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    public class LoopTest : ITestType
    {
        private Stopwatch stopWatch = new Stopwatch();
        private int size;

        public LoopTest(int size)
        {
            this.size = size;
        }

        public TimeSpan CallVoidMethod(Action method)
        {
            stopWatch.Reset();

            stopWatch.Start();
            for (int i = 0; i < size; i++)
            {
                method();
            }
            stopWatch.Stop();
            return stopWatch.Elapsed;
        }

This class's responsibility is to run a method a number of times and to return how long it had taken using Diagnostics.Stopwatch.

Conclusion

The BenchmarkHelper is reusable as you are able to pass in methods to test using the Action delegates and you can also implement ITestType to create your own type of performance tests. This means that this handy little assembly can be used in all my projects where I wish to do benchmarking.

Also, do a Bing search on profiling for testing the performance of your code. Tools such as SlimTune Profiler   attach to your assemblies and are able to show you where there might be performance issues in your code. I may possibly do a blog in the future about profiling but there is plenty of information out there already for you.

See part two for putting this library to use!

No comments:

Post a Comment