Skip to content

Latest commit

 

History

History
448 lines (358 loc) · 9.78 KB

File metadata and controls

448 lines (358 loc) · 9.78 KB

🚀 Arsenal Lab Performance Monitoring API

Complete API documentation for the real-time performance monitoring endpoints integrated with Bun v1.3.1 optimizations.

📊 API Endpoints Overview

Base URL

http://localhost:3655/api/performance

Authentication

  • No authentication required (local development)
  • All endpoints return JSON responses
  • CORS enabled for dashboard integration

🎯 Performance Metrics API

GET /api/performance/metrics

Returns real-time performance metrics including FPS, memory usage, uptime, and system load.

Response Format

{
  "timestamp": 1642857600000,
  "fps": 144,
  "memory": {
    "used": 17,
    "total": 64,
    "external": 2,
    "heapUsed": 17,
    "heapTotal": 32
  },
  "uptime": 3600,
  "systemLoad": [1.5, 1.2, 1.0],
  "version": "Bun v1.3.1-enhanced",
  "status": "excellent"
}

Usage Examples

cURL:

curl -X GET "http://localhost:3655/api/performance/metrics" \
  -H "Accept: application/json"

JavaScript:

const response = await fetch('/api/performance/metrics');
const metrics = await response.json();
console.log(`FPS: ${metrics.fps}, Memory: ${metrics.memory.used}MB`);

Response Fields:

  • fps: Current frames per second (based on requestAnimationFrame)
  • memory.used: Heap memory used in MB
  • memory.total: Total heap size in MB
  • uptime: Process uptime in seconds
  • systemLoad: System load averages (1min, 5min, 15min) - Unix only
  • status: Performance status (excellent, good, fair, poor)

📈 Performance History API

GET /api/performance/history

Returns historical performance data for trend analysis and charting.

Query Parameters

  • metric (string): memory, fps, or uptime (default: memory)
  • duration (number): Duration in seconds (default: 60)

Response Format

{
  "metric": "memory",
  "duration": 60,
  "dataPoints": 61,
  "history": [
    {
      "timestamp": 1642857600000,
      "value": 17.5,
      "metric": "memory"
    }
  ]
}

Usage Examples

Memory Usage History:

curl "http://localhost:3655/api/performance/history?metric=memory&duration=300"

FPS History (Last 2 minutes):

curl "http://localhost:3655/api/performance/history?metric=fps&duration=120"

JavaScript Integration:

// Get memory history for the last 5 minutes
const response = await fetch('/api/performance/history?metric=memory&duration=300');
const data = await response.json();

// Use for charting
data.history.forEach(point => {
  chart.addPoint(point.timestamp, point.value);
});

🚨 Performance Alerts API

GET /api/performance/alerts

Returns active performance alerts and warnings based on current metrics.

Response Format

{
  "total": 1,
  "alerts": [
    {
      "id": "performance-excellent",
      "type": "info",
      "title": "Excellent Performance",
      "message": "System is running optimally with 17MB memory usage",
      "timestamp": 1642857600000,
      "metric": "performance",
      "value": "excellent",
      "threshold": "good"
    }
  ],
  "timestamp": 1642857600000
}

Alert Types

  • critical: Immediate action required (memory > 200MB)
  • warning: Elevated usage (memory > 100MB)
  • info: Positive performance indicators

Usage Examples

Check for Alerts:

curl "http://localhost:3655/api/performance/alerts"

Dashboard Integration:

const alerts = await fetch('/api/performance/alerts').then(r => r.json());

// Display alerts in UI
alerts.alerts.forEach(alert => {
  showNotification(alert.title, alert.message, alert.type);
});

🏁 Performance Benchmarks API

GET /api/performance/benchmarks

Returns system benchmarks and Bun v1.3.1 optimization details.

Response Format

{
  "timestamp": 1642857600000,
  "system": {
    "platform": "darwin",
    "arch": "x64",
    "nodeVersion": "v18.0.0",
    "bunVersion": "1.3.1"
  },
  "metrics": {
    "fps": 144,
    "memoryUsage": 17,
    "uptime": 3600,
    "status": "excellent"
  },
  "benchmarks": {
    "Bun v1.3.1 Optimizations": {
      "Build Performance": "2x faster isolated linker",
      "Memory Efficiency": "17 MB baseline usage",
      "FPS Performance": "144 FPS sustained",
      "Test Execution": "--only-failures --pass-with-no-tests",
      "Registry Auth": "Native email forwarding"
    }
  }
}

📊 Performance Dashboard API

GET /api/performance/dashboard

Returns aggregated dashboard data for comprehensive monitoring views.

Response Format

