Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-3636 [PERF] Stop the test rig wasting CPU and hashing time (#2195)

Chandrasekharan Me2485a869e3f
diff --git a/.github/workflows/ci-test.yaml b/.github/workflows/ci-test.yaml
index b0fe661ec..f515b14ca 100644
--- a/.github/workflows/ci-test.yaml
+++ b/.github/workflows/ci-test.yaml
@@ -80,6 +80,7 @@ jobs:
with:
version: "0.6.14"
python-version: 3.12.9
+ enable-cache: true
- name: Cache tox environments
uses: actions/cache@v5
@@ -159,6 +160,7 @@ jobs:
with:
version: "0.6.14"
python-version: 3.12.9
+ enable-cache: true
- name: Cache tox environments
uses: actions/cache@v5
@@ -253,6 +255,7 @@ jobs:
with:
version: "0.6.14"
python-version: 3.12.9
+ enable-cache: true
- name: Cache tox environments
uses: actions/cache@v5
diff --git a/backend/backend/settings/test.py b/backend/backend/settings/test.py
index 4656e0749..f1ad7d584 100644
--- a/backend/backend/settings/test.py
+++ b/backend/backend/settings/test.py
@@ -1,3 +1,8 @@
from backend.settings.base import * # noqa: F401, F403
DEBUG = True
+
+# Django's default PBKDF2 hasher is deliberately slow; suites that seed several
+# users per test spend most of their time there. Test fixtures need speed, not
+# resistance to offline cracking.
+PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
diff --git a/backend/dashboard_metrics/tests/test_tasks.py b/backend/dashboard_metrics/tests/test_tasks.py
index ac45887d4..03ef13650 100644
--- a/backend/dashboard_metrics/tests/test_tasks.py
+++ b/backend/dashboard_metrics/tests/test_tasks.py
@@ -2,7 +2,7 @@
from datetime import datetime, timedelta
-from django.test import TestCase, TransactionTestCase
+from django.test import TestCase
from django.utils import timezone
from account_v2.models import Organization
@@ -81,7 +81,7 @@ class TestTimeHelpers(TestCase):
assert result.microsecond == 0
-class TestCleanupTasks(TransactionTestCase):
+class TestCleanupTasks(TestCase):
"""Tests for cleanup tasks."""
def setUp(self):
diff --git a/backend/pyproject.toml b/backend/pyproject.toml
index 46f082113..ce01979b3 100644
--- a/backend/pyproject.toml
+++ b/backend/pyproject.toml
@@ -103,7 +103,10 @@ constraint-dependencies = [
[tool.pytest.ini_options]
# Note: test.env is loaded by backend/conftest.py via python-dotenv directly
# (replaces the unmaintained pytest-dotenv plugin).
-addopts = "-s"
+# --no-migrations builds the schema straight from the models: the migration
+# history is replayed once per xdist worker otherwise, and nothing here asserts
+# on it.
+addopts = "--no-migrations"
python_files = ["test_*.py", "*_test.py", "*_tests.py", "tests.py"]
markers = [
"integration: needs live infra (Postgres/Redis); runs in the rig's integration tier, not unit (select with -m integration / exclude with -m 'not integration')",
diff --git a/tests/rig/cli.py b/tests/rig/cli.py
index 238184c0c..b959d3ab7 100644
--- a/tests/rig/cli.py
+++ b/tests/rig/cli.py
@@ -101,6 +101,21 @@ def main(argv: list[str] | None = None) -> int:
return args.func(args)
+def _default_workers() -> str:
+ """Resolve the xdist worker count ourselves.
+
+ xdist's own `auto` prefers *physical* cores when psutil happens to be
+ importable, which collapses to a single worker on hyperthreaded CI runners
+ — and only for the groups that ship psutil. Beyond ~8 the suites contend on
+ the test database more than they parallelise, so cap it.
+ """
+ try:
+ cpus = len(os.sched_getaffinity(0)) # honours CPU pinning; Linux only
+ except AttributeError:
+ cpus = os.cpu_count() or 1
+ return str(min(cpus, 8))
+
+
def _build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(prog="tests.rig", description="Unstract unified test rig")
sub = p.add_subparsers(dest="command", required=True)
@@ -120,8 +135,8 @@ def _build_parser() -> argparse.ArgumentParser:
pr.add_argument("--no-parallel", dest="parallel", action="store_false")
pr.add_argument(
"--workers",
- default="auto",
- help="pytest-xdist worker count (default: auto).",
+ default=_default_workers(),
+ help="pytest-xdist worker count (default: usable CPUs, capped).",
)
pr.add_argument("--timeout", type=int, help="Per-group timeout override in seconds.")
pr.add_argument("--reports-dir", type=Path, default=REPO_ROOT / "reports")
@@ -784,10 +799,20 @@ def _execute_group(
md_report = group_reports / "report.md"
env = _subprocess_env()
+ # A group may set its own PYTHONPATH (workdir-relative imports); keep the rig
+ # plugin dir on it so `-p rig_critical_path` stays importable instead of being
+ # clobbered by a plain env.update.
+ group_env = dict(group.env)
env["PYTHONPATH"] = os.pathsep.join(
- p for p in (str(_PYTEST_PLUGIN_DIR), env.get("PYTHONPATH")) if p
+ p
+ for p in (
+ str(_PYTEST_PLUGIN_DIR),
+ group_env.pop("PYTHONPATH", None),
+ env.get("PYTHONPATH"),
+ )
+ if p
)
- env.update(group.env)
+ env.update(group_env)
if endpoints is not None:
env.setdefault("UNSTRACT_BACKEND_URL", endpoints.backend_url)
env.setdefault("UNSTRACT_PROMPT_SERVICE_URL", endpoints.prompt_service_url)
diff --git a/tests/rig/groups.py b/tests/rig/groups.py
index 3a2c7b25d..d6f183ea7 100644
--- a/tests/rig/groups.py
+++ b/tests/rig/groups.py
@@ -173,7 +173,7 @@ def _load_manifest_dict(manifest_path: Path) -> dict[str, Any]:
if not manifest_path.is_file():
raise ValueError(f"{manifest_path}: manifest file not found")
raw = yaml.safe_load(manifest_path.read_text())
- if not isinstance(raw, dict) or "groups" not in raw:
+ if not isinstance(raw, dict) or not isinstance(raw.get("groups"), dict):
raise ValueError(f"{manifest_path}: expected top-level `groups:` mapping")
return raw
diff --git a/unstract/sdk1/tests/utils/test_retry_utils.py b/unstract/sdk1/tests/utils/test_retry_utils.py
index 227734414..5d001d85c 100644
--- a/unstract/sdk1/tests/utils/test_retry_utils.py
+++ b/unstract/sdk1/tests/utils/test_retry_utils.py
@@ -1,6 +1,7 @@
"""Unit tests for retry_utils module."""
import errno
+import time
from collections.abc import Callable
from unittest.mock import MagicMock, Mock
@@ -16,6 +17,12 @@ from unstract.sdk1.utils.retry_utils import (
)
[email protected](autouse=True)
+def _no_retry_sleep(monkeypatch: MonkeyPatch) -> None:
+ """Backoff logic is under test, not wall-clock timing — skip real sleeps."""
+ monkeypatch.setattr(time, "sleep", lambda *_a, **_k: None)
+
+
class TestIsRetryableError:
"""Tests for is_retryable_error function."""