goon/tests/test_search_walk.py
goon-foss 45ee7a81e1
Some checks are pending
Backend tests / test (push) Waiting to run
fix(api): szybkie szukanie po tytule dla popularnych i krotkich fraz
Planer szacowal kazda fraze na 427 trafien (histogram 100 probek, dolny limit), wiec
zawsze szedl indeksem trigramow i sortowal wszystkie trafienia: "vo" 44 s (dwie
litery nie maja trigramu, indeks oddaje 4,3 mln wierszy), "mis" 6,5 s, "free" 1,9 s
(GOON-73/75).

- 0046: histogram title_normalized 1000 probek, szacunki bliskie prawdy.
- list_scenes: przy q i sorcie po dacie najpierw przejscie po indeksie sortu z
  limitem 300 ms (bez bitmap, w SAVEPOINT wycofywanym zawsze), gdy probka tytulow
  z pg_stats mowi, ze wystarczy przejsc do 30 tys. scen; inaczej zwykly plan.
  Frazy bez trigramu zawsze przejsciem (limit 3 s, potem pusto).

115 prawdziwych wyszukiwan z logow: wyniki identyczne, najgorszy 44 s -> 0,8 s,
srednie frazy dotykaja 3-7x mniej stron ("bad" 122 tys. -> 16 tys.).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 12:00:11 +02:00

52 lines
1.6 KiB
Python

from app.api.scenes import (
_SEARCH_WALK_BUDGET_MS,
_SEARCH_WALK_BUDGET_NO_TRGM_MS,
_search_walk_budget,
)
# 1000 tytulow jak histogram z pg_stats: 20 z "anal", 1 z "hailey rose", reszta obojetna.
SAMPLE = ["anal scene %d" % i for i in range(20)] + ["hailey rose pov"] + [
"other title %d" % i for i in range(979)
]
def _budget(q, off=0, want=51, narrowed=False):
return _search_walk_budget(q, off, want, narrowed, lambda: SAMPLE)
def test_common_phrase_walks():
# 20/1000 = 2%: na strone 1 trzeba przejsc ~2,5 tys. scen
assert _budget("anal") == _SEARCH_WALK_BUDGET_MS
def test_phrase_absent_from_sample_goes_to_trigrams():
assert _budget("jackerman") is None
def test_single_hit_phrase_goes_to_trigrams():
# 1/1000 = 0,1%: ~51 tys. scen do przejscia, powyzej progu
assert _budget("hailey rose") is None
def test_deep_page_of_common_phrase_goes_to_trigrams():
# strona 13 "anal": (600 + 51) / 2% = ~32 tys.
assert _budget("anal", off=600) is None
def test_narrowed_search_goes_to_trigrams():
assert _budget("anal", narrowed=True) is None
def test_short_phrase_always_walks():
# bez trigramu zwykly plan czyta caly indeks; tez przy zawezeniu i poza probka
for q in ("vo", "q", "xz", " a "):
assert _budget(q, narrowed=True) == _SEARCH_WALK_BUDGET_NO_TRGM_MS
assert _budget("zq", off=5000) == _SEARCH_WALK_BUDGET_NO_TRGM_MS
def test_sample_not_read_when_not_needed():
def boom():
raise AssertionError("probka niepotrzebna")
assert _search_walk_budget("vo", 0, 51, False, boom) == _SEARCH_WALK_BUDGET_NO_TRGM_MS
assert _search_walk_budget("anal", 0, 51, True, boom) is None