import time, json, urllib.request
from playwright.sync_api import sync_playwright
def port():
    for p in range(9222,9240):
        try: urllib.request.urlopen(f'http://127.0.0.1:{p}/json/version',timeout=0.3); return p
        except: pass
LOGPOOL="()=>{const u=new Uint16Array(HEAPU8.buffer);return window.__pool.map(p=>u[p[0]>>1]);}"
with sync_playwright() as pw:
    pg=pw.chromium.connect_over_cdp(f'http://127.0.0.1:{port()}').contexts[0].pages[0]
    pg.evaluate("window.__pool=(window.__memcands||[]).map(o=>[o,'u16'])")
    pool=pg.evaluate("window.__pool")
    f=open("memlog2.csv","w"); f.write(json.dumps(pool)+"\n")
    print(f"logging {len(pool)} candidates every 200ms for 180s")
    t0=time.time()
    while time.time()-t0<180:
        vals=pg.evaluate(LOGPOOL)
        f.write(f"{time.time()-t0:.1f},"+",".join(map(str,vals))+"\n"); f.flush()
        time.sleep(0.2)
    print("done")
