"""Controlled API delay benchmark; never connects to a real service.""" import ast import json import statistics import sys import tempfile import threading import time from pathlib import Path from types import SimpleNamespace from unittest import mock project=Path(sys.argv[1]);output=Path(sys.argv[2]) sys.path.insert(0,str(project));sys.dont_write_bytecode=True import runtime_paths with tempfile.TemporaryDirectory(prefix='local-first-benchmark-') as temp, mock.patch.object(runtime_paths,'application_data_dir',return_value=Path(temp)): import wxwork_message_browser as browser import backend_client def method(path): module=ast.parse(path.read_text(encoding='utf-8')) cls=next(n for n in module.body if isinstance(n,ast.ClassDef) and n.name=='MainWindow') node=next(n for n in cls.body if isinstance(n,ast.FunctionDef) and n.name=='_refresh_wechat_database') node.decorator_list=[] namespace={'threading':threading} exec(compile(ast.Module(body=[node],type_ignores=[]),str(path),'exec'),namespace) return namespace[node.name] result={'project':str(project),'simulated_patient_api_ms':300,'iterations':3} for label,path in [('before',project/'backups/loading-performance-20260917/wechat_gui_qt.py'),('after',project/'wechat_gui_qt.py')]: refresh=method(path);samples=[];api_calls=[] for iteration in range(3): event=threading.Event();first=[] host=SimpleNamespace(_wechat_db_state={},_wechat_db_selected_account='',_wechat_db_selected_conversation='', _wechat_db_query='',_wechat_db_load_serial=0,_schedule_console_push=mock.Mock()) def emit(snapshot): first.append(time.perf_counter());event.set() host.wechatDatabaseLoaded=SimpleNamespace(emit=emit) def patient(*a,**kw): time.sleep(.3);return {'available':True} snapshot={'loading':False,'selectedAccount':'synthetic-account','selectedConversation':'synthetic-chat','messages':[{'content':'synthetic'}]} with mock.patch.object(browser,'load_browser_snapshot',side_effect=lambda **kw:dict(snapshot)), mock.patch.object(backend_client,'desktop_patient_context',side_effect=patient) as api: started=time.perf_counter();refresh(host) if not event.wait(3):raise RuntimeError('local snapshot timeout') samples.append(round((first[0]-started)*1000,3));api_calls.append(api.call_count) result[label]={'first_local_snapshot_ms':samples,'median_ms':statistics.median(samples),'api_calls_before_snapshot':api_calls} assert result['after']['median_ms'] < result['before']['median_ms'] / 3 output.write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8') print(json.dumps(result,ensure_ascii=False))