# net_rtt

The round-trip time between this box and a peer box over TCP: one small frame echoed per
sample on a kept-open connection, or one TCP handshake per sample, with every round trip
recorded on its own.

## What a figure means

Each sample is one round trip and nothing else (`repetitions` is 1). With `tcp_echo`, the
binary writes one frame of `size_bytes` (plus a 4-byte length) on a `TCP_NODELAY`
connection opened before the first sample, and the sample ends when the whole echo has
been read back. With `tcp_connect`, the sample is one `connect()`: SYN out, SYN-ACK back.

Method `runs` v1 reads the samples as any benchmark's: the time of one repetition
(`time_per_repetition_ns`) is the median round trip of the Run's counted samples. The
stream also holds an `op_histogram` (`metric` `rtt`) of every counted round trip, in
methods/requests v1's bins (each within 1/128 of the time), for a Method that reads tails.

## What a figure does not show

- Throughput: a frame is small and the connection idles between samples
  (see `net_throughput`).
- The peer's own time: the echo server answers as soon as it has read a frame, but its
  thread waking up is inside the round trip.
- Anything about other placements: `scope` names where the peer was put (same AZ, another
  AZ, loopback); AWS does not promise one path between two instances.

## Parameters

The benchmark's Series and their figures are listed at
[https://api.ledgermath.com/v1/benchmarks/net_rtt.json](https://api.ledgermath.com/v1/benchmarks/net_rtt.json).

| Variable | `--quick` default | Meaning |
|---|---|---|
| `LM_PARAM_SCOPE` | `loopback` | Where the peer is (`same_az`, `cross_az`, ...); part of the Series |
| `LM_PARAM_PROTOCOL` | `tcp_echo` | `tcp_echo` or `tcp_connect` |
| `LM_PARAM_SIZE_BYTES` | `64` | Payload bytes per frame, 1 to 65536 |
| `LM_PARAM_TIMEOUT_MS` | `5000` | Socket timeout of every send, read and connect |
| `LM_PARAM_GAP_MS` | `5` | Pause between samples, outside them |

Service inputs: `LM_SERVICE_PEER_HOST` (an IP literal, or a name resolved through
`LM_PARAM_RESOLV_CONF`, default `/etc/resolv.conf`) and `LM_SERVICE_PEER_PORT`. The peer
runs the same binary as `net_rtt --serve <port> [<bind address>]`. Under `--quick` with no
peer, the binary serves itself on `127.0.0.1`.

## Observed values

| Key | What it is |
|---|---|
| `protocol`, `size_bytes` | What each sample did (`size_bytes` is 0 for `tcp_connect`) |
| `peer_address`, `peer_port`, `local_address` | The addresses connected |
| `nodelay` | Whether `TCP_NODELAY` was in force |
| `bytes_per_repetition` | Bytes one echo sends and reads back, frame headers included |
| `warmup_samples` | Samples before the counted ones (5 for `tcp_echo`, 2 for `tcp_connect`) |

A `tcp_info` event after the samples holds the kernel's view of the connection (smoothed
RTT, minimum RTT, path MTU, retransmissions).

## Findings

No finding of its own so far.

## How it works

Each frame is the Fixture pattern at an offset of its sample's own, so an echo of an older
frame or of other bytes is caught. After each sample, outside it, every echoed byte is
compared with what was sent (a difference exits 1) and FNV-1a'd into `verified`. Before the
first sample one echo is checked, so a peer that is not a `net_rtt` server is refused
before the stream has samples. A timeout exits 1.

## Building and testing

```sh
make BUILD=static
./build/static/net_rtt --quick | python3 ../../scripts/check-events.py --quick
bun test tests/net-and-ec2-benchmarks.test.ts
```

---

Authorship: Written entirely by AI.
