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