from pathlib import Path
import hashlib,json,math,os,re,subprocess
root=Path('/Users/amlug/projects/perry/json-merged-pr10022');os.chdir(root)
b=Path('benchmarks/json_performance');work=b/'.work/main-eee'
clean={k:v for k,v in os.environ.items() if not k.startswith('PERRY_')}
node=['/opt/homebrew/bin/node']
records=b/'.work/fixtures/records_array_16k.json'
scan=[str(records),'scan','100','2','verify']
rot=[str(b/'.work/rotating/unicode_1m'),'parse','1000','8','time','rotating']
def run(name,cmd,env=None):
    with (work/(name+'.stdout')).open('wb') as out,(work/(name+'.stderr')).open('wb') as err:
        subprocess.run(list(map(str,cmd)),env=clean|{'PERRY_GC_DIAG':'1'}|(env or {}),stdout=out,stderr=err,check=True,timeout=90)
    output = (work/(name+'.stdout')).read_bytes()
    result = re.search(rb'^RESULT (.+)$', output, re.M)
    if result:
        fields = list(map(float, result.group(1).split()))
        assert len(fields) == 7 and all(math.isfinite(n) for n in fields), name
    print('PASS',name,flush=True)
    return (work/(name+'.stdout')).read_bytes(),(work/(name+'.stderr')).read_text()
def verification(text):
    return [line for line in text.splitlines() if line.startswith((b'VERIFY ',b'LAST ',b'KEEP '))]
def counters(text):
    def last(key):
        values=re.findall(r'\b'+key+r'=(\d+)',text)
        return int(values[-1]) if values else 0
    result = {key:last(key) for key in ['scheduled_collections','copying_minors','moved_objects','loop_polls','cycle_starts']}|{'protected_retired_sets':len(re.findall(r'\[gc-fromspace-protect\].*retired_set=#',text))}
    if not re.search(r'\bmoved_objects=',text):
        result['moved_objects']=sum(sum(map(int,re.findall(r'\b(?:copied_objects|promoted_objects)=(\d+)',line))) for line in text.splitlines() if line.startswith('[gc-copy-minor] ran'))
    return result
stress={'PERRY_GC_SCHEDULE_SEED':'10022','PERRY_GC_SCHEDULE_RATE':'0.1','PERRY_GC_SCHEDULE_ALLOC_KB':'0','PERRY_GC_PROTECT_FROMSPACE':'1'}
expected,_=run('scan-node',node+[b/'worker.js']+scan)
output,diagnostics=run('scan-scheduled',[work/'worker']+scan,stress)
assert verification(output)==verification(expected)
c=counters(diagnostics)
assert all(c[k]>0 for k in ['scheduled_collections','copying_minors','moved_objects','protected_retired_sets'])
full,_=run('scan-fullgc',[work/'worker']+scan,{'PERRY_GEN_GC':'0'})
assert verification(full)==verification(expected)
witness={'scan':c|{'matches_node':True,'full_gc_matches_node':True}}
expected=(b/'.work/large-leaf-lifetime/node.stdout').read_bytes()
for label,env in [('normal',{}),('scheduled',stress),('fullgc',{'PERRY_GEN_GC':'0'})]:
    output,diagnostics=run('leaf-'+label,[work/'leaf-lifetime'],env)
    assert output==expected,(label,output,expected)
    c=counters(diagnostics)
    if label in {'normal','scheduled'}: assert c['copying_minors']>0,c
    if label=='scheduled': assert c['protected_retired_sets']>0 and c['moved_objects']>0,c
    witness['retained_'+label]=c|{'matches_node':True}
output,diagnostics=run('rotating-unicode',[work/'rotating-worker']+rot)
c=counters(diagnostics)
assert c['copying_minors']>0,c
result=list(map(float,re.search(rb'^RESULT (.+)$',output,re.M).group(1).split()))
witness['rotating_unicode_diagnostic']=c|{'rss_before_bytes':result[3],'rss_after_bytes':result[4],'timed_parses':1000,'warmup_parses':8}
rot_verify=[str(b/'.work/rotating/unicode_1m'),'parse','7','8','verify','rotating']
expected,_=run('rotating-node',node+[b/'rotating-worker.js']+rot_verify)
output,_=run('rotating-verify',[work/'rotating-worker']+rot_verify)
assert verification(output)==verification(expected)
witness['rotating_unicode_diagnostic']['matches_node']=True

# Compare the actual recurring malloc trigger counts, separately from timing.
schedule_args=[str(b/'.work/fixtures/unicode_1m.json'),'stringify','150','2']
schedule_counts={}
for label,worker in [('baseline',b/'.work/decoder-r2/worker'),('candidate',work/'worker')]:
    _,diagnostics=run('stringify-cadence-'+label,[worker]+schedule_args)
    schedule_counts[label]=[int(n) for n in re.findall(r'\[gc-trigger\].*kind=MallocCount.*?\bmalloc=(\d+)',diagnostics)]
assert len(schedule_counts['candidate'])>=len(schedule_counts['baseline'])>=3,schedule_counts
assert max(schedule_counts['candidate'][1:])<=max(schedule_counts['baseline'][1:]),schedule_counts
witness['stringify_cadence_malloc_counts']=schedule_counts
(work/'gc-witness.json').write_text(json.dumps(witness,indent=2)+'\n')
print(json.dumps(witness,indent=2),flush=True)

