|
| 1 | +"""Break down where search time goes with vs without window.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 10 | +if REPO_ROOT not in sys.path: |
| 11 | + sys.path.insert(0, REPO_ROOT) |
| 12 | + |
| 13 | +from services.search import ( |
| 14 | + _composer_dict_timestamp_ms, |
| 15 | + _composer_row_raw_text, |
| 16 | + _index_bubble_texts_matching_query, |
| 17 | + _timestamp_in_search_window, |
| 18 | + resolve_search_since_ms, |
| 19 | +) |
| 20 | +from services.workspace_db import ( |
| 21 | + COMPOSER_ROWS_WITH_HEADERS_SQL, |
| 22 | + build_composer_id_to_workspace_id_cached, |
| 23 | + collect_workspace_entries, |
| 24 | + open_global_db, |
| 25 | +) |
| 26 | +from utils.workspace_path import resolve_workspace_path |
| 27 | + |
| 28 | + |
| 29 | +def profile_window(since_ms: int | None, label: str) -> None: |
| 30 | + query = "export" |
| 31 | + q = query.lower() |
| 32 | + wp = resolve_workspace_path() |
| 33 | + entries = collect_workspace_entries(wp) |
| 34 | + |
| 35 | + t0 = time.perf_counter() |
| 36 | + build_composer_id_to_workspace_id_cached(wp, entries, []) |
| 37 | + print(f" composer_map: {time.perf_counter() - t0:.2f}s") |
| 38 | + |
| 39 | + with open_global_db(wp) as (conn, _): |
| 40 | + if conn is None: |
| 41 | + return |
| 42 | + t0 = time.perf_counter() |
| 43 | + composer_rows = conn.execute(COMPOSER_ROWS_WITH_HEADERS_SQL).fetchall() |
| 44 | + print(f" load_composer_rows ({len(composer_rows)}): {time.perf_counter() - t0:.2f}s") |
| 45 | + |
| 46 | + window_ids = None |
| 47 | + if since_ms is not None: |
| 48 | + t0 = time.perf_counter() |
| 49 | + window_ids = set() |
| 50 | + in_window_rows = [] |
| 51 | + for row in composer_rows: |
| 52 | + composer_id = row["key"].split(":")[1] |
| 53 | + raw_text = _composer_row_raw_text(row) |
| 54 | + try: |
| 55 | + cd_probe = json.loads(raw_text) |
| 56 | + except Exception: |
| 57 | + continue |
| 58 | + if not isinstance(cd_probe, dict): |
| 59 | + continue |
| 60 | + if not _timestamp_in_search_window( |
| 61 | + _composer_dict_timestamp_ms(cd_probe), since_ms |
| 62 | + ): |
| 63 | + continue |
| 64 | + window_ids.add(composer_id) |
| 65 | + in_window_rows.append(row) |
| 66 | + composer_rows = in_window_rows |
| 67 | + print( |
| 68 | + f" window_filter ({len(window_ids)} composers): " |
| 69 | + f"{time.perf_counter() - t0:.2f}s" |
| 70 | + ) |
| 71 | + |
| 72 | + t0 = time.perf_counter() |
| 73 | + idx = _index_bubble_texts_matching_query(conn, q, composer_ids=window_ids) |
| 74 | + mode = ( |
| 75 | + "full_scan_python_filter" |
| 76 | + if window_ids is not None and len(window_ids) <= 500 |
| 77 | + else "full_scan" |
| 78 | + ) |
| 79 | + print( |
| 80 | + f" bubble_index ({len(idx)} hits, {mode}): " |
| 81 | + f"{time.perf_counter() - t0:.2f}s" |
| 82 | + ) |
| 83 | + |
| 84 | + t0 = time.perf_counter() |
| 85 | + hits = 0 |
| 86 | + for row in composer_rows: |
| 87 | + raw = _composer_row_raw_text(row) |
| 88 | + cid = row["key"].split(":")[1] |
| 89 | + if q in raw.lower() or cid in idx: |
| 90 | + hits += 1 |
| 91 | + print(f" composer_match_loop ({hits} candidates): {time.perf_counter() - t0:.2f}s") |
| 92 | + |
| 93 | + |
| 94 | +def main() -> None: |
| 95 | + since = resolve_search_since_ms(all_history=False) |
| 96 | + print("=== 30-day window ===") |
| 97 | + profile_window(since, "30d") |
| 98 | + print("=== all history ===") |
| 99 | + profile_window(None, "all") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments