Skip to content

Sample MCP Server - Rust (performance-benchmark-server) #902

@crivetimihai

Description

@crivetimihai

Overview

Create a sample MCP Server in Rust that provides comprehensive performance benchmarking and profiling capabilities for applications, systems, and network resources.

Server Specifications

Server Details

  • Name: performance-benchmark-server
  • Language: Rust 1.75+
  • Location: mcp-servers/rust/performance-benchmark-server/
  • Purpose: Demonstrate high-performance benchmarking and profiling via MCP

Core Features

  • CPU and memory benchmarking
  • Network performance testing
  • I/O throughput measurement
  • Concurrent load testing
  • Statistical analysis of results
  • Cross-platform performance metrics

Tools Provided

1. cpu_benchmark

CPU performance testing with various workloads

#[derive(Deserialize)]
struct CpuBenchmarkRequest {
    test_type: String,      // arithmetic, memory_bound, cache_intensive, prime_sieve
    duration_secs: u64,     // Test duration
    thread_count: Option<usize>, // Number of threads (default: CPU cores)
    workload_size: Option<usize>, // Problem size for scalable tests
    iterations: Option<u64>, // For deterministic tests
}

2. memory_benchmark

Memory performance and allocation testing

#[derive(Deserialize)]
struct MemoryBenchmarkRequest {
    test_type: String,      // sequential_read, random_access, allocation_speed
    memory_size: String,    // "1MB", "100MB", "1GB" 
    access_pattern: String, // sequential, random, stride
    thread_count: Option<usize>,
    iterations: u64,
}

3. network_benchmark

Network performance testing

#[derive(Deserialize)]
struct NetworkBenchmarkRequest {
    test_type: String,      // latency, throughput, concurrent_connections
    target_host: String,    // Target server
    port: u16,
    protocol: String,       // tcp, udp, http, https
    concurrent_connections: usize,
    data_size: String,      // Per-request data size
    duration_secs: u64,
}

4. io_benchmark

File system and disk I/O performance testing

#[derive(Deserialize)]
struct IoBenchmarkRequest {
    test_type: String,      // sequential_write, random_read, mixed_workload
    file_path: String,      // Test file path
    file_size: String,      // Total file size for test
    block_size: String,     // I/O block size (4KB, 64KB, 1MB)
    sync_mode: String,      // sync, async, direct
    thread_count: Option<usize>,
}

5. load_test

Comprehensive load testing with configurable scenarios

#[derive(Deserialize)]
struct LoadTestRequest {
    target_url: String,
    request_method: String, // GET, POST, PUT, DELETE
    headers: HashMap<String, String>,
    body: Option<String>,
    concurrent_users: usize,
    requests_per_second: Option<f64>,
    duration_secs: u64,
    ramp_up_secs: Option<u64>,
}

6. profile_application

Application profiling and performance analysis

#[derive(Deserialize)]
struct ProfileRequest {
    command: String,        // Command to profile
    args: Vec<String>,      // Command arguments
    profile_type: String,   // cpu, memory, io, all
    duration_secs: Option<u64>,
    sampling_freq: Option<u32>, // Sampling frequency in Hz
    output_format: String,  // json, flamegraph, callgrind
}

7. system_stress

System stress testing and stability analysis

#[derive(Deserialize)]
struct StressTestRequest {
    stress_type: String,    // cpu, memory, io, network, all
    intensity: f64,         // Stress intensity (0.0-1.0)
    duration_secs: u64,
    monitor_metrics: bool,
    target_resources: Option<Vec<String>>,
}

8. compare_results

Statistical comparison of benchmark results

#[derive(Deserialize)]
struct CompareRequest {
    baseline_results: String, // JSON results from previous benchmark
    current_results: String,  // JSON results to compare against
    metrics: Vec<String>,     // Metrics to compare
    significance_level: f64,  // Statistical significance threshold
}

Implementation Requirements

Directory Structure

mcp-servers/rust/performance-benchmark-server/
├── src/
│   ├── main.rs
│   ├── lib.rs
│   ├── benchmarks/
│   │   ├── mod.rs
│   │   ├── cpu.rs
│   │   ├── memory.rs
│   │   ├── network.rs
│   │   ├── io.rs
│   │   └── load_test.rs
│   ├── profiling/
│   │   ├── mod.rs
│   │   ├── cpu_profiler.rs
│   │   ├── memory_profiler.rs
│   │   └── flamegraph.rs
│   ├── metrics/
│   │   ├── mod.rs
│   │   ├── collector.rs
│   │   └── statistics.rs
│   ├── utils/
│   │   ├── mod.rs
│   │   ├── system_info.rs
│   │   └── results.rs
│   └── config.rs
├── Cargo.toml
├── README.md
├── tests/
├── benchmarks/           # Built-in benchmark suite
│   ├── cpu_tests.rs
│   ├── memory_tests.rs
│   └── integration.rs
└── examples/
    ├── basic_benchmarks.rs
    ├── load_testing.rs
    └── profiling_example.rs

Dependencies

# Cargo.toml
[package]
name = "performance-benchmark-server"
version = "0.1.0"
edition = "2021"

[dependencies]
mcp = { path = "../mcp-rust" }
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

# Performance and benchmarking
criterion = "0.5"
sysinfo = "0.30"
heim = "0.1"
perf-event = "0.4"

# Networking
reqwest = { version = "0.11", features = ["json"] }
tokio-tungstenite = "0.20"

