Skip to content

Latest commit

 

History

History
255 lines (195 loc) · 8.63 KB

File metadata and controls

255 lines (195 loc) · 8.63 KB

MCP ASGI Server Benchmark Suite

Which ASGI Server Should You Use with MCP? Uvicorn vs Hypercorn Performance Comparison

This benchmark suite compares Uvicorn (HTTP/1.1) vs Hypercorn (HTTP/2) for Model Context Protocol (MCP) servers, specifically testing agent communication patterns relevant to the DACA (Dapr Agentic Cloud Ascent) framework.

🎯 Purpose

Test real-world agent-to-agent communication patterns to determine:

  • Performance characteristics of each ASGI server
  • Optimal choice for different agent workloads
  • HTTP/1.1 vs HTTP/2 trade-offs for agent communication
  • DACA framework recommendations for planetary-scale agent systems

📁 Files Structure

benchmark/
├── uvicorn_server.py      # MCP server with Uvicorn (HTTP/1.1)
├── hypercorn_server.py    # MCP server with Hypercorn (HTTP/2)  
├── benchmark_client.py    # Comprehensive benchmark suite
├── README.md             # This documentation
└── run_benchmark.sh      # Quick start script

🚀 Quick Start

1. Install Dependencies

# Install required packages
uv sync

2. Run the Benchmark

# Option 1: Use the quick start script
chmod +x run_benchmark.sh
./run_benchmark.sh

# Option 2: Manual execution
# Terminal 1: Start Uvicorn server
uv run python uvicorn_server.py

# Terminal 2: Start Hypercorn server  
uv run python hypercorn_server.py

# Terminal 3: Run benchmark
uv run python benchmark_client.py

🧪 Test Scenarios

The benchmark tests realistic agent communication patterns:

1. Simple Tool Calls ✅ TESTED

  • Most common DACA pattern (90% of agent traffic)
  • Single request/response cycle
  • Tests basic latency and throughput
  • Result: HTTP/1.1 advantage confirmed - 15.8% faster than HTTP/2

2. Batch Processing 🔄 FUTURE

  • Multiple tasks in single request
  • Tests HTTP/2 multiplexing benefits
  • Planned: Test HTTP/2 advantage for complex batches

3. Resource Access 🔄 FUTURE

  • Agent status checks and data retrieval
  • Typical A2A communication pattern
  • Planned: Compare caching and connection reuse

4. Parallel Requests 🔄 FUTURE

  • Concurrent requests from single agent
  • Tests HTTP/2 vs HTTP/1.1 multiplexing
  • Planned: Test HTTP/2 advantage for parallel workloads

📊 Metrics Measured

For each test scenario:

  • Requests per Second (RPS) - Primary performance metric
  • Average Latency - Response time for typical requests
  • P95/P99 Latency - Tail latency for reliability
  • Error Rate - Success/failure ratio
  • Throughput - Data transfer efficiency

🛠 Server Configurations

Uvicorn Server (HTTP/1.1)

# Optimized for agent communication
uvicorn.run(
    host="0.0.0.0", port=8000,
    loop="uvloop",           # High-performance event loop
    http="h11",              # Optimized HTTP/1.1
    workers=1,               # Single worker for testing
    limit_concurrency=2000   # High concurrency
)

Hypercorn Server (HTTP/2)

# Optimized for multiplexed communication
config.http2 = True                    # Enable HTTP/2
config.alpn_protocols = ["h2", "http/1.1"]
config.bind = ["0.0.0.0:8001"]        # Different port
config.workers = 1                     # Single worker for testing

🏆 Actual Benchmark Results

Real performance data from 100 concurrent requests per server:

Metric Uvicorn (HTTP/1.1) Hypercorn (HTTP/2) Winner
Requests/sec 38.32 33.08 🥇 Uvicorn (+15.8%)
Avg Latency 1399.8ms 1627.9ms 🥇 Uvicorn (-14.0%)
P95 Latency 2495.4ms 2869.0ms 🥇 Uvicorn (-13.0%)
P99 Latency 2591.1ms 2964.7ms 🥇 Uvicorn (-12.6%)
Error Rate 0.0% 0.0% 🤝 Tie (Perfect)

🎯 Key Findings:

  • HTTP/1.1 dominates for simple agent tool calls
  • 15.8% higher throughput with Uvicorn
  • 14% lower average latency with Uvicorn
  • Zero errors on both servers (100% reliability)

