Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287"""Performance tests for OpenAPIProvider implementation."""
import gcimport time
import httpximport pytest
from fastmcp import FastMCPfrom fastmcp.server.providers.openapi import OpenAPIProvider
def create_openapi_server( openapi_spec: dict, client, name: str = "OpenAPI Server",) -> FastMCP: """Helper to create a FastMCP server with OpenAPIProvider.""" provider = OpenAPIProvider(openapi_spec=openapi_spec, client=client) mcp = FastMCP(name) mcp.add_provider(provider) return mcp
class TestPerformance: """Test performance of OpenAPIProvider implementation."""
@pytest.fixture def comprehensive_spec(self): """Comprehensive OpenAPI spec for performance testing.""" return { "openapi": "3.0.0", "info": {"title": "Performance Test API", "version": "1.0.0"}, "paths": { "/users": { "get": { "operationId": "list_users", "summary": "List users", "parameters": [ { "name": "limit", "in": "query", "required": False, "schema": {"type": "integer", "default": 10}, }, { "name": "offset", "in": "query", "required": False, "schema": {"type": "integer", "default": 0}, }, ], "responses": {"200": {"description": "Users listed"}}, }, "post": { "operationId": "create_user", "summary": "Create user", "requestBody": { "required": True, "content": { "application/json": { "schema": { "type": "object", "properties": { "name": {"type": "string"}, "email": {"type": "string"}, "age": {"type": "integer"}, }, "required": ["name", "email"], } } }, }, "responses": {"201": {"description": "User created"}}, }, }, "/users/{id}": { "get": { "operationId": "get_user", "summary": "Get user", "parameters": [ { "name": "id", "in": "path", "required": True, "schema": {"type": "integer"}, } ], "responses": {"200": {"description": "User found"}}, }, "put": { "operationId": "update_user", "summary": "Update user", "parameters": [ { "name": "id", "in": "path", "required": True, "schema": {"type": "integer"}, } ], "requestBody": { "required": True, "content": { "application/json": { "schema": { "type": "object", "properties": { "name": {"type": "string"}, "email": {"type": "string"}, "age": {"type": "integer"}, }, } } }, }, "responses": {"200": {"description": "User updated"}}, }, "delete": { "operationId": "delete_user", "summary": "Delete user", "parameters": [ { "name": "id", "in": "path", "required": True, "schema": {"type": "integer"}, } ], "responses": {"204": {"description": "User deleted"}}, }, }, "/search": { "get": { "operationId": "search_users", "summary": "Search users", "parameters": [ { "name": "q", "in": "query", "required": True, "schema": {"type": "string"}, }, { "name": "filters", "in": "query", "required": False, "style": "deepObject", "explode": True, "schema": { "type": "object", "properties": { "age_min": {"type": "integer"}, "age_max": {"type": "integer"}, "status": { "type": "string", "enum": ["active", "inactive"], }, }, }, }, ], "responses": {"200": {"description": "Search results"}}, } }, }, }
def test_provider_initialization_performance(self, comprehensive_spec): """Test that provider initialization is fast (serverless requirement).""" num_iterations = 5
# Measure provider initialization times = [] for _ in range(num_iterations): client = httpx.AsyncClient(base_url="https://api.example.com") start_time = time.time() provider = OpenAPIProvider( openapi_spec=comprehensive_spec, client=client, ) # Ensure provider is fully initialized assert provider is not None end_time = time.time() times.append(end_time - start_time)
avg_time = sum(times) / len(times) max_acceptable_time = 0.1 # 100ms
print(f"Average initialization time: {avg_time:.4f}s") print(f"Performance: {'✓' if avg_time < max_acceptable_time else '✗'}")
# Should initialize in under 100ms for serverless requirements assert avg_time < max_acceptable_time, ( f"Provider should initialize in under 100ms, got {avg_time:.4f}s" )
def test_server_initialization_performance(self, comprehensive_spec): """Test that full server initialization is fast.""" num_iterations = 5
times = [] for _ in range(num_iterations): client = httpx.AsyncClient(base_url="https://api.example.com") start_time = time.time() server = create_openapi_server( openapi_spec=comprehensive_spec, client=client, name="Performance Test", ) # Ensure server is fully initialized assert server is not None end_time = time.time() times.append(end_time - start_time)
avg_time = sum(times) / len(times) max_acceptable_time = 0.1 # 100ms
print(f"Average server initialization time: {avg_time:.4f}s")
assert avg_time < max_acceptable_time, ( f"Server should initialize in under 100ms, got {avg_time:.4f}s" )
async def test_functionality_after_optimization(self, comprehensive_spec): """Verify that performance optimization doesn't break functionality.""" client = httpx.AsyncClient(base_url="https://api.example.com")
server = create_openapi_server( openapi_spec=comprehensive_spec, client=client, name="Test Server", )
# Get tools from the server via public API tools = await server.list_tools()
# Should have 6 operations in the spec assert len(tools) == 6
# Expected operations expected_operations = { "list_users", "create_user", "get_user", "update_user", "delete_user", "search_users", } assert {t.name for t in tools} == expected_operations
def test_memory_efficiency(self, comprehensive_spec): """Test that implementation doesn't significantly increase memory usage."""
# Helper to count total tools across all providers def count_provider_tools(server): total = 0 for provider in server.providers: if hasattr(provider, "_tools"): total += len(provider._tools) return total
gc.collect() # Clean up before baseline baseline_refs = len(gc.get_objects())
servers = [] for i in range(10): client = httpx.AsyncClient(base_url="https://api.example.com") server = create_openapi_server( openapi_spec=comprehensive_spec, client=client, name=f"Memory Test Server {i}", ) servers.append(server)
# Servers should all be functional assert len(servers) == 10 assert all(count_provider_tools(s) == 6 for s in servers)
# Memory usage shouldn't explode gc.collect() current_refs = len(gc.get_objects()) growth_ratio = current_refs / max(baseline_refs, 1) assert growth_ratio < 3.0, ( f"Memory usage grew by {growth_ratio}x, which seems excessive" )