# I/O and file system
tempfile = "3.8"
memmap2 = "0.9"

# Statistics and analysis
statistical = "1.0"
plotters = "0.3"

# System interaction
nix = "0.27"
libc = "0.2"

# Utilities
rayon = "1.8"
crossbeam = "0.8"
dashmap = "5.5"
uuid = "1.6"
chrono = { version = "0.4", features = ["serde"] }

[dev-dependencies]
proptest = "1.4"

Configuration

# config.toml
[benchmark]
default_duration = 10      # seconds
max_duration = 300        # seconds  
max_threads = 32
max_memory_size = "8GB"
temp_dir = "./benchmark_temp"

[network]
timeout = 30              # seconds
max_concurrent = 1000
allowed_hosts = ["localhost", "127.0.0.1", "::1"]
default_port_range = [8000, 9000]

[profiling]
sampling_frequency = 1000 # Hz
max_profile_duration = 300 # seconds
output_directory = "./profiles"
enable_flamegraphs = true

[system]
monitor_interval = 100    # milliseconds
enable_hardware_counters = true
collect_system_metrics = true

Usage Examples

CPU Benchmarking

# CPU arithmetic benchmark
echo '{
  "method": "tools/call",
  "params": {
    "name": "cpu_benchmark",
    "arguments": {
      "test_type": "arithmetic",
      "duration_secs": 30,
      "thread_count": 8
    }
  }
}' | performance-benchmark-server

Memory Performance Testing

# Memory allocation benchmark
echo '{
  "method": "tools/call", 
  "params": {
    "name": "memory_benchmark",
    "arguments": {
      "test_type": "allocation_speed",
      "memory_size": "100MB",
      "access_pattern": "sequential",
      "iterations": 1000
    }
  }
}' | performance-benchmark-server

Network Load Testing

# HTTP load test
echo '{
  "method": "tools/call",
  "params": {
    "name": "load_test",
    "arguments": {
      "target_url": "http://localhost:8080/api/test",
      "request_method": "GET",
      "concurrent_users": 100,
      "requests_per_second": 500,
      "duration_secs": 60,
      "ramp_up_secs": 10
    }
  }
}' | performance-benchmark-server

Application Profiling

# Profile a Rust application
echo '{
  "method": "tools/call",
  "params": {
    "name": "profile_application", 
    "arguments": {
      "command": "./target/release/my_app",
      "args": ["--mode", "production"],
      "profile_type": "cpu",
      "duration_secs": 60,
      "output_format": "flamegraph"
    }
  }
}' | performance-benchmark-server

Advanced Features

  • Hardware Counters: CPU performance counters (cache misses, branch predictions)
  • NUMA Awareness: Multi-socket system optimization
  • Container Metrics: Docker and Kubernetes performance analysis
  • Continuous Monitoring: Long-term performance tracking
  • Regression Detection: Automatic performance regression detection
  • Custom Workloads: User-defined benchmark scenarios

Statistical Analysis

  • Percentile Analysis: P50, P95, P99 latency measurements
  • Confidence Intervals: Statistical significance testing
  • Outlier Detection: Anomaly identification in results
  • Trend Analysis: Performance trend visualization
  • Correlation Analysis: Multi-metric relationship analysis

Performance Optimizations

  • Zero-Copy Operations: Minimize memory allocations
  • SIMD Instructions: Vectorized benchmark operations
  • Lock-Free Data Structures: High-concurrency measurements
  • Memory Pool: Efficient memory management
  • Async I/O: Non-blocking performance testing

Results Format

#[derive(Serialize)]
struct BenchmarkResult {
    test_name: String,
    timestamp: DateTime<Utc>,
    system_info: SystemInfo,
    configuration: TestConfiguration,
    metrics: BenchmarkMetrics,
    statistics: StatisticalSummary,
    raw_data: Option<Vec<Measurement>>,
}

#[derive(Serialize)]
struct BenchmarkMetrics {
    throughput: Option<f64>,    // ops/sec or MB/sec
    latency: Option<LatencyStats>,
    cpu_usage: Option<f64>,     // percentage
    memory_usage: Option<u64>,  // bytes
    error_rate: Option<f64>,    // percentage
}

Testing Requirements

  • Unit tests for all benchmark algorithms
  • Integration tests with real workloads
  • Performance regression tests
  • Cross-platform compatibility testing
  • Stress testing of the benchmarking system itself
  • Accuracy verification against known baselines

Acceptance Criteria

  • Rust MCP server with 8+ performance testing tools
  • CPU, memory, network, and I/O benchmarking capabilities
  • Application profiling with flamegraph generation
  • Load testing with concurrent user simulation
  • Statistical analysis and comparison features
  • System stress testing and monitoring
  • Cross-platform support (Linux, macOS, Windows)
  • Comprehensive test suite with performance validation (>90% coverage)
  • Optimized implementation with minimal overhead
  • Complete documentation with benchmarking best practices

Priority

Medium - Demonstrates performance-critical applications and Rust's zero-cost abstractions

Use Cases

  • Application performance optimization
  • System capacity planning
  • Performance regression detection
  • Load testing and scalability analysis
  • Hardware benchmarking and comparison
  • Development environment performance validation
  • CI/CD performance testing integration

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmcp-serversMCP Server SamplesoicOpen Innovation Community ContributionsrustRust programming

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions