.NET Load Testing With NBomber
NBomber is an open source load testing framework focused on providing .NET developer the ability to load test their applications. One of the main features of NBomber is to provide the ability to test any system such as a database query, API call, or message broker.
Getting Started
- Create .NET Core console application
- Install the NBomber nuget
- Modify project file
- Define test scenario
- Define test steps
- Bootstrap NBomber and run test
Create .NET Core Console Application
For the purpose of this article I’ll create a simple console application that will test calling an API. The API will only have MOC data as its not the main focus of the article and all code can be found here
Start by creating a .NET Core Console Application
Install NBomber Nuget
For this post I’ll be using two packages:
- NBomber version 2.0.1
- NBomber.Http version 2.0.0
Install the two packages via the package manager in visual studio or via the package manager console by running the commands:
- Install-Package NBomber -Version 2.0.1
- Install-Package NBomber.Http -Version 2.0.0
Modify Project File
To get the most out of NBomber you should use the latest version of .NET core and you should also modify your project file to switch GarbageCollection to server and concurrent mode. To do this you’ll need to add the following settings
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
The full project file will look similar to:
Define Test Step
Steps define want functionality you want to test such as calling an API. In this example I’ll define a single step to call the api endpoint “api/users”.
Define Test Scenario
Scenarios let you configure how long the test will run, how times the step will execute per second, and how long the warm up will be. The below code setups a warm up duration of 1 minute, 1 request per second and the test will run for 11 minutes (1 minute for the warmup, 10 minutes for the actual test)
Configure NBomber Runner & Reports
Before running the application you need to bootstrap NBomber and register the previously defined scenario. Along with registering the scenario you can also define what report formats you want and where the reports should be saved. NBomber supports 4 report formats:
- Text
- CSV
- HTML
- Markdown
Reports
While the application is running it will write some basic telemetry to the console about the progress of the tests
Once the application completes the console will display another report with a summary of the succeeded / failed steps and some information about the latency for each step.
Lastly if you configured report formats during the bootstraping step you’ll have 1 or more reports generated which give additional details about the test execution. For this demo the reports can be found in the folder “fetch_users_reports” which is in the bin directory.
Git Repo
The full project can be found here https://github.com/WilliamRees/nbomber-demo