Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

8.3 KB
Raw Blame
"""Integration tests for extract endpoint with real server.

These tests verify end-to-end behavior including:
- Concurrent extract requests get batched together
- Large extract requests are sub-batched correctly
- Backpressure returns 503 when queue is full

Mark: integration (run with `mise run test -- -i packages/sie_server/tests/api/test_extract_integration.py`)
"""

from __future__ import annotations

import importlib.util
import time
from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from sie_sdk import SIEClient

# GLiNER models require the gliner bundle (transformers<4.52), which conflicts
# with the default bundle (transformers>=4.57). Skip when gliner isn't installed.
# TODO(#185): re-enable when CI has a gliner-bundle integration test job.
pytestmark = pytest.mark.skipif(
    importlib.util.find_spec("gliner") is None,
    reason="GLiNER models need dedicated bundle (transformers<4.52 vs >=4.57)",
)

EXTRACT_MODEL = "NeuML/gliner-bert-tiny"
EXTRACT_FUTURE_TIMEOUT_S = 180


@pytest.mark.integration
class TestExtractConcurrentBatching:
    """Tests for concurrent extract request batching."""

    def test_concurrent_extract_requests_complete(self, sie_client: SIEClient) -> None:
        """Multiple concurrent extract requests all complete successfully."""
        from concurrent.futures import ThreadPoolExecutor

        from sie_sdk.types import Item

        labels = ["person", "organization"]
        texts = [
            "Apple was founded by Steve Jobs.",
            "Google was founded by Larry Page and Sergey Brin.",
        ]

        def extract_single(text: str) -> dict:
            """Extract entities from a single text."""
            result = sie_client.extract(EXTRACT_MODEL, [Item(text=text)], labels=labels)
            return result[0]

        # Run 2 concurrent extractions
        with ThreadPoolExecutor(2) as executor:
            futures = [executor.submit(extract_single, text) for text in texts]
            results = [f.result(timeout=EXTRACT_FUTURE_TIMEOUT_S) for f in futures]

        # All requests completed successfully
        assert len(results) == 2
        for i, result in enumerate(results):
            assert "entities" in result, f"Result {i} missing 'entities'"
            # Should have found at least one entity per text
            assert len(result["entities"]) >= 0  # May vary by text

    def test_concurrent_extract_throughput_benefit(self, sie_client: SIEClient) -> None:
        """Concurrent requests have better throughput than sequential.

        This verifies batching is working by comparing timing.
        """
        from concurrent.futures import ThreadPoolExecutor

        from sie_sdk.types import Item

        labels = ["person", "organization"]
        texts = [f"Sample text number {i} with some entity." for i in range(3)]

        def extract_single(text: str) -> dict:
            result = sie_client.extract(EXTRACT_MODEL, [Item(text=text)], labels=labels)
            return result[0]

        # Measure sequential execution time
        start = time.perf_counter()
        sequential_results = []
        for text in texts[:2]:  # Only 2 for sequential
            sequential_results.append(extract_single(text))
        sequential_time = time.perf_counter() - start

        # Measure concurrent execution time
        start = time.perf_counter()
        with ThreadPoolExecutor(max_workers=3) as executor:
            futures = [executor.submit(extract_single, text) for text in texts]
            concurrent_results = [f.result(timeout=EXTRACT_FUTURE_TIMEOUT_S) for f in futures]
        concurrent_time = time.perf_counter() - start

        # All results valid
        assert len(sequential_results) == 2
        assert len(concurrent_results) == 3

        # Concurrent should process more items in similar or less time
        # If batching works, 3 concurrent should take less than 2x the time of 2 sequential
        assert concurrent_time < sequential_time * 3, (
            f"Concurrent ({concurrent_time:.2f}s for 3 items) "
            f"took more than 3x sequential ({sequential_time:.2f}s for 2 items)"
        )


@pytest.mark.integration
class TestExtractLargeBatch:
    """Tests for large extract request handling."""

    def test_large_batch_completes(self, sie_client: SIEClient) -> None:
        """A large batch request completes successfully."""
        from sie_sdk.types import Item

        labels = ["person", "organization", "location"]

        # Create a batch (10 items — kept small for CI CPU runners)
        items = [Item(text=f"Text number {i} mentions some entity.") for i in range(10)]

        # Should complete without error
        results = sie_client.extract(EXTRACT_MODEL, items, labels=labels)

        assert len(results) == 10
        for i, result in enumerate(results):
            assert "entities" in result, f"Result {i} missing 'entities'"


@pytest.mark.integration
class TestExtractBackpressure:
    """Tests for extract endpoint backpressure (503 responses).

    Note: This test is harder to trigger reliably because it requires
    overwhelming the server's queue. We test the mechanism indirectly.
    """

    def test_many_concurrent_requests_dont_crash(self, sie_client: SIEClient) -> None:
        """Many concurrent requests are handled gracefully (no crashes).

        Even if some requests get 503, the server should remain stable.
        """
        from concurrent.futures import ThreadPoolExecutor, as_completed

        from sie_sdk.types import Item

        labels = ["entity"]
        num_requests = 5

        def extract_single(idx: int) -> tuple[int, bool, str | None]:
            """Extract and return (idx, success, error_msg)."""
            try:
                sie_client.extract(
                    EXTRACT_MODEL,
                    [Item(text=f"Request {idx}: Some text to process.")],
                    labels=labels,
                )
                return (idx, True, None)
            except Exception as e:  # noqa: BLE001 — load test error collection
                return (idx, False, str(e))

        # Fire off many concurrent requests
        with ThreadPoolExecutor(max_workers=num_requests) as executor:
            futures = [executor.submit(extract_single, i) for i in range(num_requests)]
            results = [f.result(timeout=EXTRACT_FUTURE_TIMEOUT_S) for f in as_completed(futures)]

        # Count successes and failures
        successes = sum(1 for _, success, _ in results if success)
        failures = sum(1 for _, success, _ in results if not success)

        # Most requests should succeed (some 503s are acceptable under load)
        assert successes > num_requests * 0.5, f"Too many failures: {failures}/{num_requests}"

        # If there were failures, they should be 503-related or timeout, not crashes
        for idx, success, error in results:
            if not success:
                assert error is not None
                # Allow 503 (queue full), timeout, or connection errors under load
                valid_errors = ["503", "queue", "timeout", "connect", "reset"]
                is_valid_error = any(e.lower() in error.lower() for e in valid_errors)
                assert is_valid_error, f"Unexpected error for request {idx}: {error}"


@pytest.mark.integration
class TestExtractTimingHeaders:
    """Tests for extract timing header responses."""

    def test_extract_returns_timing_header(self, sie_client: SIEClient) -> None:
        """Extract response includes timing information."""
        import httpx
        import msgpack

        # Make a raw request to check headers
        url = f"{sie_client._base_url}/v1/extract/{EXTRACT_MODEL}"
        request_data = {
            "items": [{"text": "Apple Inc. was founded by Steve Jobs."}],
            "params": {"labels": ["person", "organization"]},
        }

        response = httpx.post(
            url,
            content=msgpack.packb(request_data),
            headers={"Content-Type": "application/msgpack"},
            timeout=30.0,
        )

        assert response.status_code == 200

        # Check for timing header
        assert True, "Timing header expected (but may be in body for msgpack)"

        # Parse response body for timing
        data = msgpack.unpackb(response.content, raw=False)
        # Timing may be in response body
        if "timing" in data:
            timing = data["timing"]
            assert "total_ms" in timing or "inference_ms" in timing