Skip to main content

Benchmarking in C#

A bit of a change to my normal blog posts this time.... In the next few posts I will be talking about benchmark testing and in particular a NuGet package that allows you to run bench-marking tests in a .NET environment. 

So in this post I will demonstrate how you can use a NuGet package to measure the time it takes to open notepad on your local machine. 

This will be the starting point and I am aiming to build up some more complex examples as I learn about the NuGet package. 

So here goes.......

The NuGet package is called BenchMarkDotNet and is a powerful library for bench-marking various tasks. 

The GitHub page can be found here:

https://github.com/dotnet/BenchmarkDotNet

So to use this package, the first thing you need to do is create a new Console App (.NET Framework) project in Visual Studio 


You then need to add the NuGet package to the project (You should find the NuGet package if you search for 'BenchMarkDotNet'):



At the time of writing the latest version is v0.11.1

Once you have done this you need to create 2 classes. One will contain the code that runs the test while the other one will contain the the code that defines the benchmark test. So in my example I have the following:

  • Program.cs - This will contain the code that runs the benchmark test.
  • MyFirstBenchMark.cs - This class contains the code that defines the benchmark test.

Below are the contents of my classes:

Program.cs




Now this class is very simple and just contains a couple of lines to run the benchmark test. Note that the Console.ReadKey() method is there to make sure that the console window does not disappear when the test is completed. 


MyFirstBenchMark.cs



Now this class is a little more complex and I will delve into the detail in future blog posts, but a few things to note:

1) The SimpleJob attribute is used to parameterise your benchmark test
2) The GlobalSetUp attribute should contain code that is run before the benchmark test is run 
3) The GlobalCleanup attribute contains code that should be run after the benchmark test has been run
4) The Benchmark attribute contains a method that runs the benchmark test.

This project will:
  • Open notepad (A set number of times - defined by the SimpleJob attribute)
  • Cleanup the test by closing down all of the instances of notepad.
One thing you need to do is run the project in Release mode and you can do this by changing the Debug drop-down to Release




When you run the project you will be greeted with the following:




Now there is more than what I have shown above as the console will display details of all the tests that were run and you should see notepad being open and closed during the test run. Above, you will see a Mean, Error and Standard Deviation details about the tests that were run. These details are also exported in csv, md and html and can be found in the \bin\Release\BenchmarkDotNet.Artifacts\results directory. This is basically the same as above but in a more importable friendly format. 

So there you go, a quick example of how to run a benchmark test against opening notepad.

Next time I will explain how the SimpleJob attribute works and what the various parameters do.

Please feel free to download the project from my GitHub page:

https://github.com/daveyboywardlaw/BenchMarkingExploration






Comments

Popular posts from this blog

Testing and Mindfulness

How aware are you? Do you live in the here and now or is your mind always somewhere else? This blog post is about Mindfulness. Mindfulness is a simple meditation and is defined as (According to Wikipedia): "The intentional, accepting and non-judgemental focus of one's attention on the emotions, thoughts and sensations occurring in the present moment" Now Mindfulness has become more popular in the west in recent years as it has shown to have benefits for people who are suffering from Depression and Anxiety. It has been around for a while and is often thought to of originated from Buddhism and some people believe it started thousands of years ago. Now modern life is hectic and I’m sure we all have lots of things going on in our lives that keep our Brains busy and trying to focus on one thing at a time can be a challenge. I can't remember the number of times I've been doing something and my mind is somewhere else entirely. Mindfuln...

How to deal with external data formats?

This blog post is in response to Challenge 7 of the #GivenWhenThenWithStyle blog post series with Gojko Adzic  You can see what the challenge is here: https://specflow.org/blog/how-to-deal-with-external-data-formats-givenwhenthenwithstyle/ Having the message format in the test would  (as mentioned in the challenge) look terrible and would be difficult to understand.  If I was to rewrite this scenario I would do something like the following: 1) Write a test that tests the validity of the payment message. So something like Scenario : payment message validity Given an account EE382200221020145685 has a balance When the system receives a payment message Then the payment message is valid.  I would store the actual payment message in another file as not to clutter the scenario file and this message would be passed into the test in the When line. The Then part of the test could cover various areas, such as: - All the correct tags are present  - The xml is valid 2) I wo...

TESTER PERCEPTION - PART 3

Its been a while since I have last blogged, its been hectic for one reason or another but I find myself with a bit more time so I can finish this series of blog posts. In this post I will complete the set of posts on testers perception. This post will describe ways in which testers can change how they are interpreted. Testers can change how they are interpreted by showing how they add value by changing their behaviours. Below are a few ways we can change how we are perceived as testers,  1) Don't shout louder, improve your argument Shouting louder than anyone else does not necessarily always work. We've all been there, you get quite heated in a discussion about a subject that you feel passionate about and someone else does the same and it all ends in disagreements and potentially damaged relationships. You need to think smarter. If you feel passionately about something on a project then do some research. Come up for valid reasons why you want something to be dif...