Yesterday I talked about Bombardier, an HTTP benchmarking tool written in Go and how you can use it to test the performance of your ASP.NET Core applications. While discussing the usage, a colleague mentioned another load testing tool, NBomber.
NBomber is a load-testing framework for Pull and Push scenarios, designed to test any system regardless of a protocol (HTTP/WebSockets/AMQP, etc) or a semantic model (Pull/Push).
It goes a lot further than what Bombardier has to offer. One of the things I find nice is that load test scenario’s can be written using C#. 😉
Remark: NBomber comes with a free personal license, if you want to use it inside your organization, you’ll have to buy a business license.
Let’s write a simple load test to test our API!
- We start by creating a new Console application and adding the NBomber nuget package:
- dotnet add package NBomber
- As we want to test an HTTP endpoint, let’s also add the NBomber.Http plugin to simplify defining and handling of HTTP load tests:
- dotnet add package NBomber.Http
- Now we can start writing our test scenario. Here is a simple example scenario where we call our API endpoint simulating 100 users:
using NBomber.CSharp; | |
using NBomber.Http.CSharp; | |
using var httpClient = new HttpClient(); | |
var scenario = Scenario.Create("call_http_api_scenario", async context => | |
{ | |
var request = | |
Http.CreateRequest("GET", "http://localhost:5042/WeatherForecast") | |
.WithHeader("Accept", "application/json"); | |
var response = await Http.Send(httpClient, request); | |
return response; | |
}) | |
.WithWarmUpDuration(TimeSpan.FromSeconds(2)) | |
.WithLoadSimulations( | |
Simulation.Inject(rate: 100, | |
interval: TimeSpan.FromSeconds(1), | |
during: TimeSpan.FromSeconds(30)) | |
); | |
NBomberRunner | |
.RegisterScenarios(scenario) | |
.Run(); | |
- To run the scenario, we only have to run the console app:
- After test completion results, results are written in multiple formats to a folder.
- Here is how the HTML report looks like after the test run;
Want to learn more? Check out this video introduction: