inference_capacity_acceptance.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import asyncio
  2. import json
  3. from collections import Counter
  4. from pathlib import Path
  5. from time import perf_counter
  6. import httpx
  7. from prometheus_client.parser import text_string_to_metric_families
  8. async def test_http_overload_and_recovery():
  9. async with httpx.AsyncClient(
  10. base_url="http://127.0.0.1:8200",
  11. timeout=30,
  12. trust_env=False,
  13. limits=httpx.Limits(max_connections=320, max_keepalive_connections=32),
  14. ) as client:
  15. start = perf_counter()
  16. responses = await asyncio.gather(
  17. *(
  18. client.post("/embeddings", json={"input": [f"查询第 {index} 个包裹的配送信息"]})
  19. for index in range(320)
  20. )
  21. )
  22. counts = Counter(response.status_code for response in responses)
  23. duration = perf_counter() - start
  24. assert set(counts) <= {200, 429}, counts
  25. assert counts[200] > 0 and counts[429] > 0, counts
  26. for response in responses:
  27. if response.status_code == 200:
  28. assert len(response.json()["vectors"][0]) == 1024
  29. recovery = await client.post("/embeddings", json={"input": ["查询物流"]})
  30. assert recovery.status_code == 200, recovery.text
  31. metrics = await client.get("/metrics")
  32. samples = [
  33. sample
  34. for family in text_string_to_metric_families(metrics.text)
  35. for sample in family.samples
  36. ]
  37. assert any(
  38. sample.name == "zhifu_embedding_queue_texts" and sample.value == 0 for sample in samples
  39. )
  40. await asyncio.to_thread(
  41. Path("docs/verification/inference-overload.json").write_text,
  42. json.dumps(
  43. {
  44. "requests": 320,
  45. "statuses": dict(counts),
  46. "seconds": duration,
  47. "recovery_status": recovery.status_code,
  48. "queue_after": 0,
  49. },
  50. indent=2,
  51. ),
  52. encoding="utf-8",
  53. )