Creating a Performance Benchmark Framework for Your .NET Codes

Performance is a one of the basic requirements in the most of the current trend of web applications. While designing software architecture, there are several technologies kept available for the designers. Among these available options, some of techniques may includes multiple solutions, combining of those can create a case, where the basic performance output can't be determined based on the common sense and/or in depth knowledge on the corresponding topics. In short, based upon different options the software architect needs to determine the appropriate object model which fits best with the current application requirement.

The Object Model

There are three layers of our test framework object model, as follows:

The Program Class: This is the starter class, which creates the requires instances to start the test operation. The corresponding caller method initializes the Runner class along with the number of iterations to be performed for each test case.

The Runner Class: The runner class includes one or multiple methods, which calls a bunch of methods of executor class, each of which contains required functionalities to call the test cases appropriately.

    • The Runner Base Class: One important issue with respect to the runner class is, we need to have a way to keep track to execution time for each run and finally we need to determine the average execution time. For this purpose, to perform required initialization and support common reporting feature of test cases, we considered a base class, which includes all tracking data to isolate individual test cases and reporting functionalities. We have used C# delegate to have a very structured and reusable object model regarding this common functionalities. The methods of the inherited class uses this features accordingly.

The Executor Class: The executor class contains the very basic codes with respect to a particular test case.

ClassDiagram

The Sample Codes

The  Program Class

class Program

    {

        static void Main(string[] args)

        {

            Output.WriteLine("Test starts: " + DateTime.Now.ToString());

            //----------------------------------------

            SampleRunner s1 = new SampleRunner(10);

            s1.RunBenchmarks();

            //----------------------------------------

            Output.WriteLine("Test ends: " + DateTime.Now.ToString());

            Console.ReadLine();

        }

    }

The Runner Base Class

public class SampleRunnerBase

    {

        protected delegate void DoWork(string text);

 

        long _Start;

        long _Stop;

        double _Total = 0;

        double _TotalSecs;

        double _TotalAvgSecs;

        int _Repeats;

        protected int _RepeatsToDo;

 

        public SampleRunnerBase(int repeats)

        {

            _RepeatsToDo = repeats;

        }

 

        private void Init()

        {

            _Total = 0;

            _TotalSecs = 0;

            _TotalAvgSecs = 0;

            _Repeats = _RepeatsToDo;

        }

 

        private void ReportResults()

        {

            _TotalSecs = _Total / 10000000;

            _TotalAvgSecs = _Total / 10000000 / _RepeatsToDo;

 

            Output.WriteLine("Avg: " + _TotalAvgSecs + " s");

        }

 

        protected void RunBenchmark(DoWork func, string text)

        {

            if (null != func)

            {

                Init();

                while (_Repeats-- > 0)

                {

                    _Start = DateTime.Now.Ticks;

                    func(text);

                    _Stop = DateTime.Now.Ticks;

                    _Total += _Stop - _Start;

                }

                ReportResults();

            }

        }

    }

The Runner Class

public sealed class SampleRunner : SampleRunnerBase

    {

        public SampleRunner(int repeats)

            : base(repeats)

        {

        }

 

        public void RunBenchmarks()

        {

 

            Output.WriteLine("Running Simple Sample Benchmark");

            Output.WriteLine("--------------------------------");

            RunSample1();

            RunSample2();

        }

 

        private void RunSample1()

        {

            Output.WriteLine("Running Sample 1");

            SampleExecutor runner = new SampleExecutor();

            RunBenchmark(new DoWork (runner.DoSample1), "");

        }

 

        private void RunSample2()

        {

            Output.WriteLine("Running Sample 2");

            SampleExecutor runner = new SampleExecutor();

            RunBenchmark(new DoWork(runner.DoSample2), "");

        }

 

    }

The Executor Class

public class SampleExecutor

    {

        public void DoSample1(string text)

        {

            //do something

        }

 

        public void DoSample2(string text)

        {

            //do something

        }

    }

 Download source code:

kick it on DotNetKicks.com

Print | posted on Thursday, November 22, 2007 4:32 PM

Feedback

# re: Creating a Performance Benchmark Framework for Your .NET Codes

left by sami at 12/23/2008 2:47 AM Gravatar
hi there,
i hope you can give me some guide here
What is the standard perforamance for ASP.Net ? is there benchmark for such thing ?

Post A Comment
Title:
Name:
Email:
Comment:
Verification: