Kaleido/scripts/profile_transitions.py
Elijah 784921dba9
All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m25s
Release Kaleido 0.1.9 with responsive navigation and clearer show details
2026-10-26 09:57:02 -07:00

99 lines
6 KiB
Python

"""Time individual D-pad focus transitions on Home and Movies with gfxinfo framestats.
Select the device with KALEIDO_DEVICE (default: the onn). Use an optimized release
with the baseline profile installed and speed-profile compilation, and never run
another device test alongside it. Results stay in ignored evidence/private/.
For each transition this reports the frames drawn in the following window, the
vsyncs skipped between them (visible stutter), and frame latency percentiles.
"""
import argparse,json,time
from device import adb,OUT,DEVICE
from verify_ui import launch,key,wait,focus,focused,snapshot
HERO=('Play','Resume')
LABEL='run'
def card(r):return focus(r) not in ('','Home','Movies','TV Shows','Discover','Library')+HERO
def changed(previous):return lambda r:card(r) and focus(r)!=previous
def frames(raw):
rows=[]
for block in raw.split('---PROFILEDATA---')[1::2]:
lines=[line.strip() for line in block.splitlines() if line.strip()]
if not lines:continue
names=lines[0].rstrip(',').split(',')
for line in lines[1:]:
values=line.rstrip(',').split(',')
if len(values)!=len(names):continue
row=dict(zip(names,map(int,values)))
if row['Flags']==0 and row['FrameCompleted']>row['IntendedVsync']:rows.append(row)
return sorted(rows,key=lambda r:r['IntendedVsync'])
def summarize(rows,period):
if not rows:return {'frames':0,'skipped':0}
latency=sorted((r['FrameCompleted']-r['IntendedVsync'])/1e6 for r in rows)
ui=sorted((r['SyncQueued']-r['IntendedVsync'])/1e6 for r in rows)
# A gap after a slow frame is a stall; a gap after a fast frame, or one longer than
# 300 ms, is idle time (on weak GPUs every frame's latency exceeds one interval).
skipped=sum(max(0,round((b['IntendedVsync']-a['IntendedVsync'])/period)-1) for a,b in zip(rows,rows[1:])
if a['FrameCompleted']-a['IntendedVsync']>period and b['IntendedVsync']-a['IntendedVsync']<3e8)
pick=lambda v,p:v[min(len(v)-1,int((len(v)-1)*p))]
return {'frames':len(rows),'skipped':skipped,'median':round(pick(latency,.5),1),'p95':round(pick(latency,.95),1),
'max':round(latency[-1],1),'uiMax':round(ui[-1],1)}
def measure(name,code,expect,window,results):
time.sleep(.8)
adb('shell','dumpsys','gfxinfo','tv.kaleido.app','reset')
adb('shell','input','keyevent',str(code));time.sleep(window)
raw=adb('shell','dumpsys','gfxinfo','tv.kaleido.app','framestats').decode(errors='replace')
rows=frames(raw)
with open(OUT/f'transitions-{LABEL}-frames.txt','a') as log:log.write(f'### {name}\n'+raw.split('---PROFILEDATA---')[1]+'\n')
# The display's frame interval is the shortest step between measured frames.
steps=[b['IntendedVsync']-a['IntendedVsync'] for a,b in zip(rows,rows[1:]) if b['IntendedVsync']>a['IntendedVsync']]
results.setdefault(name,[]).append(summarize(rows,min(steps) if steps else 1e9/60))
print(name,json.dumps(results[name][-1]),flush=True)
return wait(expect,name+' destination')
def resummarize(label):
"""Recompute a saved run's per-transition stalls from its raw framestats."""
text=(OUT/f'transitions-{label}-frames.txt').read_text();totals={}
for chunk in text.split('### ')[1:]:
name,raw=chunk.split('\n',1);rows=frames('---PROFILEDATA---\n'+raw+'---PROFILEDATA---')
steps=[b['IntendedVsync']-a['IntendedVsync'] for a,b in zip(rows,rows[1:]) if b['IntendedVsync']>a['IntendedVsync']]
totals.setdefault(name,[]).append(summarize(rows,min(steps) if steps else 1e9/60))
return totals
def main():
parser=argparse.ArgumentParser();parser.add_argument('label');parser.add_argument('--cycles',type=int,default=3)
parser.add_argument('--window',type=float,default=1.2);parser.add_argument('--skip-movies',action='store_true');parser.add_argument('--skip-home',action='store_true');args=parser.parse_args()
global LABEL;LABEL=args.label;(OUT/f'transitions-{LABEL}-frames.txt').unlink(missing_ok=True)
results={}
launch()
for _ in range(0 if args.skip_home else args.cycles):
measure('home: tab -> hero',20,lambda r:focus(r) in HERO,args.window,results)
root=measure('home: hero -> row',20,card,args.window,results)
first=focus(root)
root=measure('home: row -> row',20,changed(first),args.window,results)
measure('home: row <- row',19,lambda r:focus(r)==first,args.window,results)
measure('home: hero <- row',19,lambda r:focus(r) in HERO,args.window,results)
measure('home: tab <- hero',19,lambda r:focus(r)=='Home',args.window,results)
if not args.skip_movies:
for _ in range(args.cycles):
measure('tabs: home -> movies',22,lambda r:focus(r)=='Movies' and 'Recently released' in [n.get('text') for n in r.iter('node')],args.window*2,results)
measure('movies: tab -> discover',20,lambda r:focus(r)=='Discover',args.window,results)
root=measure('movies: discover -> row',20,card,args.window,results)
first=focus(root)
measure('movies: row -> row',20,changed(first),args.window,results)
measure('movies: row <- row',19,lambda r:focus(r)==first,args.window,results)
measure('movies: discover <- row',19,lambda r:focus(r)=='Discover',args.window,results)
measure('movies: tab <- discover',19,lambda r:focus(r)=='Movies',args.window,results)
measure('tabs: home <- movies',21,lambda r:focus(r)=='Home',args.window*2,results)
summary={}
for name,runs in results.items():
summary[name]={'skipped':[r['skipped'] for r in runs],'p95':max(r.get('p95',0) for r in runs),'max':max(r.get('max',0) for r in runs),'uiMax':max(r.get('uiMax',0) for r in runs)}
report={'device':DEVICE,'transitions':summary,'runs':results}
(OUT/f'transitions-{args.label}.json').write_text(json.dumps(report,indent=2)+'\n')
print('\nSUMMARY',args.label,DEVICE)
for name,s in summary.items():print(f" {name:28} skipped={s['skipped']} p95={s['p95']} max={s['max']} uiMax={s['uiMax']}")
if __name__=='__main__':main()