|
| 1 | +"""Verify that multiple Runloop (sync) instances share a single connection pool. |
| 2 | +
|
| 3 | +Spins up N SDK instances and checks that the number of open connections to |
| 4 | +api.runloop.ai does not grow linearly with instance count — it should stay at |
| 5 | +one (or a small fixed number) because all instances share the same underlying |
| 6 | +httpx transport. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + uv run python loadtest/pool_check.py |
| 10 | +
|
| 11 | +No API key is required — we only check the transport object identity and the |
| 12 | +OS-level connection count, not make real requests. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import resource |
| 20 | + |
| 21 | +from runloop_api_client import Runloop |
| 22 | + |
| 23 | +HOST = "api.runloop.ai" |
| 24 | +N = 20 |
| 25 | + |
| 26 | + |
| 27 | +def main() -> None: |
| 28 | + fd_before = resource.getrlimit(resource.RLIMIT_NOFILE)[0] |
| 29 | + print(f"FD limit: {fd_before}") |
| 30 | + print(f"Creating {N} Runloop instances...\n") |
| 31 | + |
| 32 | + clients: list[Runloop] = [] |
| 33 | + for _ in range(N): |
| 34 | + clients.append(Runloop(bearer_token=os.environ.get("RUNLOOP_API_KEY", "dummy"))) |
| 35 | + |
| 36 | + # All instances should reference the same shared transport object. |
| 37 | + transport_ids: set[int] = set() |
| 38 | + for c in clients: |
| 39 | + t = getattr(c._client, "_transport", None) |
| 40 | + if t is not None: |
| 41 | + transport_ids.add(id(t)) |
| 42 | + |
| 43 | + print(f"Distinct transport objects across {N} instances: {len(transport_ids)}") |
| 44 | + if len(transport_ids) == 1: |
| 45 | + print("PASS — all instances share one transport (connection pool).") |
| 46 | + else: |
| 47 | + print("FAIL — instances have separate transports; FD exhaustion is possible.") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + # Close all clients. |
| 51 | + for c in clients: |
| 52 | + c.close() |
| 53 | + |
| 54 | + print("\nDone.") |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + main() |
0 commit comments