tiktoken icon indicating copy to clipboard operation
tiktoken copied to clipboard

bench: add tail-latency benchmark for encode_ordinary_batch

Open alobroke opened this issue 6 months ago • 0 comments

Motivated by #530, which reported worst-of-10 tail spikes of 1.1×–7.6× over median on encode_ordinary_batch. The issue author offered to PR a benchmark harness — this is that harness.

Problem

The existing scripts/benchmark.py measures throughput only (bytes/sec). Throughput numbers hide tail latency completely — a run that takes 7× longer than median still averages out fine across many runs.

What this adds

scripts/benchmark_tail_latency.py — a self-contained tail-latency harness that:

  • Measures median and worst-of-N wall-clock time per corpus
  • Reports the worst/median ratio so tail spikes are immediately visible
  • Tests four synthetic corpora generated at runtime (no data files needed):
    • english prose
    • python source
    • multilingual + emoji
    • random ascii
  • Accepts CLI flags for --runs, --batch-size, --encoding, --threads

Example output

encoding: o200k_base | batch_size: 64 | runs: 10 ── num_threads=8 ────────────────────────────────────── corpus tokens/batch median ms worst ms worst/med english prose 2,560,000 240 980 4.1x python source 4,480,000 580 950 1.6x multilingual+emoji 5,120,000 1020 2100 2.1x random ascii 7,680,000 680 780 1.1x

Usage

python scripts/benchmark_tail_latency.py
python scripts/benchmark_tail_latency.py --runs 10 --batch-size 256 --encoding o200k_base --threads 1,4,8

Fixes #530 (benchmark harness portion).

alobroke avatar May 20 '26 21:05 alobroke