|
| 1 | +"""Measure CogniOS process-tree and system overhead in browser or desktop mode.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import csv |
| 7 | +import json |
| 8 | +import os |
| 9 | +import statistics |
| 10 | +import time |
| 11 | +from datetime import datetime, timezone |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import psutil |
| 15 | + |
| 16 | + |
| 17 | +def summarize(samples: list[dict]) -> dict: |
| 18 | + fields = ( |
| 19 | + "system_cpu_pct", "app_cpu_pct", "app_rss_mb", "app_processes", |
| 20 | + "memory_available_mb", "swap_used_mb", "disk_read_mb_s", |
| 21 | + "disk_write_mb_s", "network_recv_mb_s", "network_sent_mb_s", |
| 22 | + ) |
| 23 | + if not samples: |
| 24 | + return {key: {"average": 0.0, "peak": 0.0, "minimum": 0.0} for key in fields} |
| 25 | + |
| 26 | + return { |
| 27 | + key: { |
| 28 | + "average": round(statistics.mean(sample[key] for sample in samples), 2), |
| 29 | + "peak": round(max(sample[key] for sample in samples), 2), |
| 30 | + "minimum": round(min(sample[key] for sample in samples), 2), |
| 31 | + } |
| 32 | + for key in fields |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def self_test() -> None: |
| 37 | + result = summarize([ |
| 38 | + {"system_cpu_pct": 10, "app_cpu_pct": 20, "app_rss_mb": 100, "app_processes": 2, |
| 39 | + "memory_available_mb": 1000, "swap_used_mb": 0, "disk_read_mb_s": 1, |
| 40 | + "disk_write_mb_s": 2, "network_recv_mb_s": 3, "network_sent_mb_s": 4}, |
| 41 | + {"system_cpu_pct": 30, "app_cpu_pct": 40, "app_rss_mb": 200, "app_processes": 4, |
| 42 | + "memory_available_mb": 800, "swap_used_mb": 1, "disk_read_mb_s": 3, |
| 43 | + "disk_write_mb_s": 4, "network_recv_mb_s": 5, "network_sent_mb_s": 6}, |
| 44 | + ]) |
| 45 | + assert result["system_cpu_pct"] == {"average": 20, "peak": 30, "minimum": 10} |
| 46 | + assert result["app_rss_mb"] == {"average": 150, "peak": 200, "minimum": 100} |
| 47 | + assert summarize([])["system_cpu_pct"] == {"average": 0.0, "peak": 0.0, "minimum": 0.0} |
| 48 | + |
| 49 | + |
| 50 | +def main() -> None: |
| 51 | + parser = argparse.ArgumentParser(description=__doc__) |
| 52 | + parser.add_argument("--mode", choices=("browser", "desktop")) |
| 53 | + parser.add_argument("--pid", type=int, help="PID of the CogniOS main.py process") |
| 54 | + parser.add_argument("--ui-pid", type=int, help="Optional browser PID; include its process tree") |
| 55 | + parser.add_argument("--duration", type=int, default=60, help="Sample duration in seconds") |
| 56 | + parser.add_argument("--interval", type=float, default=1, help="Sample interval in seconds") |
| 57 | + parser.add_argument("--output", type=Path, default=Path("ui-overhead-results")) |
| 58 | + parser.add_argument("--self-test", action="store_true") |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + if args.self_test: |
| 62 | + self_test() |
| 63 | + print("Benchmark summary check passed.") |
| 64 | + return |
| 65 | + if not args.mode or not args.pid or args.duration < 1 or args.interval <= 0: |
| 66 | + parser.error("--mode, --pid, positive --duration, and positive --interval are required") |
| 67 | + |
| 68 | + try: |
| 69 | + roots = [psutil.Process(args.pid)] |
| 70 | + if args.ui_pid: |
| 71 | + roots.append(psutil.Process(args.ui_pid)) |
| 72 | + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as exc: |
| 73 | + parser.error(f"Could not access process PID(s): {exc}") |
| 74 | + for proc in roots: |
| 75 | + proc.cpu_percent(None) |
| 76 | + psutil.cpu_percent(None) |
| 77 | + |
| 78 | + args.output.mkdir(parents=True, exist_ok=True) |
| 79 | + stamp = datetime.now().strftime("%Y%m%d-%H%M%S") |
| 80 | + stem = f"ui-overhead-{args.mode}-{stamp}" |
| 81 | + csv_path = args.output / f"{stem}.csv" |
| 82 | + json_path = args.output / f"{stem}.json" |
| 83 | + columns = ( |
| 84 | + "timestamp", "elapsed_s", "mode", "system_cpu_pct", "memory_used_mb", |
| 85 | + "memory_available_mb", "swap_used_mb", "disk_read_mb_s", "disk_write_mb_s", |
| 86 | + "network_recv_mb_s", "network_sent_mb_s", "app_cpu_pct", "app_rss_mb", |
| 87 | + "app_processes", "pid", "parent_pid", "process_name", "process_cpu_pct", |
| 88 | + "process_rss_mb", "threads", "read_mb", "write_mb", |
| 89 | + ) |
| 90 | + totals = [] |
| 91 | + started_at = datetime.now(timezone.utc) |
| 92 | + started = time.monotonic() |
| 93 | + previous_elapsed = 0.0 |
| 94 | + previous_disk = psutil.disk_io_counters() |
| 95 | + previous_network = psutil.net_io_counters() |
| 96 | + |
| 97 | + with csv_path.open("w", newline="", encoding="utf-8") as output: |
| 98 | + writer = csv.DictWriter(output, fieldnames=columns) |
| 99 | + writer.writeheader() |
| 100 | + while time.monotonic() - started < args.duration: |
| 101 | + time.sleep(min(args.interval, max(0, args.duration - (time.monotonic() - started)))) |
| 102 | + now = datetime.now(timezone.utc).isoformat() |
| 103 | + app_cpu = app_rss = process_count = 0 |
| 104 | + rows = [] |
| 105 | + seen = set() |
| 106 | + |
| 107 | + for root in roots: |
| 108 | + try: |
| 109 | + processes = [root, *root.children(recursive=True)] |
| 110 | + except psutil.Error: |
| 111 | + processes = [] |
| 112 | + for proc in processes: |
| 113 | + if proc.pid in seen: |
| 114 | + continue |
| 115 | + seen.add(proc.pid) |
| 116 | + try: |
| 117 | + info = proc.as_dict(attrs=["pid", "ppid", "name", "memory_info", "num_threads"]) |
| 118 | + cpu = proc.cpu_percent(None) |
| 119 | + io = proc.io_counters() |
| 120 | + rss = info["memory_info"].rss / (1024 * 1024) |
| 121 | + app_cpu += cpu |
| 122 | + app_rss += rss |
| 123 | + process_count += 1 |
| 124 | + rows.append({ |
| 125 | + "pid": proc.pid, "parent_pid": info["ppid"], "process_name": info["name"], |
| 126 | + "process_cpu_pct": round(cpu, 2), "process_rss_mb": round(rss, 2), |
| 127 | + "threads": info["num_threads"], |
| 128 | + "read_mb": round(io.read_bytes / (1024 * 1024), 2) if io else "", |
| 129 | + "write_mb": round(io.write_bytes / (1024 * 1024), 2) if io else "", |
| 130 | + }) |
| 131 | + except (psutil.Error, OSError): |
| 132 | + continue |
| 133 | + |
| 134 | + memory = psutil.virtual_memory() |
| 135 | + system_cpu = psutil.cpu_percent(None) |
| 136 | + elapsed = time.monotonic() - started |
| 137 | + interval = max(elapsed - previous_elapsed, 0.001) |
| 138 | + disk = psutil.disk_io_counters() |
| 139 | + network = psutil.net_io_counters() |
| 140 | + disk_read = ( |
| 141 | + max(0, disk.read_bytes - previous_disk.read_bytes) / 1048576 / interval |
| 142 | + if disk and previous_disk else 0 |
| 143 | + ) |
| 144 | + disk_write = ( |
| 145 | + max(0, disk.write_bytes - previous_disk.write_bytes) / 1048576 / interval |
| 146 | + if disk and previous_disk else 0 |
| 147 | + ) |
| 148 | + network_recv = ( |
| 149 | + max(0, network.bytes_recv - previous_network.bytes_recv) / 1048576 / interval |
| 150 | + if network and previous_network else 0 |
| 151 | + ) |
| 152 | + network_sent = ( |
| 153 | + max(0, network.bytes_sent - previous_network.bytes_sent) / 1048576 / interval |
| 154 | + if network and previous_network else 0 |
| 155 | + ) |
| 156 | + previous_elapsed, previous_disk, previous_network = elapsed, disk, network |
| 157 | + sample = { |
| 158 | + "system_cpu_pct": system_cpu, |
| 159 | + "app_cpu_pct": app_cpu, |
| 160 | + "app_rss_mb": app_rss, |
| 161 | + "app_processes": process_count, |
| 162 | + "memory_available_mb": round(memory.available / (1024 * 1024), 2), |
| 163 | + "swap_used_mb": round(psutil.swap_memory().used / (1024 * 1024), 2), |
| 164 | + "disk_read_mb_s": round(disk_read, 3), |
| 165 | + "disk_write_mb_s": round(disk_write, 3), |
| 166 | + "network_recv_mb_s": round(network_recv, 3), |
| 167 | + "network_sent_mb_s": round(network_sent, 3), |
| 168 | + } |
| 169 | + totals.append(sample) |
| 170 | + shared = { |
| 171 | + "timestamp": now, |
| 172 | + "elapsed_s": round(elapsed, 2), |
| 173 | + "mode": args.mode, |
| 174 | + **sample, |
| 175 | + "memory_used_mb": round(memory.used / (1024 * 1024), 2), |
| 176 | + } |
| 177 | + for row in rows or [{}]: |
| 178 | + writer.writerow({**shared, **row}) |
| 179 | + output.flush() |
| 180 | + |
| 181 | + report = { |
| 182 | + "mode": args.mode, |
| 183 | + "started_at": started_at.isoformat(), |
| 184 | + "duration_seconds": round(time.monotonic() - started, 2), |
| 185 | + "sample_interval_seconds": args.interval, |
| 186 | + "samples": len(totals), |
| 187 | + "process_roots": [proc.pid for proc in roots], |
| 188 | + "summary": summarize(totals), |
| 189 | + "rss_note": "Process-tree RSS is summed and can double-count shared memory.", |
| 190 | + "csv": str(csv_path), |
| 191 | + } |
| 192 | + json_path.write_text(json.dumps(report, indent=2), encoding="utf-8") |
| 193 | + print(json.dumps(report, indent=2)) |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + main() |
0 commit comments