API Load Testing using .NET NBomber

NBomber is an open source load testing framework which help .NET developers to load test their applications.

Published on Thursday, May 26, 2022

NBomber is an open source load testing framework which help .NET developers to load test their applications. NBomber is a lightweight framework for writing load tests which you can use to test literally any system and simulate any production workload. With NBomber you can test any PULL or PUSH system (HTTP, WebSockets, GraphQl, gRPC, SQL Database, MongoDb, Redis etc).

In this post, I'll show you a very simple example of API load testing using NBomber C#.

Step 1: Create WebAPI for Load Test

To avoid network latency, I have created sample WebAPI project from visual studio for load test which is accessible using http://localhost:5104/WeatherForecast endpoint. you can use your existing application endpoint to do load test.

Create Load Test Application

There are two ways to run a test with NBomber:

  • XUnit or NUnit: You can write your NBomber load test same as your Unit/Integration test.
  • Console application: You can run NBomber load test using console application which will shows result in console window output. You can write only one test with this approach.

To keep it simple, I have created simple C# console application for NBomber load test and add NBomber nuget package on it.

Setup API Load Test

There are few important building blocks of NBomber:

  • Step: Step and Scenario play the most important role in building real-world simulations with NBomber. To represent users behaviors, testers should define scenarios with steps. The scenario is basically a workflow that virtual users will follow. The step represents a single user action like login, logout, etc.
  • Scenario: Scenario is basically a workflow that virtual users will follow. It helps you organize steps into user actions.
  • NBomberRunner: NBomberRunner is responsible for registering and running scenarios under Test Suite. Also it provides configuration points related to infrastructure, reporting, loading plugins.

By using all three build blocks, I have written my load test code to send 200 request per-second for 30 seconds duration.

NBomber Load Test Result

While the application is running it will write some basic telemetry to the console about the progress of the tests. Once load test complete, console will show you the result of load test with succeeded, failed and latency for each step.

NBomber also generates HTML report which you can find from NBomber console result window.

Conclusion

I used to write multi-thread custom code to do a basic load test of my application. I really liked NBomber and I'll share more sophisticated example in my future post.

Happy coding.

Related posts

Handling Partial Batch Failure When Processing SQS Messages with a Lambda Function

Create custom project template in Visual Studio

How to Deserialize DynamoDb stream JSON to Object in .NET Lambda?

Run .NET Lambda Function Locally Using LocalStack

Amazon S3 local development using LocalStack