Kaleido/scripts/profile_transitions.py
Elijah 30e0f30bca
All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m24s
Release Kaleido 0.6.1 with smoother browsing
Home, Movies and TV stay composed once shown and are revealed instead of
rebuilt; hidden screens are unplaced, have no semantics and take no focus.
The hero stays composed as a zero-alpha layer. Lazy-list prefetch runs on
FramePrefetch, which uses only spare frame time (unlimited only when truly
idle) instead of Compose's scheduler, which gave late frames unlimited time
and composed whole rows mid-scroll. CacheWindows keeps Compose's cache-window
strategy on that scheduler and premeasures nested cards.

With any accessibility service enabled, Compose walked and sorted every
composed node every 100 ms while browsing; rows and cards outside the
viewport now carry no semantics. Cards use a lighter artwork path, lay out
both title weights up front, and models are @Immutable. Preview and hero copy
compose over the transparent start of their fade. Library sync applies its
snapshot after 2 s without input, and moveToRow retries focus so a refreshed
card is never left unfocused.

profile_transitions.py now counts frames actually dropped on the display.
On the onn the 42 measured transitions dropped 24-38 frames, versus 55 for
0.6.0; details are in docs/runtime-verification.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:24:07 -07:00

109 lines
6.9 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)
# What the viewer sees: refreshes that repeated the previous frame while frames kept coming.
# Buffer stuffing hides many slow UI frames, so this is stricter than latency and catches
# stalls the heuristic above misses. Invalid present times (0 or before the vsync) are skipped.
# A gap longer than ~6 frames counts only when the frame before it ran long enough to cause it;
# otherwise nothing was animating (e.g. a later image arriving after an idle pause).
shown=[r for r in rows if r.get('DisplayPresentTime',0)>r['IntendedVsync']]
def stalled(a,b):
gap=b['DisplayPresentTime']-a['DisplayPresentTime']
return 0<gap<3e8 and (gap<=6.5*period or a['FrameCompleted']-a['IntendedVsync']>=gap-period)
dropped=sum(max(0,round((b['DisplayPresentTime']-a['DisplayPresentTime'])/period)-1) for a,b in zip(shown,shown[1:]) if stalled(a,b))
pick=lambda v,p:v[min(len(v)-1,int((len(v)-1)*p))]
return {'frames':len(rows),'skipped':skipped,'dropped':dropped,'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]={'dropped':[r.get('dropped',0) for r in runs],'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} dropped={s['dropped']} skipped={s['skipped']} p95={s['p95']} max={s['max']} uiMax={s['uiMax']}")
if __name__=='__main__':main()