{
  "timestamp": 1642857600000,
  "summary": {
    "status": "excellent",
    "uptime": 3600,
    "memoryUsage": 17,
    "fps": 144,
    "activeAlerts": 0,
    "lastUpdate": "2023-01-22T10:00:00.000Z"
  },
  "charts": {
    "fps": {
      "current": 144,
      "average": 142,
      "min": 120,
      "max": 144,
      "trend": "stable"
    },
    "memory": {
      "current": 17,
      "average": 16,
      "min": 12,
      "max": 25,
      "trend": "stable"
    }
  },
  "alerts": [],
  "recommendations": [
    {
      "type": "optimization",
      "title": "Bun v1.3.1 Performance",
      "message": "Your system is running optimally with Bun v1.3.1 enhancements",
      "priority": "low"
    }
  ]
}

🧪 API Testing Scripts

Test All Endpoints

# Run comprehensive API tests
bun run scripts/test-performance-api.ts

Individual Endpoint Tests

# Test metrics endpoint
curl "http://localhost:3655/api/performance/metrics"

# Test history with different metrics
curl "http://localhost:3655/api/performance/history?metric=fps&duration=30"
curl "http://localhost:3655/api/performance/history?metric=memory&duration=60"

# Test alerts
curl "http://localhost:3655/api/performance/alerts"

# Test benchmarks
curl "http://localhost:3655/api/performance/benchmarks"

# Test dashboard data
curl "http://localhost:3655/api/performance/dashboard"

🔧 Integration Examples

React Hook Integration

import { useEffect, useState } from 'react';

function usePerformanceMetrics() {
  const [metrics, setMetrics] = useState(null);

  useEffect(() => {
    const fetchMetrics = async () => {
      const response = await fetch('/api/performance/metrics');
      const data = await response.json();
      setMetrics(data);
    };

    fetchMetrics();
    const interval = setInterval(fetchMetrics, 2000);
    return () => clearInterval(interval);
  }, []);

  return metrics;
}

// Usage
function PerformanceDashboard() {
  const metrics = usePerformanceMetrics();

  if (!metrics) return <div>Loading...</div>;

  return (
    <div>
      <h2>FPS: {metrics.fps}</h2>
      <h2>Memory: {metrics.memory.used}MB</h2>
      <h2>Status: {metrics.status}</h2>
    </div>
  );
}

Chart.js Integration

import Chart from 'chart.js';

async function createPerformanceChart() {
  const response = await fetch('/api/performance/history?metric=memory&duration=300');
  const data = await response.json();

  new Chart(document.getElementById('performance-chart'), {
    type: 'line',
    data: {
      labels: data.history.map(point => new Date(point.timestamp).toLocaleTimeString()),
      datasets: [{
        label: 'Memory Usage (MB)',
        data: data.history.map(point => point.value),
        borderColor: 'rgb(75, 192, 192)',
        tension: 0.1
      }]
    }
  });
}

📈 Monitoring Dashboard Integration

Real-time Updates

// WebSocket or Server-Sent Events for real-time updates
const eventSource = new EventSource('/api/performance/stream');

eventSource.onmessage = (event) => {
  const metrics = JSON.parse(event.data);
  updateDashboard(metrics);
};

Alert Notifications

// Poll for alerts every 30 seconds
setInterval(async () => {
  const response = await fetch('/api/performance/alerts');
  const data = await response.json();

  data.alerts.forEach(alert => {
    showNotification(alert.title, alert.message, alert.type);
  });
}, 30000);

🔒 Security Considerations

Local Development

  • All endpoints are open for local development
  • No authentication required on localhost:3655
  • CORS enabled for dashboard integration

Production Deployment

  • Consider adding API key authentication
  • Rate limiting for monitoring endpoints
  • HTTPS enforcement
  • IP whitelisting for sensitive metrics

📊 Performance Expectations

Response Times

  • Metrics: < 10ms (local system calls)
  • History: < 50ms (data generation)
  • Alerts: < 20ms (threshold checking)
  • Benchmarks: < 5ms (static data)
  • Dashboard: < 30ms (aggregation)

Memory Overhead

  • Base API: ~2MB additional memory
  • History Storage: ~1MB for 60-second buffers
  • Concurrent Connections: Minimal impact (< 100KB per connection)

Scalability

  • Concurrent Requests: Handles 1000+ RPS
  • Data Retention: 60-second rolling history
  • Real-time Updates: 2-second intervals
  • System Load: Low CPU overhead (< 1%)

🚀 Quick Start

  1. Start the Server:

    bun run src/server.ts
  2. Test API Endpoints:

    curl "http://localhost:3655/api/performance/metrics"
  3. View Dashboard:

    http://localhost:3655
    
  4. Run Tests:

    bun run scripts/test-performance-api.ts

📚 API Reference

Endpoint Method Description
/api/performance/metrics GET Real-time performance metrics
/api/performance/history GET Historical data with query params
/api/performance/alerts GET Active performance alerts
/api/performance/benchmarks GET System benchmarks and specs
/api/performance/dashboard GET Aggregated dashboard data

All endpoints return JSON responses with proper HTTP status codes and error handling.