HTTP Performance Testing with NBomber in C# .NET💥
Performance testing is crucial for ensuring that your web applications can handle real-world traffic. In this article, we’ll deep dive into HTTP performance testing using NBomber in C# .NET. We’ll explore how to set up NBomber, write simple scenario with XUnit, assertion, run test and analyse HTML report generated from NBomber.
What is NBomber?
NBomber is a powerful and flexible load testing framework for .NET. It is modern and flexible 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).
With NBomber you can convert some of your integration tests to load tests easily.
How to setup NBomber?
Just add reference to Nuget package into your project and you are good to go!
There are heaps of plugins available in NBomber, which does quite a lot of magics for you, specific to the scenario that you are performance testing for, in this example we will performance test HTTP services, hence the library you need to add is this
<PackageReference Include="NBomber.Http" Version="5.1.0" />
Writing simple scenario
We are going to write a super simple scenario for NBomber.Http load test something like this
[Fact]
public async Task HttpPerformanceTest()
{
//Create instance for HttpClient
using var httpClient = new HttpClient();
var scenario = Scenario.Create("Http-Load-Test", async scenarioContext =>
{
var httpRequest = Http.CreateRequest("GET", "http://localhost:8001/Product/GetProductById/3")
.WithHeader("content-type", "application/json");
var httpResponse = await Http.Send(httpClient, httpRequest);
return httpResponse;
}).WithoutWarmUp()
.WithLoadSimulations(LoadSimulation.NewInject(50, TimeSpan.FromMilliseconds(10), TimeSpan.FromMinutes(1)));
var result = NBomberRunner.RegisterScenarios(scenario).Run();
Assert.True(result.ScenarioStats.Get("Http-Load-Test").Ok.Latency.Percent99 < 10);
}
In NBomber, we can create multiple scenario to be executed and each scenarios can have multiple steps
The code snippet above create a scenario to perform load testing of our HTTP API logics.
var scenario = Scenario.Create("Http-Load-Test", async scenarioContext =>
{
//Steps
});
These two lines below performs sending the HTTP GET request to the application
var httpRequest = Http.CreateRequest("GET", "http://localhost:8001/Product/GetProductById/3")
.WithHeader("content-type", "application/json");
var httpResponse = await Http.Send(httpClient, httpRequest);
We can run the test WithWarmUp() and WithoutWarmUp() based on your scenario.
Load Simulations
The most important part to run your tests with NBombers are based on Load simulations. Here are the Load simulation types and their use cases
Reporting
Once the test execution completes with dotnet run command, we get a nice looking report something like this
dotnet run