Cotton code
63 lines · 2.5 KB · PythonRaw
#!/usr/bin/env python3"""Validate simulator samples and print the cut line; record.py reads it and keeps the dated gate."""import jsonimport mathfrom pathlib import Pathimport statisticsimport sysfrom record import COLD_START_BUDGET_MS, RECORD_PAGE_BUDGET_MSKINDS = ('todo', 'note', 'person')def samples(values, count):    if not isinstance(values, list) or len(values) != count:        raise ValueError(f'expected {count} timing samples')    if any(type(v) not in (int, float) or not math.isfinite(v) or v <= 0 for v in values):        raise ValueError('timing samples must be finite positive milliseconds')    return valuesdef figures(raw):    try:        cold = samples(raw['cold_start_ms'], 5)        pages = [statistics.median(samples(raw['record_page_ms'][kind], 10)) for kind in KINDS]    except (KeyError, TypeError) as error:        raise ValueError(f'missing timing figure: {error}') from error    # Report the slowest kind's median, so a fast todo cannot hide a slow person.    return max(pages), statistics.median(cold)def first_open(raw, how):    """The slowest kind's median first open in a process, `warmed` (as the    app runs, with the record page's warm-up) or `unwarmed`."""    try:        return max(statistics.median(samples(raw['first_open_ms'][how][kind], 5)) for kind in KINDS)    except (KeyError, TypeError) as error:        raise ValueError(f'missing first-open figure: {error}') from errordef runtime(raw):    # A figure says which simulator runtime it came from: a runtime without a    # dyld cache once doubled the cold start (scripts/speedgate/runtime.py).    name = raw.get('runtime_name')    if not isinstance(name, str) or not name.strip():        raise ValueError('missing simulator runtime name')    return namedef main():    raw = json.loads(Path(sys.argv[1]).read_text())    if raw.get('scope') != 'synthetic-ui-stub-simulator':        raise ValueError('cut figures require the simulator; raster guard samples are not cut evidence')    page, cold = figures(raw)    warmed, unwarmed = first_open(raw, 'warmed'), first_open(raw, 'unwarmed')    print(f'[speed] record page {page:.1f} ms (budget {RECORD_PAGE_BUDGET_MS}; first open {warmed:.1f} ms with the warm-up, {unwarmed:.1f} ms without) · cold start {cold:.1f} ms (budget {COLD_START_BUDGET_MS}) · {runtime(raw)} simulator', flush=True)if __name__ == '__main__':    try:        main()    except (ValueError, OSError) as error:        sys.exit(f'[speed] FAILED: {error}')