Files
wehub-resource-sync 0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

116 lines
4.3 KiB
Python

"""Regression tests for the LocalEmbedder CPU thread cap (issue #198).
Under concurrent load the torch/sentence-transformers embedder oversubscribes
BLAS/OpenMP threads (≈ ``os.cpu_count()`` per ``encode()``), starving the
asyncio event loop and spiking ``/livez`` latency. ``LocalEmbedder`` now runs
CPU encodes on a dedicated, size-limited executor whose workers each pin their
torch/BLAS/OpenMP thread pool, bounding total embedding threads to
``HEADROOM_EMBED_CONCURRENCY x HEADROOM_EMBED_NUM_THREADS``. The ONNX embedder
already caps its threads; this brings the torch path to parity.
"""
from __future__ import annotations
import os
import pytest
from headroom.memory.adapters import embedders
from headroom.memory.adapters.embedders import (
_BLAS_THREAD_ENV_VARS,
_init_cpu_embed_worker,
_resolve_embed_concurrency,
_resolve_embed_thread_cap,
)
# ---------------------------------------------------------------------------
# Env resolution (no torch required)
# ---------------------------------------------------------------------------
def test_thread_cap_default_when_unset(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("HEADROOM_EMBED_NUM_THREADS", raising=False)
assert _resolve_embed_thread_cap() == 1
def test_thread_cap_reads_positive_int(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "3")
assert _resolve_embed_thread_cap() == 3
def test_thread_cap_invalid_falls_back(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "not-a-number")
assert _resolve_embed_thread_cap() == 1
def test_thread_cap_non_positive_is_clamped(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "0")
assert _resolve_embed_thread_cap() == 1
def test_concurrency_default_is_bounded(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("HEADROOM_EMBED_CONCURRENCY", raising=False)
value = _resolve_embed_concurrency()
assert 1 <= value <= 4
assert value <= (os.cpu_count() or 1)
def test_concurrency_reads_positive_int(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_EMBED_CONCURRENCY", "7")
assert _resolve_embed_concurrency() == 7
# ---------------------------------------------------------------------------
# Worker initializer env application (no torch required)
# ---------------------------------------------------------------------------
def test_worker_init_sets_blas_env_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
for var in _BLAS_THREAD_ENV_VARS:
monkeypatch.delenv(var, raising=False)
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "2")
_init_cpu_embed_worker()
for var in _BLAS_THREAD_ENV_VARS:
assert os.environ[var] == "2", var
def test_worker_init_does_not_override_operator_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""An explicit operator setting must win over our default (setdefault)."""
monkeypatch.setenv("OMP_NUM_THREADS", "8")
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "1")
_init_cpu_embed_worker()
assert os.environ["OMP_NUM_THREADS"] == "8"
# ---------------------------------------------------------------------------
# Behavioral: real CPU load path bounds every encode worker's thread pool
# ---------------------------------------------------------------------------
async def test_cpu_embed_workers_are_thread_capped(monkeypatch: pytest.MonkeyPatch) -> None:
"""CPU encodes run on a dedicated, size-limited executor and every worker
pins its torch intra-op thread pool to the configured cap."""
torch = pytest.importorskip("torch")
pytest.importorskip("sentence_transformers")
monkeypatch.setenv("HEADROOM_EMBED_NUM_THREADS", "1")
monkeypatch.setenv("HEADROOM_EMBED_CONCURRENCY", "2")
emb = embedders.LocalEmbedder(device="cpu")
await emb.embed("hello world")
assert emb._device == "cpu"
assert emb._executor is not None
assert emb._executor._max_workers == 2 # type: ignore[attr-defined]
# Probe the actual encode workers: each was pinned to 1 intra-op thread.
futures = [emb._executor.submit(torch.get_num_threads) for _ in range(4)]
assert [f.result() for f in futures] == [1, 1, 1, 1]
await emb.close()
assert emb._executor is None # close() tears the executor down