mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
* feat(perf): add structured summary/record builders to analyzer parse_log_files() already returns a fully-structured PerfReport, but the only way to read it was the colored text report. Add reusable machine-readable views so CI guards, dashboards, and agent harnesses can consume perf data without scraping ANSI text: - build_perf_summary(report) -> dict with the aggregated KPIs (savings_pct, cache_hit_pct, by_model, by_transform, ...), mirroring format_report() numbers exactly. - perf_records_as_dicts(report) -> per-record list for --raw output. - PERF_RECORD_FIELDS: shared column order for CSV/raw consumers. Pure additions; no behaviour change to existing callers. Part of #595. * feat(perf): add --format {text,json,csv} to headroom perf Adds a machine-readable output path to the perf command (issue #595): - --format json: aggregated summary (default) or, with --raw, a JSON array of per-record dicts. - --format csv: per-model breakdown (default) or, with --raw, one row per PERF record using the shared PERF_RECORD_FIELDS column order. - --format text (default): unchanged human-readable report. Enables CI guards (jq '.savings_pct < 70'), dashboards, and agent wrappers to consume perf data without scraping ANSI text. Closes #595. * test(perf): cover --format json/csv and structured builders Unit tests for build_perf_summary (totals, savings/cache pct, by_model/by_transform, empty-report zero-division guard) and perf_records_as_dicts, plus CliRunner integration tests for --format json, json --raw, csv, csv --raw, the unchanged text default, and rejection of an unknown format. Part of #595. * fix(perf): rename transform loop var to satisfy mypy The structured-summary builder reused `recs` for both the per-model (list[PerfRecord]) and per-transform (list[TransformRecord]) groupings, so mypy flagged the second assignment as an incompatible-type reuse (analyzer.py:704). Rename the transform loop variable to `t_recs` so each loop keeps a single element type. No behavior change. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Kumario1 <ramsakal.ipec@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
190 lines
6.4 KiB
Python
190 lines
6.4 KiB
Python
"""Tests for `headroom perf --format {text,json,csv}` (issue #595)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import io
|
|
import json
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from headroom.cli.main import main
|
|
from headroom.perf import analyzer
|
|
from headroom.perf.analyzer import (
|
|
PerfRecord,
|
|
PerfReport,
|
|
TransformRecord,
|
|
build_perf_summary,
|
|
perf_records_as_dicts,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def runner() -> CliRunner:
|
|
return CliRunner()
|
|
|
|
|
|
def _sample_report() -> PerfReport:
|
|
"""A small report with two models, cache numbers, and a transform."""
|
|
return PerfReport(
|
|
perf_records=[
|
|
PerfRecord(
|
|
timestamp="2026-06-05 10:00:00,000",
|
|
request_id="hr_1",
|
|
model="claude-sonnet-4.5",
|
|
num_messages=10,
|
|
tokens_before=1000,
|
|
tokens_after=400,
|
|
tokens_saved=600,
|
|
cache_read=800,
|
|
cache_write=200,
|
|
cache_hit_pct=80,
|
|
optimization_ms=12.0,
|
|
transforms=["content_router"],
|
|
),
|
|
PerfRecord(
|
|
timestamp="2026-06-05 11:00:00,000",
|
|
request_id="hr_2",
|
|
model="claude-opus-4-8",
|
|
num_messages=4,
|
|
tokens_before=1000,
|
|
tokens_after=600,
|
|
tokens_saved=400,
|
|
cache_read=200,
|
|
cache_write=0,
|
|
cache_hit_pct=100,
|
|
optimization_ms=8.0,
|
|
transforms=["content_router"],
|
|
),
|
|
],
|
|
transform_records=[
|
|
TransformRecord(
|
|
timestamp="2026-06-05 10:00:00,000",
|
|
name="content_router",
|
|
tokens_before=2000,
|
|
tokens_after=1000,
|
|
tokens_saved=1000,
|
|
),
|
|
],
|
|
log_files_read=1,
|
|
total_lines_parsed=42,
|
|
requested_hours=24.0,
|
|
oldest_kept_ts="2026-06-05 10:00:00,000",
|
|
newest_kept_ts="2026-06-05 11:00:00,000",
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Pure builders
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_build_perf_summary_totals_and_pct():
|
|
summary = build_perf_summary(_sample_report())
|
|
|
|
assert summary["total_requests"] == 2
|
|
assert summary["total_tokens_before"] == 2000
|
|
assert summary["total_tokens_after"] == 1000
|
|
assert summary["tokens_saved"] == 1000
|
|
# 1000 / 2000 == 50.0%
|
|
assert summary["savings_pct"] == 50.0
|
|
# cache: read 1000, write 200 -> 1000 / 1200 == 83.3%
|
|
assert summary["cache_read_tokens"] == 1000
|
|
assert summary["cache_write_tokens"] == 200
|
|
assert summary["cache_hit_pct"] == 83.3
|
|
assert summary["window_hours"] == 24.0
|
|
|
|
|
|
def test_build_perf_summary_by_model_and_transform():
|
|
summary = build_perf_summary(_sample_report())
|
|
|
|
models = {m["model"]: m for m in summary["by_model"]}
|
|
assert set(models) == {"claude-sonnet-4.5", "claude-opus-4-8"}
|
|
assert models["claude-sonnet-4.5"]["tokens_saved"] == 600
|
|
assert models["claude-sonnet-4.5"]["savings_pct"] == 60.0
|
|
assert models["claude-opus-4-8"]["savings_pct"] == 40.0
|
|
|
|
assert summary["by_transform"][0]["transform"] == "content_router"
|
|
assert summary["by_transform"][0]["tokens_saved"] == 1000
|
|
assert summary["by_transform"][0]["uses"] == 1
|
|
|
|
|
|
def test_build_perf_summary_empty_report_no_zero_division():
|
|
summary = build_perf_summary(PerfReport(requested_hours=168.0))
|
|
assert summary["total_requests"] == 0
|
|
assert summary["savings_pct"] == 0.0
|
|
assert summary["cache_hit_pct"] == 0.0
|
|
assert summary["by_model"] == []
|
|
|
|
|
|
def test_perf_records_as_dicts_roundtrips_fields():
|
|
dicts = perf_records_as_dicts(_sample_report())
|
|
assert len(dicts) == 2
|
|
assert dicts[0]["request_id"] == "hr_1"
|
|
assert dicts[0]["tokens_saved"] == 600
|
|
# transforms stays a list for JSON consumers
|
|
assert dicts[0]["transforms"] == ["content_router"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# CLI integration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _patch_report(monkeypatch, report: PerfReport) -> None:
|
|
monkeypatch.setattr(analyzer, "parse_log_files", lambda last_n_hours=168.0: report)
|
|
|
|
|
|
def test_perf_json_format(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf", "--format", "json"])
|
|
assert result.exit_code == 0, result.output
|
|
data = json.loads(result.output)
|
|
assert data["savings_pct"] == 50.0
|
|
assert "by_model" in data
|
|
assert data["total_requests"] == 2
|
|
|
|
|
|
def test_perf_json_raw_is_array(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf", "--format", "json", "--raw"])
|
|
assert result.exit_code == 0, result.output
|
|
data = json.loads(result.output)
|
|
assert isinstance(data, list)
|
|
assert len(data) == 2
|
|
assert data[0]["request_id"] == "hr_1"
|
|
|
|
|
|
def test_perf_csv_by_model(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf", "--format", "csv"])
|
|
assert result.exit_code == 0, result.output
|
|
rows = list(csv.DictReader(io.StringIO(result.output)))
|
|
assert {r["model"] for r in rows} == {"claude-sonnet-4.5", "claude-opus-4-8"}
|
|
sonnet = next(r for r in rows if r["model"] == "claude-sonnet-4.5")
|
|
assert sonnet["tokens_saved"] == "600"
|
|
|
|
|
|
def test_perf_csv_raw_per_record(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf", "--format", "csv", "--raw"])
|
|
assert result.exit_code == 0, result.output
|
|
rows = list(csv.DictReader(io.StringIO(result.output)))
|
|
assert len(rows) == 2
|
|
assert rows[0]["request_id"] == "hr_1"
|
|
# transforms flattened to a string cell
|
|
assert rows[0]["transforms"] == "content_router"
|
|
|
|
|
|
def test_perf_text_default_unchanged(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "Headroom Performance Report" in result.output
|
|
|
|
|
|
def test_perf_rejects_unknown_format(runner, monkeypatch):
|
|
_patch_report(monkeypatch, _sample_report())
|
|
result = runner.invoke(main, ["perf", "--format", "xml"])
|
|
assert result.exit_code != 0
|