WilsonPharma/Practitioners-Workload-DB
1
1"""Diagnostic with correct password — tests full API round-trip timing."""2import sys, time, requests, concurrent.futures3sys.path.insert(0, r"w:\01-03-2026\Dr. Heba\Practitioners Workload DB")4 5API_BASE = "http://127.0.0.1:8000"6PASSWORD = "admin123" # from run.bat7 8# ── Auth ───────────────────────────────────────────────────────────────────9print("=== Authenticating ===")10try:11 r = requests.post(f"{API_BASE}/login", data={"username": "admin", "password": PASSWORD}, timeout=10)12 token = r.json().get("access_token", "") if r.ok else ""13 print(f" Status: {r.status_code} token={'OK' if token else 'FAILED'}")14except Exception as e:15 token = ""16 print(f" Cannot reach backend: {e}")17 print(" >>> Make sure run.bat is running first! <<<")18 sys.exit(1)19 20if not token:21 print(" Wrong password — trying common variants...")22 for pw in ["Admin123", "admin", "password", "1234"]:23 r = requests.post(f"{API_BASE}/login", data={"username": "admin", "password": pw}, timeout=5)24 if r.ok and r.json().get("access_token"):25 token = r.json()["access_token"]26 print(f" Found password: {pw}")27 break28 29headers = {"Authorization": f"Bearer {token}"}30 31# ── Benchmark each individual call ────────────────────────────────────────32tests = [33 ("facility pivot (fast)", {"include_top_facs": "false", "include_kpi": "false", "include_breakdown": "false", "group_by": "facility_name"}),34 ("region pivot (fast)", {"include_top_facs": "false", "include_kpi": "false", "include_breakdown": "false", "group_by": "region"}),35 ("speciality breakdown (fast)", {"include_top_facs": "false", "include_kpi": "false", "include_breakdown": "true", "group_by": "speciality"}),36 ("pract pivot NO top_facs", {"include_top_facs": "false", "include_kpi": "false", "include_breakdown": "false", "group_by": "practitioner_name"}),37 ("pract pivot WITH top_facs", {"include_top_facs": "true", "include_kpi": "true", "include_breakdown": "true", "group_by": "practitioner_name"}),38]39 40print("\n=== Individual /summary calls (sequential) ===")41for label, params in tests:42 t0 = time.time()43 r = requests.get(f"{API_BASE}/summary", headers=headers, params=params, timeout=120)44 elapsed = time.time() - t045 d = r.json()46 print(f" [{elapsed:5.2f}s] {label:<35} pivot={len(d.get('pivot',[]))} kpi_keys={len(d.get('kpi',{}))} breakdown={len(d.get('breakdown',[]))}")47 48print("\n=== Second call — should hit server cache ===")49for label, params in tests:50 t0 = time.time()51 r = requests.get(f"{API_BASE}/summary", headers=headers, params=params, timeout=120)52 elapsed = time.time() - t053 print(f" [{elapsed:5.2f}s] {label} (cached)")54 55print("\n=== 5 parallel calls (dashboard simulation) ===")56call_set = [p for _, p in tests]57def _hit(p):58 t0 = time.time()59 r = requests.get(f"{API_BASE}/summary", headers=headers, params=p, timeout=120)60 return time.time()-t0, r.status_code61 62t_wall = time.time()63with concurrent.futures.ThreadPoolExecutor(max_workers=5) as ex:64 results = list(ex.map(_hit, call_set))65wall = time.time() - t_wall66 67for (elapsed, status), (label, _) in zip(results, tests):68 print(f" [{elapsed:5.2f}s] {label:<35} status={status}")69print(f"\n Wall-clock total (parallel): {wall:.2f}s")70 71print("\n=== KPI summary alone ===")72t0 = time.time()73r = requests.get(f"{API_BASE}/summary", headers=headers,74 params={"include_top_facs": "false", "include_kpi": "true",75 "include_breakdown": "false", "group_by": "practitioner_name"},76 timeout=120)77print(f" [{time.time()-t0:.2f}s] KPI only")78 79print("\n=== facility_breakdown_table alone (speciality group) ===")80t0 = time.time()81r = requests.get(f"{API_BASE}/summary", headers=headers,82 params={"include_top_facs": "false", "include_kpi": "false",83 "include_breakdown": "true", "group_by": "speciality"},84 timeout=120)85print(f" [{time.time()-t0:.2f}s] breakdown by speciality rows={len(r.json().get('breakdown',[]))}")86 87t0 = time.time()88r = requests.get(f"{API_BASE}/summary", headers=headers,89 params={"include_top_facs": "false", "include_kpi": "false",90 "include_breakdown": "true", "group_by": "practitioner_name"},91 timeout=120)92print(f" [{time.time()-t0:.2f}s] breakdown by practitioner_name rows={len(r.json().get('breakdown',[]))}")93 