> For the complete documentation index, see [llms.txt](https://docs.cennso.com/test-tools/tpload/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/test-tools/tpload/latest/interactive-mode/benchmarking.md).

# Run the benchmark test using TPLoad CLI

TPLoad CLI supports the `benchmark` command to measure the throughput between left and right Generators or the session setup rate of the Packet Gateway (PGW).

Benchmark test has three phases:
1. **WarmUp phase** - create requested number of sessions and warm up the system under test (SUT) with `session-setup-rate` during `hold-time`.
2. **Search phase** - depending on the search function type (linear or binary), detect the maximum throughput with the given `lost-acceptable` drop rate.
3. **TearDown phase** - delete all created sessions and report the result.

## Flags

Both types of the search function have flags that can be divided into four groups: general, benchmark, common, and function-specific (for the linear and binary search groups respectively).

### General group

The general group contains flags for connecting to TPLoad and selecting the search function:

| Flag | Description | Default value |
|---|---|---|
| `--server` | Endpoint of the TPLoad API | `"127.0.0.1:8900"` |
| `--metrics-endpoint` | Endpoint of the TPLoad metrics | `"127.0.0.1:9106"` |
| `--output-file` | Sets a path to save all data points, the test result, and benchmark parameters. | `./result.json` |
| `--type` | Type of the search function. The possible values are `linear-search`, `binary-search`, and `setup-rate`. | `linear-search` |
| `--config`, `-c` | Path to a YAML configuration file that predefines any of the benchmark flags. See [Configuration file](#configuration-file). | `""` |

### Control Plane group

The Control Plane group contains flags that control the sessions used in the benchmark test:

| Flag | Description | Default value |
|---|---|---|
| `--concurrent-sessions` | Sets the number of sessions for the benchmark test. | `250 000` |
| `--sessions-pool-size` | Size of the whole sessions pool, for example: the number of unique sessions. | `0` |
| `--session-setup-rate` | Rate of the session creation, deletion, and the session churn, if enabled. | `250` |
| `--session-churn` | Enables the session churn for the benchmark test. | `false` |
| `--hold-time` | Sets the duration of the monitored benchmark tick. | `1m` |

### Data Plane group

The Data Plane group contains flags that control the traffic generated during the benchmark test:

| Flag | Description | Default value |
|---|---|---|
| `--forwarding-rate` | Specifies the start forwarding rate for the Generators. | `100 kpps` |
| `--forwarding-ratio` | Specifies the ratio between download and upload forwarding rates. `9` means `9:1`, for example: `90kpps` download, `10kpps` upload for forwarding-rate 100kpps. | `9` |
| `--traffic-profile` | Sets the traffic profile for the benchmark test. The traffic profile must be present in the configuration. | `imix` |
| `--burst-profile` | Sets the burst profile for the benchmark test. The burst profile must be present in the configuration. | `user_common` |
| `--flows` | Sets a number of the traffic flows per session. | `1` |
| `--lost-acceptable` | Sets the acceptable drop rate in the boundaries of 0 \< acceptable \< 100. | `0.3` |

### Linear search group

The linear search group contains the flag specific to the linear search function:

| Flag | Description | Default value |
|---|---|---|
| `--forwarding-rate-step` | Sets the rate increment for each step. The value is provided in `pps`. | `50 kpps` |

### Binary search group

The binary search group contains the flag specific to the binary search function:

| Flag | Description | Default value |
|---|---|---|
| `--convergence` | Convergence factor for binary search stop, for example, a rate multiplier diff between steps | `0.1` |

### Rate setup group

The rate setup group contains flags specific to the session setup rate benchmark:

| Flag | Description | Default value |
|---|---|---|
| `--success-rate` | Minimal success threshold for the session setup rate benchmark. The value is between 0-100%. | `99.99` |
| `--step-rate-size` | Rate increment for the session rate measurement. | `25` |

## Configuration file

Instead of passing every flag on the command line, you can predefine a benchmark run in a YAML file and point to it with `--config` (or `-c`):

```bash
tp-qa-tools tpload-cli benchmark --config ~/pps-bench.yaml
```

Each key in the file matches a benchmark flag name (the same name as the long flag, without the leading `--`). For example, the following command:

```bash
tp-qa-tools tpload-cli benchmark --session-setup-rate 50 --concurrent-sessions 1000 \
  --flows 1 --forwarding-ratio 1 --traffic-profile pps --type binary-search \
  --lost-acceptable 0.3 --output-file /tmp/benchmark-pps-25k-1-binary-search.json
```

is equivalent to the following `~/pps-bench.yaml`:

```yaml
session-setup-rate: 50
concurrent-sessions: 1000
flows: 1
forwarding-ratio: 1
traffic-profile: pps
type: binary-search
lost-acceptable: 0.3
output-file: /tmp/benchmark-pps-25k-1-binary-search.json
```

Notes:
- **Command-line flags override the configuration file.** This lets you keep a base configuration and tweak individual values, for example: `--config ~/pps-bench.yaml --concurrent-sessions 5000`.
- **Unknown keys are rejected.** A key that does not match a benchmark flag aborts the run with an error, which helps catch typos.
- **Missing or malformed files are rejected** with a descriptive error.
- Any flag omitted from the file keeps its default value.

## Usage

### WarmUp phase

During this phase, the benchmark test:
1. Creates the `--concurrent-sessions` number of sessions at the `--session-setup-rate` rate.
2. For the `--hold-time`, generates traffic with the given traffic and burst profiles at the `--forwarding-rate` rate.
3. After the `--hold-time`, the WarmUp phase stops.

### Search phase

The search phase can use two different types of the search function that you can specify with the `--type` flag.

#### Linear search

Linear search function searches the maximum rate with the sequential increase of the throughput from `--forwarding-rate` with the `--forwarding-rate-step` size.

![Linear search rate graph](https://docs.cennso.com/api/assets/test-tools/tpload/latest/assets/benchmark-linear.svg "Linear search rate graph")

During this phase, the function:
1. Sets the throughput to the `--forwarding-rate`.
2. Gathers traffic statistics every 10 seconds for `--hold-time.`
3. Takes a decision about the next step:
    - If more than half of the points have a combined drop rate (upload + download) lower than `--lost-acceptable`, add the `--forwarding-rate-step` to the current rate.
    - If more than half of the points have a combined drop rate higher than `--lost-acceptable`, take the statistics from the previous step as the result.

#### Binary search

Binary search function contains two steps: 
1. Search the maximum rate with the sequential increase of the throughput by two. 
2. Search for the throughput with the given `--lost-acceptable` between 0 and found `Max`.

![Binary search rate graph](https://docs.cennso.com/api/assets/test-tools/tpload/latest/assets/benchmark-binary.svg "Binary search rate graph")

During this phase, the function:
1. Sets the throughput to the `--forwarding-rate`.
2. Gathers traffic statistics every 10 seconds for `--hold-time`.
3. Takes a decision of the next step:
   - If more than half of the points have a combined drop rate (upload + download) lower than `--lost-acceptable`, multiply the current rate by two.
   - If more than half of the points have a combined drop rate higher than `--lost-acceptable`, take the current rate as `Max`.
4. Starts searching for the rate. Sets the rate to the half between [`0`, `Max`].
5. Gathers traffic statistics every 10 seconds for `--hold-time`.
6. Takes a decision about the next step:
   - If more than half of the points have combined drop rate (upload + download) lower than `--lost-acceptable`, set the current rate to half between [`current_rate`, `Max`].
   - If more than half of the points have a combined drop rate higher than `--lost-acceptable`, set the current rate to half between [`0`, `current_rate`].
7. Checks the difference between `current_rate` from this and previous step.
   - If the difference is lower than `--convergence`, stop the test and take the result.
   - If the difference is higher, continue the search.

#### Rate setup

Rate setup function measures the maximum setup rate of the Packet Gateway (PGW) with the sequential increase of the CSR-DSR rate from `--session-setup-rate` with the `--step-rate-size`. This benchmark requires the session churn enabled.

During this phase, the function:
1. Sets the GTPv2 churn rate to the `--session-setup-rate`.
2. Gathers request statistics every 10 seconds for `--hold-time.`
3. Decides about the next step:
    - If the average success rate for requests is higher than `--success-rate`, add the `--step-rate-size` to the current rate.
    - If the average success rate for requests is lower than `--success-rate`, take the statistics from the previous step as the result.

### TearDown phase

During the TearDown phase, the benchmark test terminates the sessions at the `--session-setup-rate` rate and writes raw and human-readable data to the `--output-file`.

## Examples

Measure the throughput with the linear search benchmark, with the session churn enabled:
```bash
tp-qa-tools tpload-cli benchmark --session-churn --session-setup-rate 250 \
  --concurrent-sessions 250000 --sessions-pool-size 260000 --type linear-search \
  --lost-acceptable 0.3 --output-file ./linear-search.json
```

Measure the throughput with the binary search benchmark, with the session churn enabled:
```bash
tp-qa-tools tpload-cli benchmark --session-churn --session-setup-rate 250 \
  --concurrent-sessions 250000 --sessions-pool-size 260000 --type binary-search \
  --lost-acceptable 0.3 --output-file ./binary-search.json
```

Measure the maximum session setup rate:
```bash
tp-qa-tools tpload-cli benchmark --session-churn --session-setup-rate 250 \
  --concurrent-sessions 250000 --sessions-pool-size 260000 --type setup-rate \
  --success-rate 99.99 --step-rate-size 25 --output-file ./rate-setup-search.json
```

Run a benchmark predefined in a configuration file (see [Configuration file](#configuration-file)):
```bash
tp-qa-tools tpload-cli benchmark --config ~/pps-bench.yaml
```

### Result example 

Here is the example of the test result:

```json
{
 "tp_load_endpoint": "127.0.0.1:8900",
 "metrics_endpoint": "localhost:9106",
 "traffic_profile": "imix",
 "burst_profile": "user_common",
 "sessions_count": 10000,
 "warm_up_rate": "1 kpps",
 "warm_up_duration": "2m",
 "output_file": "./result.json",
 "stats_container": {
  "start_time": 1693403927,
  "stats": [
   {
    "timestamp": "2023-08-30T13:58:47.959295379Z",
    "time_from_start": 0,
    "upload_tx": 0,
    "upload_rx": 0,
    "download_tx": 0,
    "download_rx": 0,
    "upload_tx_rate": 0,
    "upload_rx_rate": 0,
    "download_tx_rate": 0,
    "download_rx_rate": 0,
    "download_drop_rate": 0,
    "upload_drop_rate": 0,
    "combined_drop_rate": 0,
    "rate_multiplier": 0
   },
   {
    "timestamp": "2023-08-30T13:58:57.967958202Z",
    "time_from_start": 10.967958485,
    "upload_tx": 5112,
    "upload_rx": 5099,
    "download_tx": 5092,
    "download_rx": 5102,
    "upload_tx_rate": 510.75753978369386,
    "upload_rx_rate": 509.4586649759497,
    "download_tx_rate": 508.7592708487029,
    "download_rx_rate": 509.7584053161984,
    "download_drop_rate": -0.001374705420267075,
    "upload_drop_rate": 0.001956181533646295,
    "combined_drop_rate": 0.0002940023520188184,
    "rate_multiplier": 1
   },
...
   {
    "timestamp": "2023-08-30T14:45:51.355243639Z",
    "time_from_start": 2824.355243895,
    "upload_tx": 763023849,
    "upload_rx": 762550336,
    "download_tx": 762655201,
    "download_rx": 678385206,
    "upload_tx_rate": 389118.1441045593,
    "upload_rx_rate": 389073.3755890028,
    "download_tx_rate": 388985.73722261196,
    "download_rx_rate": 306492.7522291543,
    "download_drop_rate": -0.00022529969097728753,
    "upload_drop_rate": 0.21234011604764147,
    "combined_drop_rate": 0.10607549388936843,
    "rate_multiplier": 1.9500000000000008
   }
  ],
  "end_time": 1693406761
 },
 "result": {
  "timestamp": "2023-08-30T14:43:51.215166787Z",
  "time_from_start": 2639.147552778,
  "upload_tx": 57598368.15972222,
  "upload_rx": 691180417.9166666,
  "download_tx": 691254869.25,
  "download_rx": 622169410.1666666,
  "upload_tx_rate": 378945.28986058966,
  "upload_rx_rate": 378781.98204906745,
  "download_tx_rate": 378678.8327384286,
  "download_rx_rate": 309187.20318667515,
  "download_drop_rate": -0.00027766094581133105,
  "upload_drop_rate": 0.18408108844185941,
  "combined_drop_rate": 0.09193357638574444,
  "rate_multiplier": 1.900000000000001
 },
 "human_readable_result": {
  "timestamp": "2023-08-30 14:43:51.215166787 +0000 UTC m=+2703.313306911",
  "upload_tx_rate": "378.95 Kpps",
  "download_rx_rate": "309.19 Kpps",
  "download_drop_rate": "-0.03%",
  "upload_rx_rate": "378.78 Kpps",
  "download_tx_rate": "378.68 Kpps",
  "upload_drop_rate": "18.41%",
  "total_tx_rate": "757.62 Kpps",
  "total_rx_rate": "687.97 Kpps",
  "total_drop_rate": "9.19%",
  "rate_multiplier": "1.900000000000001"
 },
 "search_interface": {
  "type": "",
  "start_rate": "200kpps",
  "step": 0.05,
  "hold_time": "2m",
  "lost_acceptable": 0.1
 }
}
```
