|
| 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 resource |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | + |
| 22 | +from runloop_api_client import Runloop |
| 23 | + |
| 24 | +HOST = "api.runloop.ai" |
| 25 | +N = 20 |
| 26 | + |
| 27 | + |
| 28 | +def open_connections_to(host: str) -> int: |
| 29 | + """Count ESTABLISHED TCP connections to *host* via lsof.""" |
| 30 | + try: |
| 31 | + out = subprocess.check_output( |
| 32 | + ["lsof", "-i", f"@{host}", "-sTCP:ESTABLISHED", "-n", "-P"], |
| 33 | + stderr=subprocess.DEVNULL, |
| 34 | + text=True, |
| 35 | + ) |
| 36 | + return max(0, len(out.strip().splitlines()) - 1) # subtract header |
| 37 | + except Exception: |
| 38 | + return -1 # lsof unavailable |
| 39 | + |
| 40 | + |
| 41 | +def main() -> None: |
| 42 | + fd_before = resource.getrlimit(resource.RLIMIT_NOFILE)[0] |
| 43 | + print(f"FD limit: {fd_before}") |
| 44 | + print(f"Creating {N} Runloop instances...\n") |
| 45 | + |
| 46 | + clients: list[Runloop] = [] |
| 47 | + for i in range(N): |
| 48 | + clients.append(Runloop(bearer_token=os.environ.get("RUNLOOP_API_KEY", "dummy"))) |
| 49 | + |
| 50 | + # All instances should reference the same shared transport object. |
| 51 | + transport_ids = set() |
| 52 | + for c in clients: |
| 53 | + t = getattr(c._client, "_transport", None) |
| 54 | + if t is not None: |
| 55 | + transport_ids.add(id(t)) |
| 56 | + |
| 57 | + print(f"Distinct transport objects across {N} instances: {len(transport_ids)}") |
| 58 | + if len(transport_ids) == 1: |
| 59 | + print("PASS — all instances share one transport (connection pool).") |
| 60 | + else: |
| 61 | + print("FAIL — instances have separate transports; FD exhaustion is possible.") |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + # Close all clients. |
| 65 | + for c in clients: |
| 66 | + c.close() |
| 67 | + |
| 68 | + print("\nDone.") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments