Performing a comparative test of a Web API we developed is the first step toward consciously optimizing our source code. For this reason, a few days ago we published an article dedicated to the benchmarking of a Web API with Postman, a useful tool that can be used to obtain quick and helpful measurements in just a few steps.
However, when it comes to performance, simply measuring the response time of a single endpoint is not enough: in real-world scenarios, APIs must handle concurrent loads, traffic spikes, or respond to sequential calls with very tight timing constraints. In such cases, tools like Postman or simple manual scripts are not sufficient.
NBomber is a powerful open-source load testing tool written in .NET that allows you to simulate complex scenarios and collect detailed performance metrics for your Web APIs. It is particularly suited for developers and teams working in the .NET ecosystem, but it supports any kind of HTTP endpoint. In this article, we will focus on the main features of NBomber and see how it can be used to create more in-depth benchmarks for our Web API endpoints.
Requirements
- Visual Studio or any compatible .NET editor (e.g., Rider, VS Code)
- A .NET 6+ console project
- NuGet package
NBomber.Http
- An API endpoint to test (e.g.,
https://localhost:5001/api/products
)
#1. Creating a Console Project
Open the terminal and create a new console project:
1 2 |
dotnet new console -n ApiLoadTest cd ApiLoadTest |
#2. Adding NBomber via NuGet
Add the required packages:
1 |
dotnet add package NBomber.Http |
#3. Test Scenario
Open the Program.cs
file and insert the following base code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using NBomber.Contracts; using NBomber.CSharp; using NBomber.Plugins.Http.CSharp; var step = Step.Create("GET /api/products", clientFactory: HttpClientFactory.Create(), execute: async context => { var request = Http.CreateRequest("GET", "https://localhost:5001/api/products") .WithHeader("Accept", "application/json"); var response = await Http.Send(request, context); return response; }); var scenario = ScenarioBuilder .CreateScenario("API Load Test", step) .WithWarmUpDuration(TimeSpan.FromSeconds(5)) .WithLoadSimulations(Simulation.KeepConstant(copies: 10, during: TimeSpan.FromSeconds(30))); NBomberRunner .RegisterScenarios(scenario) .Run(); |
In this example, NBomber will perform 10 simultaneous requests for 30 seconds, with a 5-second warm-up phase.
Customizable Parameters
- copies: number of simulated users
- duration: total duration of the test
- WithWarmUpDuration: warm-up time to exclude unstable initial metrics
#4. Running the Benchmark
Launch the test with:
1 |
dotnet run |
At the end of the execution, NBomber will display a detailed report in the console with:
- Total number of requests executed
- Percentiles (50th, 75th, 95th, 99th)
- RPS (Requests per second)
- Errors, timeouts, and more
NBomber can also automatically generate reports in HTML and JSON format.
#5. Exporting Results to HTML
To save the results into a readable HTML report, modify the runner configuration as follows:
1 2 3 4 5 6 7 8 |
NBomberRunner .RegisterScenarios(scenario) .WithReportFileName("api_load_test") .WithReportFolder("./reports") .WithReportFormats(ReportFormat.Html, ReportFormat.Md, ReportFormat.Txt) .Run(); |
You’ll find the report in the /reports
folder, ready to be shared with the team or integrated into your CI/CD pipeline.
What You Can Test with NBomber
- Performance under load (stress testing)
- Scalability under concurrent users
- Reliability of critical endpoints over time
- Resilience in case of errors or timeouts
Conclusions
NBomber is a powerful, flexible tool designed for realistic and repeatable performance testing scenarios. It’s perfect for those working in .NET environments but also want to test any HTTP API, thanks to its simple syntax and the quality of its generated reports.
If you’ve already performed basic testing with Postman and want to go deeper, NBomber is the ideal next step. And if you haven’t yet, we recommend reading the introductory guide: Quick Benchmarking of a Web API with Postman.