Files
hyprland-headless-stream-pl…/spikes/capture-speed/chart.py
T
Hermenix 36efa316fd spike: the verdict, the table, the chart -- in-process pywayland wins on zwlr-screencopy
Four routes measured on one frozen contract at one session's container size, with
RAM and CPU as first-class results: in-process pywayland is ~1.5x faster (78 vs
117 ms), uses ~2.8x less of our own CPU, and nearly halves the compositor's
per-frame CPU (67 vs 116 ms). grim-rs buys nothing and costs 746 MB RSS.
ext-image-copy-capture measures identical to screencopy and appears in a
compositor crash stack, so the deprecated-but-working protocol is the pick.

Also fixes the client's unclean exit (pywayland teardown segfaulted after the
result was printed, 16 times, one batch coinciding with a compositor crash).
2026-10-11 17:08:13 +00:00

141 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Turn results/results.json into the two pictures that decide this.
One chart per question, because they answer different things:
- **speed**: p50 frame time and sustained fps per route and cell -- which route is
quickest;
- **machine cost**: the compositor's CPU per frame per route -- which route leaves
the box able to render, which is the question the frame rate does not answer.
The idle control is drawn as a line on the cost chart so a reader can see how
much of the machine was busy before any capture happened.
nix-shell --run "python3 chart.py" # -> results/head-to-head.png
"""
from __future__ import annotations
import json
import os
import statistics
import sys
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
HERE = os.path.dirname(os.path.abspath(__file__))
# Which pass to chart: `python3 chart.py results/final-motion`
RESULTS = os.path.join(HERE, sys.argv[1] if len(sys.argv) > 1 else "results")
LABELS = {
"grim": "grim (subprocess)",
"grim-rs": "grim-rs (Rust)",
"py-screencopy": "pywayland\nscreencopy",
"py-ext": "pywayland\next-copy-capture",
}
ORDER = ["grim", "grim-rs", "py-screencopy", "py-ext"]
CELLS = ["output-jpeg", "output-raw", "all-jpeg", "all-raw"]
def load() -> tuple[list[dict], dict | None]:
with open(os.path.join(RESULTS, "results.json")) as handle:
records = json.load(handle)
control = None
try:
with open(os.path.join(RESULTS, "control.json")) as handle:
control = json.load(handle)
except OSError:
pass
return records, control
def cell_value(records: list[dict], route: str, cell: str, key: str) -> float | None:
values = [
r.get(key)
for r in records
if r.get("route") == route and r.get("cell") == cell and r.get(key) is not None
]
if not values:
return None
return statistics.median(values)
def main() -> int:
records, control = load()
routes = [r for r in ORDER if any(rec.get("route") == r for rec in records)]
if not routes:
print("no successful runs in results.json", file=sys.stderr)
return 1
fig, axes = plt.subplots(1, 2, figsize=(15, 6.5), facecolor="#0f1115")
for axis in axes:
axis.set_facecolor("#0f1115")
for spine in axis.spines.values():
spine.set_color("#475569")
axis.tick_params(colors="#cbd5e1", labelsize=9)
axis.grid(axis="y", color="#1e293b", linewidth=0.8)
axis.set_axisbelow(True)
width = 0.8 / len(routes)
colours = ["#f87171", "#fbbf24", "#60a5fa", "#34d399"]
# -- left: speed ---------------------------------------------------------
axis = axes[0]
for index, route in enumerate(routes):
values, positions = [], []
for cell_index, cell in enumerate(CELLS):
value = cell_value(records, route, cell, "fps_sustained")
if value is None:
continue
values.append(value)
positions.append(cell_index + index * width)
axis.bar(positions, values, width, label=LABELS.get(route, route),
color=colours[index % len(colours)], edgecolor="#0f1115")
axis.set_title("speed — sustained frames per second", color="#e2e8f0", fontsize=12)
axis.set_xticks([i + 0.4 - width / 2 for i in range(len(CELLS))])
axis.set_xticklabels([c.replace("-", "\n") for c in CELLS], fontsize=8)
axis.set_ylabel("fps", color="#cbd5e1")
axis.legend(facecolor="#111827", edgecolor="#334155", labelcolor="#e2e8f0", fontsize=8)
# -- right: what it costs the compositor --------------------------------
axis = axes[1]
for index, route in enumerate(routes):
values, positions = [], []
for cell_index, cell in enumerate(CELLS):
value = cell_value(records, route, cell, "compositor_cpu_ms_per_frame")
if value is None:
continue
values.append(value)
positions.append(cell_index + index * width)
axis.bar(positions, values, width, label=LABELS.get(route, route),
color=colours[index % len(colours)], edgecolor="#0f1115")
axis.set_title("cost — the compositor's own CPU per frame (lower is better)",
color="#e2e8f0", fontsize=12)
axis.set_xticks([i + 0.4 - width / 2 for i in range(len(CELLS))])
axis.set_xticklabels([c.replace("-", "\n") for c in CELLS], fontsize=8)
axis.set_ylabel("ms of compositor CPU per frame", color="#cbd5e1")
if control and control.get("compositor_cpu_ms") is not None:
per_frame_note = (
f"idle control: machine {control['system_cpu_pct']}% of one core "
f"with nothing capturing"
)
axis.text(0.02, 0.95, per_frame_note, transform=axis.transAxes,
color="#94a3b8", fontsize=8, va="top")
axis.legend(facecolor="#111827", edgecolor="#334155", labelcolor="#e2e8f0", fontsize=8)
fig.suptitle("hhstream capture routes — 1280×720 container and 2560×720 stitched, "
"jpeg q80 and raw, moving screen, software rendering, no GPU",
color="#e2e8f0", fontsize=11)
fig.tight_layout(rect=(0, 0, 1, 0.95))
out = os.path.join(RESULTS, "head-to-head.png")
fig.savefig(out, dpi=140, facecolor="#0f1115")
print(out)
return 0
if __name__ == "__main__":
sys.exit(main())