-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·351 lines (291 loc) · 12.6 KB
/
Copy pathrun.py
File metadata and controls
executable file
·351 lines (291 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
"""interop_bench orchestrator.
Runs a publisher × subscriber matrix of MoQ client implementations against a
local relay, one pair at a time:
fixture -> publisher adapter -> moq-relay -> subscriber adapter -> received.media
| |
events_pub.csv events_sub.csv
For each pair it manages the relay, spawns the two adapter processes with the
shared env contract (see README), samples their CPU/RSS, and hands the pair
directory to analyze.py. Ends with a run-level report.md.
NOTE: this file is LLM-generated.
Usage:
./run.py # all available pairs
./run.py --pairs membrane:moq-cli,moq-cli:membrane
./run.py --list # probe adapters and show availability
"""
import argparse
import json
import os
import shutil
import signal
import subprocess
import sys
import time
from pathlib import Path
import analyze
BENCH_DIR = Path(__file__).resolve().parent
# Convenience fallback for running inside a membrane_moq_plugin checkout that
# carries a built reference clone; everything else resolves via env or $PATH.
LEGACY_MOQ_REF = BENCH_DIR.parent / ".reference" / "moq"
DEFAULT_FIXTURES = {
"h264": BENCH_DIR / "fixtures" / "bbb_20s_25fps.h264",
"h264-bframes": BENCH_DIR / "fixtures" / "bbb_20s_25fps_bframes.h264",
"h265": BENCH_DIR / "fixtures" / "bbb_20s_25fps_hevc.h265",
"aac": BENCH_DIR / "fixtures" / "bbb_20s.aac",
}
SUFFIX_MEDIA = {".h264": "h264", ".h265": "h265", ".aac": "aac"}
DEFAULT_FPS = 25
DEFAULT_RELAY_URL = "https://localhost:4443/anon"
WARMUP_S = 2.0 # publisher head start before the subscriber joins
def find_tool(env_var, names, legacy):
"""Resolve an external binary: $env_var override, then $PATH, then the
reference-clone debug build. None if nowhere."""
override = os.environ.get(env_var)
if override:
return override
for name in names:
path = shutil.which(name)
if path:
return path
if legacy.exists():
return str(legacy)
return None
# --- process tree resource accounting ----------------------------------------
def read_stats(stats_path: Path):
"""Resource summary written by wrap.py, reshaped for metrics/report."""
if not stats_path.exists():
return None
stats = json.loads(stats_path.read_text())
if stats["wall_s"] <= 0:
return None
return {
"cpu_mean_pct": round(100 * (stats["user_s"] + stats["sys_s"]) / stats["wall_s"], 1),
"rss_max_mb": round(stats["max_rss_bytes"] / 2**20, 1),
"wall_s": stats["wall_s"],
}
# --- relay management --------------------------------------------------------
class Relay:
"""Uses an already-running relay when one exists, otherwise starts one
(resolved via $MOQ_RELAY / $PATH / the reference clone) and stops it
afterwards."""
def __init__(self, url):
self.url = url
self.proc = None
def __enter__(self):
if self.url != DEFAULT_RELAY_URL:
print(f"using external relay ({self.url})")
return self
running = subprocess.run(["pgrep", "-x", "moq-relay"], capture_output=True)
if running.returncode == 0:
print(f"using already-running moq-relay ({self.url})")
return self
relay_bin = find_tool(
"MOQ_RELAY", ["moq-relay"], LEGACY_MOQ_REF / "target" / "debug" / "moq-relay"
)
if relay_bin is None:
sys.exit(
"no running relay and no moq-relay binary"
" (checked $MOQ_RELAY, $PATH, the reference clone);"
" enter the nix dev shell or set --relay-url to a running relay"
)
self.proc = subprocess.Popen(
[relay_bin, str(BENCH_DIR / "relay.toml")],
cwd=BENCH_DIR,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
time.sleep(1.5)
if self.proc.poll() is not None:
sys.exit("moq-relay exited immediately; run it manually to see why")
print(f"started moq-relay (pid {self.proc.pid}, {self.url})")
return self
def __exit__(self, *exc):
if self.proc and self.proc.poll() is None:
self.proc.terminate()
try:
self.proc.wait(timeout=5)
except subprocess.TimeoutExpired:
self.proc.kill()
# --- adapter execution -------------------------------------------------------
def load_registry():
return json.loads((BENCH_DIR / "adapters" / "registry.json").read_text())
def base_env():
env = os.environ.copy()
# The nix package ships the moq-cli crate's binary as `moq`.
cli = find_tool("MOQ_CLI", ["moq", "moq-cli"], LEGACY_MOQ_REF / "target" / "debug" / "moq-cli")
if cli:
env["MOQ_CLI"] = cli
return env
def probe(adapter):
try:
result = subprocess.run(
adapter["probe"], cwd=BENCH_DIR, env=base_env(),
capture_output=True, text=True, timeout=60,
)
return result.returncode == 0, result.stderr.strip()
except (subprocess.TimeoutExpired, FileNotFoundError) as error:
return False, str(error)
def spawn(cmd, env, log_path, stats_path):
log = open(log_path, "w")
wrapped = [sys.executable, str(BENCH_DIR / "wrap.py"), str(stats_path), "--"] + cmd
proc = subprocess.Popen(
wrapped, cwd=BENCH_DIR, env=env, stdout=log, stderr=subprocess.STDOUT,
start_new_session=True,
)
proc.log_file = log
return proc
def stop(proc, grace_s):
"""Wait for a clean exit; escalate SIGINT -> SIGKILL on the process group.
Returns (exit_code, forced): forced means the process only stopped because
the harness signalled it (normal for subscribers that follow a broadcast
indefinitely instead of exiting when it ends).
"""
forced = False
try:
proc.wait(timeout=grace_s)
except subprocess.TimeoutExpired:
forced = True
try:
os.killpg(proc.pid, signal.SIGINT)
proc.wait(timeout=5)
except (subprocess.TimeoutExpired, ProcessLookupError):
try:
os.killpg(proc.pid, signal.SIGKILL)
except ProcessLookupError:
pass
proc.wait()
proc.log_file.close()
return proc.returncode, forced
def run_pair(pub_name, sub_name, registry, args, run_dir, fps, clip_duration_s):
pub = registry["publishers"][pub_name]
sub = registry["subscribers"][sub_name]
pair_dir = run_dir / f"{pub_name}__{sub_name}"
pair_dir.mkdir(parents=True)
broadcast = f"bench-{pub_name}-{sub_name}.hang"
timeout_s = clip_duration_s + 40
env = base_env()
env.update(
MOQ_URL=args.relay_url,
MOQ_BROADCAST=broadcast,
BENCH_MEDIA=args.media,
BENCH_INPUT=str(args.fixture),
BENCH_INPUT_MP4=str(args.fixture.with_suffix(".mp4")),
# rounded for adapters doing shell arithmetic (audio: ~43.07 -> 43)
BENCH_FPS=str(round(fps)),
BENCH_OUTPUT=str(pair_dir / "received.media"),
BENCH_TIMEOUT_MS=str(int(timeout_s * 1000)),
)
print(f"\n=== {pub_name} -> {sub_name} (broadcast {broadcast}) ===")
pub_env = env | pub.get("env", {}) | {"BENCH_EVENTS": str(pair_dir / "events_pub.csv")}
pub_proc = spawn(pub["cmd"], pub_env, pair_dir / "pub.log", pair_dir / "stats_pub.json")
pub_spawn_ns = time.time_ns()
time.sleep(WARMUP_S)
if pub_proc.poll() is not None:
print(f"publisher exited during warmup (code {pub_proc.returncode}); see pub.log")
sub_env = env | sub.get("env", {}) | {"BENCH_EVENTS": str(pair_dir / "events_sub.csv")}
sub_proc = spawn(sub["cmd"], sub_env, pair_dir / "sub.log", pair_dir / "stats_sub.json")
sub_spawn_ns = time.time_ns()
# The publisher ends the broadcast when done; the subscriber should then
# exit on its own. Grace periods cover implementations that don't.
pub_exit, pub_forced = stop(pub_proc, timeout_s)
sub_exit, sub_forced = stop(sub_proc, 15)
meta = {
"pub": pub_name,
"sub": sub_name,
"pub_exit": pub_exit,
"sub_exit": sub_exit,
"pub_forced_stop": pub_forced,
"sub_forced_stop": sub_forced,
"pub_resources": read_stats(pair_dir / "stats_pub.json"),
"sub_resources": read_stats(pair_dir / "stats_sub.json"),
"pub_spawn_wall_ns": pub_spawn_ns,
"sub_spawn_wall_ns": sub_spawn_ns,
"pub_events": pub["events"],
"sub_events": sub["events"],
"sub_output_format": sub["output"],
"media": args.media,
"fps": fps,
"fixture": str(args.fixture),
"broadcast": broadcast,
}
(pair_dir / "meta.json").write_text(json.dumps(meta, indent=2) + "\n")
metrics = analyze.analyze_pair(pair_dir)
d = metrics["delivery"]
print(
f"{metrics['status']}: {d['frames_matched']}/{d['frames_fixture']} frames"
f" (corrupt {d['corrupt']}), startup {metrics['startup_ms']} ms,"
f" e2e {metrics['e2e_latency'] and metrics['e2e_latency']['p50_ms']} ms p50"
)
return metrics
# --- main ---------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--pairs", help="comma-separated pub:sub pairs (default: all available)")
parser.add_argument("--media", choices=[*DEFAULT_FIXTURES, "video", "audio"],
help="fixture format (default: inferred from the fixture suffix;"
" video/audio are aliases for h264/aac)")
parser.add_argument("--fixture", type=Path, default=None)
parser.add_argument("--fps", type=int, default=DEFAULT_FPS,
help="video fixture frame rate (aac derives its own from the ADTS header)")
parser.add_argument("--relay-url", default=DEFAULT_RELAY_URL)
parser.add_argument("--results", type=Path, default=BENCH_DIR / "results")
parser.add_argument("--list", action="store_true", help="probe adapters and exit")
args = parser.parse_args()
if args.media:
args.media = analyze.canonical_media(args.media)
if args.fixture is None:
args.fixture = DEFAULT_FIXTURES[args.media or "h264"]
if args.media is None:
# A B-frame fixture shares the .h264 suffix; select it via --media.
args.media = SUFFIX_MEDIA.get(args.fixture.suffix, "h264")
registry = load_registry()
def supports_media(adapter):
return args.media in adapter.get("media", ["h264"])
available = {"publishers": {}, "subscribers": {}}
for role in available:
for name, adapter in registry[role].items():
if not supports_media(adapter):
available[role][name] = False
if args.list:
print(f"{role[:-1]} {name}: skipped (no {args.media} adapter)")
continue
ok, why = probe(adapter)
available[role][name] = ok
if args.list or not ok:
status = "available" if ok else f"unavailable ({why or 'probe failed'})"
print(f"{role[:-1]} {name}: {status}")
if args.list:
return
if args.pairs:
pairs = [tuple(pair.split(":")) for pair in args.pairs.split(",")]
for pub_name, sub_name in pairs:
for role, name in (("publishers", pub_name), ("subscribers", sub_name)):
if not supports_media(registry[role][name]):
sys.exit(f"{role[:-1]} {name} has no {args.media} adapter")
else:
pairs = [
(p, s)
for p, ok_p in available["publishers"].items() if ok_p
for s, ok_s in available["subscribers"].items() if ok_s
]
if not pairs:
sys.exit("no runnable pairs")
if not args.fixture.exists():
sys.exit(f"fixture missing: {args.fixture} (run make_fixture.sh)")
fixture_bytes = args.fixture.read_bytes()
frames = analyze.fixture_frames(args.media, fixture_bytes)
fps = analyze.adts_frame_rate(fixture_bytes) if args.media == "aac" else args.fps
clip_duration_s = len(frames) / fps
print(f"fixture: {args.fixture.name} ({args.media}),"
f" {len(frames)} frames, {clip_duration_s:.0f}s @ {fps:.1f}fps")
run_dir = args.results / time.strftime("%Y%m%d_%H%M%S")
run_dir.mkdir(parents=True)
with Relay(args.relay_url):
for pub_name, sub_name in pairs:
run_pair(pub_name, sub_name, registry, args, run_dir, fps, clip_duration_s)
report = analyze.write_report(run_dir)
print(f"\n{report.read_text()}")
print(f"results: {run_dir}")
if __name__ == "__main__":
main()