Automating performance testing of core C# code - BenchmarkDotNet

 BenchmarkDotNet is a free nuget package you can use to automatically performance test your core C# code.

Requirement: the code must be built as a CLI (command line interface) EXE. Assuming you practice Clean Architecture, this should be no problem, since your business logic is not coupled to externals such as UI, database or framework.

By also install R (rscript), then BenchmarkDotNet can be very simply configured to generate charts. The charts make comparing performance results much easier - it is often clearer if a performance run has any significant difference versus another performance run, by comparing charts rather than tables. To add the chart generation, simply add the property [RPlotExporter] to the benchmark entry class.

Since the performance run is checked via a command line, it is very simple to script and then even to execute as part of a CI/CD (Continuous Integration/Continuous Deployment) job. The results are output in a variety of formats, so you could write a script to post-process the results, writing them to a blog or a time series database etc.

 

Tips:

1 - Use a fixed job name, to make BenchmarkDotNet always build its instrumented assemblies to the same sub-directory. To set the job name, use the Id attribute of the SimpleJob C# property:

[SimpleJob(Id="bm1")]
This can simplify debugging, and also avoids consuming more disk space if you interrupt job execution.

2 - In a complex code-base, it can occur that some dependency projects assemblies are not copied over. In this case, a pre-run step can be added to your script, to simply xcopy the assemblies before running BenchmarkDotNet. This is possible if you use a fixed job name (see tip 1).

 

References:

- github repo 

- documentation

- CODE magazine article


Comments