Files
computer-playbook/plugins/lookup/playwright_workers.py
T
kevinveenbirkenbachandClaude Opus 5 370c8d54c8 refactor(lookup): reuse the MemTotal probe
_ram_gb reimplemented the /proc/meminfo parse that utils/env/runtime.py owns: same pseudo-file, same MemTotal scan, same line.split()[1], same swallow-and-fall-back guard. It now calls mem_total_mb().

The error semantics map exactly. The helper returns 0 for an unreadable file, a malformed line and a missing field; all three previously produced 4.0, and 0 is falsy, so the conservative fallback still covers every one of them.

One real delta: the helper floors kB to MB before the GB conversion, so _ram_gb loses up to 0.001 GB. That can only move the worker count where MemTotal/2 sits within half a megabyte of an exact 1.5 GiB multiple, and it moves it downwards, which is the direction this sizing already prefers.

Touching the file pulled its ten pre-existing narration comments into the diff-scoped comment lint. They were deleted, not relabelled.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 17:05:50 +02:00

80 lines
2.3 KiB
Python

from __future__ import annotations
import os
from typing import Any
from ansible.plugins.lookup import LookupBase
from utils.env.runtime import mem_total_mb
_PER_WORKER_GB = 1.5
_RAM_FRACTION = 0.5
_CPU_DIVISOR = 4
_HARD_CAP = 6
_CI_CAP = 2
_CI_ENV = ("CI", "GITHUB_ACTIONS", "GITLAB_CI", "BUILDKITE", "JENKINS_URL")
def _cpu_count() -> int:
try:
return max(1, len(os.sched_getaffinity(0)))
except (AttributeError, OSError):
return max(1, os.cpu_count() or 1)
def _ram_gb() -> float:
total_mb = mem_total_mb()
return total_mb / 1024 if total_mb else 4.0
def _is_ci() -> bool:
return any(os.environ.get(k) for k in _CI_ENV)
def compute_workers(
cpus: int,
ram_gb: float,
ci: bool,
*,
per_worker_gb: float = _PER_WORKER_GB,
ram_fraction: float = _RAM_FRACTION,
cpu_divisor: int = _CPU_DIVISOR,
hard_cap: int = _HARD_CAP,
ci_cap: int = _CI_CAP,
) -> int:
cpu_workers = max(1, cpus // max(1, cpu_divisor))
ram_workers = max(1, int((ram_gb * ram_fraction) // per_worker_gb))
workers = min(cpu_workers, ram_workers, hard_cap)
if ci:
workers = min(workers, ci_cap)
return max(1, workers)
class LookupModule(LookupBase):
"""
lookup('playwright_workers')
Conservative, stable parallel-worker count for the Playwright e2e suite.
Derived from the control host's effective CPU and RAM, divided down because
browser workers are heavy and run against the co-resident application stack,
reserved against RAM, hard-capped for the application-under-test's
concurrency limits, and reduced further on CI. Always returns an int >= 1.
Tunable via kwargs: cpu_divisor, hard_cap, ci_cap, per_worker_gb,
ram_fraction.
"""
def run(self, terms, variables: dict[str, Any] | None = None, **kwargs):
return [
compute_workers(
_cpu_count(),
_ram_gb(),
_is_ci(),
per_worker_gb=float(kwargs.get("per_worker_gb", _PER_WORKER_GB)),
ram_fraction=float(kwargs.get("ram_fraction", _RAM_FRACTION)),
cpu_divisor=int(kwargs.get("cpu_divisor", _CPU_DIVISOR)),
hard_cap=int(kwargs.get("hard_cap", _HARD_CAP)),
ci_cap=int(kwargs.get("ci_cap", _CI_CAP)),
)
]