wrolpi/scripts/api_perf_compare.py
lrnselfreliance a61d528832
API benchmark: run history and regression detection (#1084)
* API benchmark: run history and regression detection

api_perf.py --record saves each run to a SQLite history kept outside the
repository (--data-dir, $WROLPI_BENCH_DIR, or ~/.local/share/wrolpi-bench) and
reports endpoints that got slower than their own baseline: the median of the
last 5 trustworthy runs, flagged at 1.5x and +100 ms, with hot regressions
re-timed before they are reported.  Also reports slow drift, errors, slow
endpoints, response growth, library growth, and the commits between the
baseline and the run that touched related code.

Runs on a busy host (downloads, load above core count) or during which the API
was unavailable are recorded but never used as baselines.  Host profiles can
live in the data dir so LAN addresses stay out of every checkout; a profile
marked production refuses --cold without --restart-production.

The catalog drops the removed ai/chat endpoints and adds repos, bookmarks,
jobs, torrents, sync, speedtest, map and video description endpoints.
test_api_perf.py fails when a GET route is neither timed nor deliberately
excluded, so new endpoints are not silently left out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* API benchmark: a 5xx is an endpoint error, not an unavailable API

Sanic answers 503 when a single request exceeds its response timeout; that
endpoint is reported as an error and the run stays a usable baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 17:16:23 -07:00

109 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""
Merge the CSVs written by scripts/api_perf.py into one cross-host table.
python3 scripts/api_perf_compare.py ~/.local/share/wrolpi-bench/runs/*.csv
python3 scripts/api_perf_compare.py ~/.local/share/wrolpi-bench/runs/*.csv --sort hot --top 25
python3 scripts/api_perf_compare.py ~/.local/share/wrolpi-bench/runs/*.csv --markdown > summary.md
For each endpoint and host prints cold (ms, first request after an API restart) and hot median
(ms, over the hot passes). When several CSVs exist for the same host the newest one wins.
"""
import argparse
import csv
import statistics
from collections import defaultdict
from pathlib import Path
def load(paths):
# host -> newest file
newest = {}
for p in paths:
host = p.stem.rsplit('_', 2)[0]
if host not in newest or p.stat().st_mtime > newest[host].stat().st_mtime:
newest[host] = p
data = defaultdict(lambda: defaultdict(lambda: dict(cold=None, cold_each=None, hot=[], status=set())))
order = []
for host, p in sorted(newest.items()):
with open(p) as f:
for row in csv.DictReader(f):
d = data[row['name']][host]
ms = float(row['ms'])
if row['phase'] == 'cold':
d['cold'] = ms
elif row['phase'] == 'cold-each':
d['cold_each'] = ms
elif row['phase'] == 'hot':
d['hot'].append(ms)
d['status'].add(row['status'])
if row['name'] not in order:
order.append(row['name'])
return sorted(newest), data, order
def fmt(v):
if v is None:
return '-'
return f'{v:,.0f}'
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument('csvs', nargs='+', type=Path)
ap.add_argument('--sort', choices=['catalog', 'hot', 'cold'], default='catalog',
help='catalog order (default), or by worst hot median / worst cold across hosts.')
ap.add_argument('--top', type=int, help='Only print the first N rows after sorting.')
ap.add_argument('--markdown', action='store_true')
args = ap.parse_args()
hosts, data, order = load(args.csvs)
def hot_med(name, host):
h = data[name][host]['hot']
return statistics.median(h) if h else None
def worst(name, key):
vals = [data[name][h][key] if key == 'cold' else hot_med(name, h) for h in hosts]
vals = [v for v in vals if v is not None]
return max(vals) if vals else -1
names = list(order)
if args.sort == 'hot':
names.sort(key=lambda n: -worst(n, 'hot'))
elif args.sort == 'cold':
names.sort(key=lambda n: -worst(n, 'cold'))
if args.top:
names = names[:args.top]
cols = ['endpoint'] + [f'{h} cold' for h in hosts] + [f'{h} hot' for h in hosts]
rows = []
for n in names:
row = [n]
for h in hosts:
d = data[n][h]
c = d['cold_each'] if d['cold_each'] is not None else d['cold']
bad = d['status'] and d['status'] != {'200'}
row.append(fmt(c) + ('*' if d['cold_each'] is not None else '') + (' !' if bad else ''))
for h in hosts:
row.append(fmt(hot_med(n, h)))
rows.append(row)
if args.markdown:
print('| ' + ' | '.join(cols) + ' |')
print('|' + '|'.join(['---'] + ['---:'] * (len(cols) - 1)) + '|')
for r in rows:
print('| ' + ' | '.join(r) + ' |')
else:
widths = [max(len(str(x)) for x in col) for col in zip(cols, *rows)]
line = ' '.join(c.ljust(w) if i == 0 else c.rjust(w) for i, (c, w) in enumerate(zip(cols, widths)))
print(line)
print('-' * len(line))
for r in rows:
print(' '.join(c.ljust(w) if i == 0 else c.rjust(w) for i, (c, w) in enumerate(zip(r, widths))))
print('\nms; cold = first request after API restart (* = own restart via --cold-each); hot = median of hot passes;'
' ! = non-200 seen')
if __name__ == '__main__':
main()