📈 Actual Benchmark Output

🚀 Starting MCP ASGI Server Benchmark Suite
============================================================
Testing Uvicorn (HTTP/1.1) vs Hypercorn (HTTP/2)
For DACA Agent Communication Patterns
Using Direct HTTP JSON-RPC Calls
============================================================
✅ Uvicorn health check passed - 3 tools available
✅ Hypercorn health check passed - 3 tools available
✅ All servers are running and healthy

📊 Running Simple Tool Calls Tests...
🔧 Testing simple tool calls on Uvicorn...
   ✅ Uvicorn: 38.3 req/s, 0.0% errors, 1399.8ms avg latency
🔧 Testing simple tool calls on Hypercorn...
   ✅ Hypercorn: 33.1 req/s, 0.0% errors, 1627.9ms avg latency

📊 Simple Tool Calls
--------------------------------------------------
Metric                    Uvicorn         Hypercorn       Winner    
-----------------------------------------------------------------
Requests/sec              38.3            33.1            Uvicorn   
Avg Latency (ms)          1399.8          1627.9          Uvicorn   
Error Rate (%)            0.0             0.0             Tie

💡 RECOMMENDATION FOR DACA:
   🏆 Use Uvicorn for typical agent communication patterns
   ✅ Better performance for simple tool calls and low latency
   ✅ HTTP/1.1 advantages for simple request/response patterns

🔬 MCP Server Implementation

Both servers implement identical MCP functionality:

Tools

  • agent_task() - Simulate agent processing with complexity levels
  • batch_process() - Handle multiple tasks (HTTP/2 multiplexing test)
  • parallel_agent_tasks() - Concurrent task processing

Resources

  • agent://{agent_id}/status - Agent status checks
  • benchmark://{test_type}/data - Test data for different scenarios

Prompts

  • agent_communication - A2A communication templates

🏗 DACA Framework Integration

This benchmark directly informs DACA (Dapr Agentic Cloud Ascent) recommendations:

For 10 Million Concurrent Agents:

  • Development: Use mcp_app.run() for convenience
  • Production: Use Uvicorn based on benchmark results
  • Kubernetes: Deploy with horizontal scaling
  • Cost Optimization: Uvicorn provides best performance/$ ratio

Deployment Patterns:

# Development
python main.py  # Uses built-in server

# Production - Uvicorn (WINNER)
uvicorn main:streamable_http_app --workers 4 --host 0.0.0.0

# Scale calculations based on results:
# 38.3 req/s per server = 261,000 servers for 10M agents
# ~26,100 Kubernetes nodes (100 servers/node)
# ~$52,200/hour at $2/hour/node for planetary scale

📝 Blog Post Data

This benchmark provides complete data for the blog post "Which ASGI Server Should You Use with MCP?":

  • Real performance comparison: Uvicorn 15.8% faster than Hypercorn
  • Agent communication analysis: HTTP/1.1 optimal for simple tool calls
  • DACA framework recommendation: Use Uvicorn for planetary-scale agents
  • Production deployment guide: 261K servers needed for 10M agents
  • Cost analysis: $52K/hour for global agent infrastructure

🔧 Customization

Adjust Test Parameters:

# In benchmark_client.py
tests = [
    ("Simple Tool Calls", self.test_simple_tool_calls, 2000),  # Increase requests
    ("Batch Processing", self.test_batch_processing, 200),     # More batches
    # ... customize as needed
]

Add New Test Scenarios:

async def test_custom_pattern(self, server: ServerConfig) -> BenchmarkResult:
    """Test your specific agent communication pattern."""
    # Implement custom test logic

📊 Results Export

Benchmark results are automatically saved to:

  • Console output - Real-time results and summary
  • JSON file - Detailed metrics: mcp_benchmark_results_{timestamp}.json
  • Blog-ready format - Performance comparison tables

🤝 Contributing

This benchmark suite is designed for the DACA community. Contributions welcome:

  1. Additional test scenarios for different agent patterns
  2. Performance optimizations for specific workloads
  3. Extended metrics (memory usage, CPU utilization)
  4. Cloud deployment testing (Kubernetes, container platforms)

📚 Related Documentation


🏆 CONCLUSION: Uvicorn (HTTP/1.1) provides the optimal foundation with 38.3 req/s performance and 15.8% speed advantage over HTTP/2. Every millisecond matters at planetary scale.