|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Format the recursion-guest per-function profile as a Markdown PR comment. |
| 3 | +
|
| 4 | +`test_recursion_profile_1query`/`_multiquery` print a global top-25 functions |
| 5 | +table (folded over all verifier steps, % of total run cycles), followed by |
| 6 | +one top-25 table per verifier step (% of that step's own cycles, so the |
| 7 | +table shows what dominates *within* the step) — e.g. how much of |
| 8 | +`step4:openings` is `keccak`. We parse all of those tables and render them |
| 9 | +as Markdown. |
| 10 | +
|
| 11 | + Top 25 functions by cycle count (aggregated over their PCs, all steps; % of total cycles): |
| 12 | + rank cycles % cum % PCs function |
| 13 | + 1 5335072 24.95% 24.95% 72 <...>::visit_seq::<...> |
| 14 | +
|
| 15 | + Top 25 functions by cycle count — step airs_bus_balance (% of this step's 5129138364 cycles): |
| 16 | + rank cycles % cum % PCs function |
| 17 | + 1 5335072 24.95% 24.95% 72 <...>::visit_seq::<...> |
| 18 | +
|
| 19 | +Reads the test's captured output from argv[1]; writes the Markdown body to |
| 20 | +argv[2] (or stdout). |
| 21 | +""" |
| 22 | + |
| 23 | +import re |
| 24 | +import sys |
| 25 | +from collections import OrderedDict |
| 26 | + |
| 27 | +# A per-function summary row: rank, cycles, pct%, cum%, pcs, function. |
| 28 | +FN_ROW = re.compile( |
| 29 | + r"^\s*\d+\s+(\d+)\s+([\d.]+)%\s+([\d.]+)%\s+(\d+)\s+(.*\S)\s*$" |
| 30 | +) |
| 31 | +HEADER_ROW = re.compile(r"^\s*rank\s+cycles") |
| 32 | +GLOBAL_TABLE_START = re.compile( |
| 33 | + r"Top \d+ functions by cycle count \(aggregated over their PCs, all steps" |
| 34 | +) |
| 35 | +STEP_TABLE_START = re.compile( |
| 36 | + r"Top \d+ functions by cycle count — step (\S+) \(% of this step's (\d+) cycles\):" |
| 37 | +) |
| 38 | +TOTAL_CYCLES = re.compile(r"Total cycles\s*:\s*(\d+)") |
| 39 | +UNIQUE_PCS = re.compile(r"Unique PCs\s*:\s*(\d+)") |
| 40 | +EXEC_TIME = re.compile(r"Exec time\s*:\s*(\S+)") |
| 41 | + |
| 42 | +GLOBAL_KEY = "__global__" |
| 43 | + |
| 44 | + |
| 45 | +def parse(text): |
| 46 | + total_cycles = unique_pcs = exec_time = None |
| 47 | + # GLOBAL_KEY -> {"denom": int|None, "rows": [...]}, then one entry per |
| 48 | + # step tag in first-seen order. |
| 49 | + tables = OrderedDict() |
| 50 | + current = None |
| 51 | + skip_header = False |
| 52 | + for line in text.splitlines(): |
| 53 | + if total_cycles is None and (m := TOTAL_CYCLES.search(line)): |
| 54 | + total_cycles = int(m.group(1)) |
| 55 | + if unique_pcs is None and (m := UNIQUE_PCS.search(line)): |
| 56 | + unique_pcs = int(m.group(1)) |
| 57 | + if exec_time is None and (m := EXEC_TIME.search(line)): |
| 58 | + exec_time = m.group(1) |
| 59 | + |
| 60 | + if GLOBAL_TABLE_START.search(line): |
| 61 | + current = GLOBAL_KEY |
| 62 | + tables[current] = {"denom": total_cycles, "rows": []} |
| 63 | + skip_header = True |
| 64 | + continue |
| 65 | + if m := STEP_TABLE_START.search(line): |
| 66 | + current = m.group(1) |
| 67 | + tables[current] = {"denom": int(m.group(2)), "rows": []} |
| 68 | + skip_header = True |
| 69 | + continue |
| 70 | + |
| 71 | + if current is None: |
| 72 | + continue |
| 73 | + if skip_header: |
| 74 | + # The header row right after a table-start line; anything else |
| 75 | + # (e.g. a stray blank line) just ends the table early, which is |
| 76 | + # fine — an empty table renders as "no rows". |
| 77 | + skip_header = False |
| 78 | + if HEADER_ROW.match(line): |
| 79 | + continue |
| 80 | + if m := FN_ROW.match(line): |
| 81 | + tables[current]["rows"].append( |
| 82 | + { |
| 83 | + "cycles": int(m.group(1)), |
| 84 | + "pct": m.group(2), |
| 85 | + "cum": m.group(3), |
| 86 | + "pcs": int(m.group(4)), |
| 87 | + "fn": m.group(5), |
| 88 | + } |
| 89 | + ) |
| 90 | + else: |
| 91 | + current = None |
| 92 | + |
| 93 | + return total_cycles, unique_pcs, exec_time, tables |
| 94 | + |
| 95 | + |
| 96 | +def short(name, width=90): |
| 97 | + return name if len(name) <= width else name[: width - 1] + "…" |
| 98 | + |
| 99 | + |
| 100 | +def render_table(rows, denom_label): |
| 101 | + if not rows: |
| 102 | + return "> _no rows_\n" |
| 103 | + body = "| Rank | Cycles | % | Cum % | PCs | Function |\n" |
| 104 | + body += "|-----:|-------:|--:|------:|----:|----------|\n" |
| 105 | + for i, r in enumerate(rows, 1): |
| 106 | + body += ( |
| 107 | + f"| {i} | {r['cycles']:,} | {r['pct']}% | {r['cum']}% | " |
| 108 | + f"{r['pcs']} | `{short(r['fn'])}` |\n" |
| 109 | + ) |
| 110 | + last_cum = rows[-1]["cum"] |
| 111 | + body += ( |
| 112 | + f"\n<sub>Each function's cycles are summed over all its program counters " |
| 113 | + f"in this table's scope; the top {len(rows)} cover {last_cum}% of " |
| 114 | + f"{denom_label}.</sub>\n" |
| 115 | + ) |
| 116 | + return body |
| 117 | + |
| 118 | + |
| 119 | +def render(total_cycles, unique_pcs, exec_time, tables, title="Recursion guest profile"): |
| 120 | + if not tables.get(GLOBAL_KEY, {}).get("rows"): |
| 121 | + return ( |
| 122 | + f"### {title}\n\n" |
| 123 | + "> ⚠️ No per-function rows found in the test output — the run may " |
| 124 | + "have failed before printing the table. Check the workflow logs.\n" |
| 125 | + ) |
| 126 | + |
| 127 | + body = f"### {title}\n\n" |
| 128 | + if total_cycles is not None: |
| 129 | + body += f"**Total cycles:** {total_cycles:,}" |
| 130 | + if unique_pcs is not None: |
| 131 | + body += f" · **Unique PCs:** {unique_pcs:,}" |
| 132 | + if exec_time: |
| 133 | + body += f" · **Exec time:** {exec_time}" |
| 134 | + body += "\n\n" |
| 135 | + |
| 136 | + global_rows = tables[GLOBAL_KEY]["rows"] |
| 137 | + body += f"#### Top {len(global_rows)} functions by cycles (all steps)\n\n" |
| 138 | + body += render_table(global_rows, "total cycles") |
| 139 | + |
| 140 | + for step, table in tables.items(): |
| 141 | + if step == GLOBAL_KEY: |
| 142 | + continue |
| 143 | + rows, denom = table["rows"], table["denom"] |
| 144 | + denom_note = f" of {denom:,} step cycles" if denom is not None else "" |
| 145 | + body += ( |
| 146 | + f"\n<details><summary>Step <code>{step}</code>{denom_note} — " |
| 147 | + f"top {len(rows)} functions</summary>\n\n" |
| 148 | + ) |
| 149 | + body += render_table(rows, "this step's cycles") |
| 150 | + body += "\n</details>\n" |
| 151 | + |
| 152 | + return body |
| 153 | + |
| 154 | + |
| 155 | +def main(): |
| 156 | + import argparse |
| 157 | + |
| 158 | + ap = argparse.ArgumentParser(description=__doc__) |
| 159 | + ap.add_argument("log", help="captured test output to parse") |
| 160 | + ap.add_argument("-o", "--out", help="write Markdown here instead of stdout") |
| 161 | + ap.add_argument( |
| 162 | + "-t", |
| 163 | + "--title", |
| 164 | + default="Recursion guest profile", |
| 165 | + help="section heading (e.g. the test/config name)", |
| 166 | + ) |
| 167 | + args = ap.parse_args() |
| 168 | + |
| 169 | + with open(args.log, "r", errors="replace") as f: |
| 170 | + text = f.read() |
| 171 | + body = render(*parse(text), title=args.title) |
| 172 | + if args.out: |
| 173 | + with open(args.out, "w") as f: |
| 174 | + f.write(body) |
| 175 | + else: |
| 176 | + sys.stdout.write(body) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + main() |
0 commit comments