RAG-DEMO/tests/performance/test_vector_query_load.py

65 lines
2.1 KiB
Python

from __future__ import annotations
import os
import time
import pytest
from graphrag_platform.config import Settings
from graphrag_platform.models import SearchItem
from graphrag_platform.runtime import StageContext
from graphrag_platform.stage_loader import load_stage
from conftest import FakeLLM
POSTGRES_URL = os.getenv("TEST_POSTGRES_URL")
RUN_PERFORMANCE_TESTS = os.getenv("RUN_PERFORMANCE_TESTS") == "1"
@pytest.mark.performance
@pytest.mark.skipif(
not POSTGRES_URL or not RUN_PERFORMANCE_TESTS,
reason="Set TEST_POSTGRES_URL and RUN_PERFORMANCE_TESTS=1",
)
def test_vector_query_latency_at_configured_load() -> None:
item_count = int(os.getenv("PERF_VECTOR_ITEM_COUNT", "10000"))
maximum_seconds = float(os.getenv("PERF_MAX_QUERY_SECONDS", "2.0"))
settings = Settings(
_env_file=None,
database_url=POSTGRES_URL,
corpus_name="performance-corpus",
llm_embedding_dimensions=3,
retrieval_top_k=10,
)
context = StageContext.create(settings, llm=FakeLLM())
context.initialize()
with context.storage.session() as session:
session.query(SearchItem).filter_by(corpus=settings.corpus_name).delete()
session.add_all(
SearchItem(
corpus=settings.corpus_name,
item_type="benchmark",
item_key=str(index),
text=f"Benchmark graph item {index}",
source_url=None,
metadata_json={},
embedding=[
float(index % 2),
float((index // 2) % 2),
(index % 255) / 255,
],
embedding_model=FakeLLM.embedding_model,
)
for index in range(item_count)
)
context.storage.ensure_vector_index(3)
started = time.perf_counter()
result = load_stage(9).run(context, question="Atlas graph retrieval", top_k=10)
elapsed = time.perf_counter() - started
assert len(result["context"]) == 10
assert elapsed <= maximum_seconds, (
f"vector query took {elapsed:.3f}s; threshold is {maximum_seconds:.3f}s"
)