|
| 1 | +import argparse |
| 2 | +import time |
| 3 | +from statistics import mean |
| 4 | + |
| 5 | +import iris |
| 6 | + |
| 7 | + |
| 8 | +def spawn_push_actors(rt, count: int, budget: int): |
| 9 | + pids = [] |
| 10 | + t0 = time.perf_counter() |
| 11 | + |
| 12 | + def no_op_handler(_msg): |
| 13 | + return None |
| 14 | + |
| 15 | + for _ in range(count): |
| 16 | + pids.append(rt.spawn(no_op_handler, budget=budget, release_gil=False)) |
| 17 | + |
| 18 | + elapsed = time.perf_counter() - t0 |
| 19 | + return pids, elapsed |
| 20 | + |
| 21 | + |
| 22 | +def send_one_per_actor(rt, pids, payload: bytes): |
| 23 | + t0 = time.perf_counter() |
| 24 | + for pid in pids: |
| 25 | + ok = rt.send(pid, payload) |
| 26 | + if not ok: |
| 27 | + raise RuntimeError(f"send failed for pid={pid}") |
| 28 | + elapsed = time.perf_counter() - t0 |
| 29 | + return elapsed |
| 30 | + |
| 31 | + |
| 32 | +def spawn_pull_actors(rt, count: int, budget: int): |
| 33 | + pids = [] |
| 34 | + t0 = time.perf_counter() |
| 35 | + |
| 36 | + def mailbox_handler(mailbox): |
| 37 | + while True: |
| 38 | + msg = mailbox.recv() |
| 39 | + if msg is None: |
| 40 | + break |
| 41 | + |
| 42 | + for _ in range(count): |
| 43 | + pids.append(rt.spawn_with_mailbox(mailbox_handler, budget=budget)) |
| 44 | + |
| 45 | + elapsed = time.perf_counter() - t0 |
| 46 | + return pids, elapsed |
| 47 | + |
| 48 | + |
| 49 | +def cleanup(rt, pids): |
| 50 | + for pid in pids: |
| 51 | + rt.stop(pid) |
| 52 | + |
| 53 | + |
| 54 | +def fmt_rate(total: int, secs: float) -> str: |
| 55 | + if secs <= 0: |
| 56 | + return "inf" |
| 57 | + return f"{total / secs:,.0f}" |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + parser = argparse.ArgumentParser( |
| 62 | + description="One-shot benchmark for push actor send regression checks (ARM-friendly)." |
| 63 | + ) |
| 64 | + parser.add_argument("--count", type=int, default=100_000, help="Number of actors/messages") |
| 65 | + parser.add_argument("--budget", type=int, default=10, help="Actor budget") |
| 66 | + parser.add_argument( |
| 67 | + "--payload-size", |
| 68 | + type=int, |
| 69 | + default=4, |
| 70 | + help="Payload size in bytes", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--rounds", |
| 74 | + type=int, |
| 75 | + default=3, |
| 76 | + help="How many rounds to run for the push path", |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "--include-pull", |
| 80 | + action="store_true", |
| 81 | + help="Also benchmark pull/mailbox actors in same run", |
| 82 | + ) |
| 83 | + args = parser.parse_args() |
| 84 | + |
| 85 | + payload = b"p" * args.payload_size |
| 86 | + |
| 87 | + print("--- Iris Push Regression Benchmark ---") |
| 88 | + print(f"version={iris.version()}") |
| 89 | + print( |
| 90 | + f"count={args.count:,}, budget={args.budget}, payload_size={args.payload_size}, rounds={args.rounds}" |
| 91 | + ) |
| 92 | + print("Tip: run this script on two commits and compare send msg/s.") |
| 93 | + |
| 94 | + push_spawn_times = [] |
| 95 | + push_send_times = [] |
| 96 | + |
| 97 | + for i in range(args.rounds): |
| 98 | + rt = iris.Runtime() |
| 99 | + pids = [] |
| 100 | + try: |
| 101 | + pids, spawn_t = spawn_push_actors(rt, args.count, args.budget) |
| 102 | + send_t = send_one_per_actor(rt, pids, payload) |
| 103 | + push_spawn_times.append(spawn_t) |
| 104 | + push_send_times.append(send_t) |
| 105 | + |
| 106 | + print( |
| 107 | + f"round {i + 1} push: spawn={spawn_t:.3f}s ({fmt_rate(args.count, spawn_t)} actors/s) | " |
| 108 | + f"send={send_t:.3f}s ({fmt_rate(args.count, send_t)} msg/s)" |
| 109 | + ) |
| 110 | + finally: |
| 111 | + cleanup(rt, pids) |
| 112 | + |
| 113 | + print("\n--- Push Summary (mean) ---") |
| 114 | + mean_spawn = mean(push_spawn_times) |
| 115 | + mean_send = mean(push_send_times) |
| 116 | + print(f"spawn: {mean_spawn:.3f}s ({fmt_rate(args.count, mean_spawn)} actors/s)") |
| 117 | + print(f"send : {mean_send:.3f}s ({fmt_rate(args.count, mean_send)} msg/s)") |
| 118 | + |
| 119 | + if args.include_pull: |
| 120 | + rt = iris.Runtime() |
| 121 | + pids = [] |
| 122 | + try: |
| 123 | + pids, pull_spawn_t = spawn_pull_actors(rt, args.count, args.budget) |
| 124 | + pull_send_t = send_one_per_actor(rt, pids, payload) |
| 125 | + print("\n--- Pull (mailbox) One Shot ---") |
| 126 | + print( |
| 127 | + f"spawn={pull_spawn_t:.3f}s ({fmt_rate(args.count, pull_spawn_t)} actors/s) | " |
| 128 | + f"send={pull_send_t:.3f}s ({fmt_rate(args.count, pull_send_t)} msg/s)" |
| 129 | + ) |
| 130 | + finally: |
| 131 | + cleanup(rt, pids) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments