Cotton code
91 lines · 3.8 KB · PythonRaw
#!/usr/bin/env python3"""A database switch's timeline, read off the fan's log lines (lane stalepic).    switch-times.py <fan.log> [--last N]<fan.log> is what `HOME_WALK_LOG` kept of a walk (`ui-tests-stub.sh--home-walk … --walk switches`), or any `log stream` of subsystemcom.cottondb.fan. Each switch starts at `switch: <db> asked` — the caps' archeading for the cap, as the tap lands — and `SwitchClock` logs, once each, themilliseconds since then at which the window shows the database (shown: theswitch has read its Home and turns, the old Home standing until then), itsfan mounts (fan), the page area first draws a ghost / a page picture (ghost,picture), its tiles are answered (arranged) and its live page has drawnarranged (live).First frame is the first of picture and live: the database's own page in thepage area. A ghost is a placeholder, not the page, and is listed apart.Prints one row a switch and the median and worst of the last N (default 10).A cold launch is a timeline of its own (`switch: launch asked`, from thewindow's first appearance, 295 B): Home's first read asked (home-asked), theshown database's read come back (home-read), the fan's marks, and the framethe splash goes (splash-down). Each launch is a row of its own, with theread's own time (home-read less home-asked)."""import reimport statisticsimport sysMARKS = ["shown", "fan", "ghost", "picture", "arranged", "live"]LAUNCH_MARKS = ["fan", "home-asked", "home-read", "arranged", "live", "splash-down"]ASKED = re.compile(r"switch: (\S+) asked")MARK = re.compile(r"switch: (\S+) ([\w-]+) (\d+) ms")def switches(lines):    out = []    for line in lines:        if m := ASKED.search(line):            out.append({"database": m.group(1)})        elif (m := MARK.search(line)) and out and out[-1]["database"] == m.group(1):            out[-1].setdefault(m.group(2), int(m.group(3)))    return outdef first_frame(switch):    drawn = [switch[k] for k in ("picture", "live") if k in switch]    return min(drawn) if drawn else Nonedef read_time(launch):    if "home-read" in launch and "home-asked" in launch:        return launch["home-read"] - launch["home-asked"]    return Nonedef summary(name, values):    values = [v for v in values if v is not None]    if not values:        return f"{name}: none"    return f"{name}: median {statistics.median(values):.0f} ms, worst {max(values)} ms, best {min(values)} ms ({len(values)})"def main(argv):    if not argv or argv[0].startswith("-"):        sys.exit(__doc__)    last = int(argv[argv.index("--last") + 1]) if "--last" in argv else 10    with open(argv[0], errors="replace") as log:        found = switches(log)    launches = [s for s in found if s["database"] == "launch"]    found = [s for s in found if s["database"] != "launch"]    if launches:        print("launch".ljust(8) + "".join(m.rjust(13) for m in LAUNCH_MARKS) + "      read")        for s in launches:            print("launch".ljust(8) + "".join(str(s.get(m, "-")).rjust(13) for m in LAUNCH_MARKS)                  + str(read_time(s) if read_time(s) is not None else "-").rjust(10))        print(summary("Home's read at launch (home-read less home-asked)", [read_time(s) for s in launches]))        print(summary("window to splash down", [s.get("splash-down") for s in launches]))    if not found:        return    print("database".ljust(20) + "".join(m.rjust(9) for m in MARKS) + "   first")    for s in found:        print(s["database"].ljust(20) + "".join(str(s.get(m, "-")).rjust(9) for m in MARKS)              + str(first_frame(s) if first_frame(s) is not None else "-").rjust(8))    tail = found[-last:]    print(summary(f"tap to first frame (last {len(tail)})", [first_frame(s) for s in tail]))    print(summary(f"tap to live page (last {len(tail)})", [s.get("live") for s in tail]))if __name__ == "__main__":    main(sys.argv[1:])