from pathlib import Path import urllib.request,urllib.error,json,time,concurrent.futures,base64,io R=Path('/opt/neiro/reports/pets-knp-artifacts');R.mkdir(exist_ok=True);URL='http://10.9.8.230:8082' def get(path):return json.load(urllib.request.urlopen(URL+path,timeout=30)) def post(data): req=urllib.request.Request(URL+'/api/recognize',json.dumps(data).encode(),{'Content-Type':'application/json'}) try: with urllib.request.urlopen(req,timeout=40) as f:return f.status,json.load(f) except urllib.error.HTTPError as e:return e.code,json.load(e) info=get('/api/info');samples=get('/api/samples');assert len(samples)==24;results=[] for s in samples[:4]: start=time.monotonic();status,res=post({'image':s['image']});assert status==200;assert len(res['heat'])==196 and len(res['outputs'])==2 and res['model']==info['model_sha256'];results.append({'sample_id':s['id'],'truth':s['label'],'result':res,'external_seconds':time.monotonic()-start}) assert post({'image':'bad'})[0]==400 assert post({'image':'data:image/png;base64,AAAA'})[0]==400 with concurrent.futures.ThreadPoolExecutor(2) as pool: rows=list(pool.map(lambda _:post({'image':samples[0]['image']}),range(2))) assert sorted(r[0] for r in rows)==[200,429] res={'info':info,'cases':results,'malformed_400':True,'concurrent_200_429':True} (R/'api-checks.json').write_text(json.dumps(res,indent=2));print(json.dumps({'latency_ms':[r['result']['milliseconds'] for r in results],'api_checked':True}))