Overview

Welcome to NBomber! This tutorial will explore the basics of using NBomber and help you get familiar with it at a high level.

Why we build NBomber and what you can do with it?

  1. The main reason behind NBomber is to provide a lightweight framework for writing load tests which you can use to test literally any system and simulate any production workload. We wanted to provide only a few abstractions so that we could describe any type of load and still have a simple, intuitive API.
  2. Another goal is to provide building blocks to validate your POC (proof of concept) projects by applying any complex load distribution.
  3. With NBomber you can test any PULL or PUSH system (HTTP, WebSockets, GraphQl, gRPC, SQL Databse, MongoDb, Redis etc).

NBomber as a modern framework provides:

Step by step introduction

note

Installation prerequisites

Create console application project

dotnet new console -n [project_name] -lang ["F#" or "C#"]
dotnet new console -n NBomberTest -lang "F#"
cd NBomberTest

Add NBomber package

dotnet add package NBomber

Create hello world load test

Let's fist start with an empty hello world example to get more familiar with NBomber. In this example, we will define one simple Step and Scenario which does nothing.

Program.fs
open System
open System.Threading.Tasks
open FSharp.Control.Tasks.V2.ContextInsensitive
open NBomber.Contracts
open NBomber.FSharp
[<EntryPoint>]
let main argv =
// first, you need to create a step
// step is a basic element of every scenario which will be executed and measured
// every step is running in separated lightweight Task
// you can run concurrently hundreds steps - it's cheap
let step = Step.create("step", fun context -> task {
// you can define and execute any logic here,
// for example: send http request, SQL query etc
// NBomber will measure how much time it takes to execute your step
do! Task.Delay(seconds 1)
return Response.Ok()
})
// second, we add our step to the scenario
// scenario is basically a container for steps
// you can think of scenario like a job of sequential operations
// you can add several steps into one scenario
Scenario.create "hello_world" [step]
|> NBomberRunner.registerScenario
|> NBomberRunner.run
|> ignore
0 // return an integer exit code

Run load test

dotnet run -c Release

After running a test you will get report. Don't get scared, we can skip it for now. Later we will understand how to analyse such reports.

Create simple HTTP load test

Now, let's add HTTP client to test a web server and then run it.

Program.fs
open System
open System.Net.Http
open FSharp.Control.Tasks.V2.ContextInsensitive
open NBomber.Contracts
open NBomber.FSharp
[<EntryPoint>]
let main argv =
use httpClient = new HttpClient()
let step = Step.create("step", fun context -> task {
let! response = httpClient.GetAsync("https://nbomber.com",
context.CancellationToken)
match response.IsSuccessStatusCode with
| true -> return Response.Ok()
| false -> return Response.Fail()
})
Scenario.create "hello_world" [step]
|> NBomberRunner.registerScenario
|> NBomberRunner.run
|> ignore
0 // return an integer exit code

Create production-ready HTTP load test

Now, you got a basic understanding of NBomber and ready to move on. This time we will use:

  • NBomber.HTTP plugin to simplify defining and handling of HTTP
  • NBomber.PingPlugin to add additional reporting
  • Specify load simulation

So, we only need to install missed NBomber.Http package (NBomber.PingPlugin is included as part of NBomber, we don't need to install it).

dotnet add package NBomber.Http
Program.fs
open System
open System.Net.Http
open FSharp.Control.Tasks.V2.ContextInsensitive
open NBomber.Contracts
open NBomber.FSharp
open NBomber.Plugins.Network.Ping
open NBomber.Plugins.Http.FSharp
[<EntryPoint>]
let main argv =
// it's optional Ping plugin that brings additional reporting data
let pingConfig = PingPluginConfig.CreateDefault ["nbomber.com"]
use pingPlugin = new PingPlugin(pingConfig)
// now you use HttpStep instead of NBomber's default Step
let step = HttpStep.create("fetch_html_page", fun context ->
Http.createRequest "GET" "https://nbomber.com"
)
Scenario.create "hello_world" [step]
|> Scenario.withLoadSimulations [
InjectPerSec(rate = 100, during = seconds 10)
]
|> NBomberRunner.registerScenario
|> NBomberRunner.withPlugins [pingPlugin]
|> NBomberRunner.run
|> ignore
0 // return an integer exit code

Congratulations! You have done it!

Finally, you reach this point! Here you can find additional information which helps you in building real world NBomber tests: