Files
ai-agent/scripts/bench_lists.py

81 lines
2.5 KiB
Python

"""Conversation-list hot-path timings — run via scripts/bench-lists.sh.
Imports the backend (no startup hooks run, so no threads) against a copy of /data."""
import threading
import time
import main
from agents.runs import _build_agent_runs
from conversations.cards import _summaries_with_account
from conversations.catalog import _conversation_cards, _skills_by_path
from conversations.routes import (
conversation_summary as _conversation_summary,
conversations_list as _conversations_list,
)
from users.caller import OWNER
deps = main.app.state.ai
store, meta = deps.store, deps.meta_store
def raw(fn):
"""The handler's own dict, past `_json_in_worker`'s JSONResponse (if any)."""
return getattr(fn, "__wrapped__", fn)
conversations_list = raw(_conversations_list)
conversation_summary = raw(_conversation_summary)
def bump():
"""What one transcript write does to the caches."""
store.summaries_version += 1
def t(label, fn, n=3):
best = 1e9
for _ in range(n):
bump()
t0 = time.perf_counter()
fn()
best = min(best, time.perf_counter() - t0)
print(f"{label:<44} {best*1000:8.0f} ms")
return best
store.all_summaries() # warm the decode cache (a one-off at boot)
print("summaries:", len(store.all_summaries()), " meta entries:", len(meta.all()))
first = conversations_list(deps, OWNER, limit=50)["conversations"][0]["id"]
t("_skills_by_path", lambda: _skills_by_path(deps))
t("_build_agent_runs", lambda: _build_agent_runs(deps))
t("_summaries_with_account", lambda: _summaries_with_account(deps))
t("_conversation_cards(full=False)", lambda: _conversation_cards(deps, False))
t("GET /api/conversations?limit=50", lambda: conversations_list(deps, OWNER, limit=50))
t("GET /api/conversation-summary (1 id)", lambda: conversation_summary(deps, OWNER, first))
def herd(k):
"""k clients refetching the list right after one transcript write."""
bump()
ths = [threading.Thread(target=conversations_list, args=(deps, OWNER),
kwargs={"limit": 50})
for _ in range(k)]
t0 = time.perf_counter()
for th in ths:
th.start()
for th in ths:
th.join()
print(f"{'herd x' + str(k) + ' (wall, after one write)':<44} "
f"{(time.perf_counter() - t0)*1000:8.0f} ms")
herd(1)
herd(4)
herd(8)
# Steady state: nothing changed, repeat request.
t0 = time.perf_counter()
conversations_list(deps, OWNER, limit=50)
print(f"{'list, unchanged versions (warm)':<44} {(time.perf_counter()-t0)*1000:8.0f} ms")