March 16

NBench Performance Testing – NUnit and ReSharper Integration

10  comments

You can run your NBench performance tests using NUnit and ReSharper.

All the logic to run performance tests in NBench is actually embedded in the core library and for this reason an external runner is not strictly required. You can easily integrate your unit testing framework and use your favorite tools to run your performance tests.

In this post, I show you how you can run NBench tests with NUnit and ReSharper.

Full source code is available here

How to create your NBench performance tests

You can create performance tests using attributes as usual.

The only difference is to inherit from a new base class.

PerformanceTestSuite in NBench

The PerformanceTestSuite base class

The abstract class PerformanceTestStuite<T> defines a parametric test in NUnit that run a single performance test (benchmark). The TestCaseSource attribute point to a method that returns all the performance tests so this single NUnit test basically runs all the performance tests defined in the type T.

PerformanceTestSuite to run NBench tests using NUnit and ReSharper

The Benchmarks method use an instance of the ReflectionDiscovery class to find all the benchmarks and then return a TestCaseData for each benchmark found.

The discovery class takes an IBenchmarkOutput instance that is used by the benchmark.Finish() method to notify the result of the performance test. For each benchmark NBench tells us all the assertion results and we can use those information to run a NUnit assert and print useful information to the console.

Integrate NBench with NUnit

Note: the reason why I had to use generics is because NUnit requires the TestCaseSource method to be static and in a static method I don’t have a way to get type information.

Running the tests in ReSharper

This is how running the tests looks like in the ReSharper test runner.

Show NBench tests results in ReSharper test runner

For a failing test, you get a clear error message.

Message from a failed NBench test

For a passing test, you still get information about the actual results of your performance tests.

Messaged from passed NBench tests

Conclusions

I think this is great! You can add this base class into your own project and very easily get your team to start writing automatic performance tests. Of course, the biggest problem is to define what to test but it’s great that now we have some tools that helps us to do it so easily. The learning curve is so slow.

NBench vision is to become a complete, user-friendly and fully extensible framework for automatic performance testing in .NET and more importantly, to make automatic performance testing a regular software development practice. I think this framework is a very good step forward in this direction.

NBench is an open source project. If you like the vision, you can help. Contribution is very welcome. The github page of the project is https://github.com/petabridge/NBench.

Previous posts

 

 


Tags


You may also like

      • You can easily adapt the super class to use xUnit as well. I can create a blog post about it. I created this class so it’s not part of NBench yet and there are many ways it can be improved. For example at the moment is picking measurements as well even if they probably should not. It’s a kind of an hack to integrate NBench with a unit test framework to proof that it is possible today. The idea of having separate package for NUnit or xUnit is not too bad, you can suggest it in the github project for consideration. Thanks

    • This gives me what I want for now :
      var discovery = new ReflectionDiscovery(new CompositeBenchmarkOutput(consoleOutput));

  • […] Obviously we run a build/continuous integration server, so we want to integrate these tests into our builds. Now it may be that you can use the NBench runner within your build, but (as mentioned in a previous post) there’s already a way to achieve this integration with the likes on NUnit (see NBench Performance Testing – NUnit and ReSharper Integration). […]

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Subscribe to our newsletter now!

    Get instant access to the Master C# 9 webinar to learn all the new exciting C# 9 features quickly.
    Get regular videos and news on C# and .NET and offers on products and services to master C#.

    We will collect, use and protect your data in accordance with our Privacy Policy.

    >