# New Optimization Testing Guide

## Test Real-Time Dashboard with Active Optimization

This will let you see the WebSocket updates in real-time as trials complete.

## 1. Start Dashboard (Both Servers)

### Terminal 1 - Backend
```bash
cd atomizer-dashboard/backend
python -m uvicorn api.main:app --reload --port 8000
```

### Terminal 2 - Frontend
```bash
cd atomizer-dashboard/frontend
npm run dev
```

Visit: `http://localhost:3003`

## 2. Connect WebSocket to Existing Study

Open browser console and run:
```javascript
const ws = new WebSocket('ws://localhost:8000/api/ws/optimization/bracket_stiffness_optimization_V3');

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log(`[${message.type}]`, message.data);
};

ws.onopen = () => console.log('✓ Connected to optimization stream');
```

You should see:
```
✓ Connected to optimization stream
[connected] {study_id: "bracket_stiffness_optimization_V3", current_trials: 100, ...}
```

## 3. Start a Small Optimization Run (5 Trials)

### Terminal 3 - Run Optimization
```bash
cd studies/bracket_stiffness_optimization_V3
python run_optimization.py --trials 5
```

## 4. Watch Real-Time Events

As trials complete, you'll see WebSocket events:

```javascript
// Trial completed
[trial_completed] {
  trial_number: 101,
  objective: 0.0234,
  params: {rib_thickness_1: 2.3, ...},
  ...
}

// Progress update
[progress] {
  current: 101,
  total: 105,
  percentage: 96.19
}

// New best found (if better than previous)
[new_best] {
  trial_number: 103,
  objective: 0.0198,
  ...
}

// Pareto front update (multi-objective)
[pareto_front] {
  pareto_front: [{...}, {...}],
  count: 49
}
```

## 5. Test Report Generation While Running

While optimization is running, generate a report:

```bash
curl -X POST "http://localhost:8000/api/optimization/studies/bracket_stiffness_optimization_V3/generate-report?format=html"
```

Then download it:
```bash
curl http://localhost:8000/api/optimization/studies/bracket_stiffness_optimization_V3/reports/optimization_report.html -o report.html
```

Open `report.html` in browser to see formatted report with all 100+ trials.

## 6. Expected Behavior

- WebSocket receives events as trials complete (2-5 minute intervals per trial)
- Progress percentage updates
- Pareto front grows if new non-dominated solutions found
- Report can be generated at any point during optimization
- All endpoints remain responsive during optimization

## 7. Production Testing

For full production test:
```bash
python run_optimization.py --trials 50
```

This will run for several hours and provide extensive real-time data for dashboard testing.

## Notes

- Each trial takes 2-5 minutes (NX simulation solve time)
- WebSocket will broadcast updates immediately upon trial completion
- Frontend should handle all 6 event types gracefully
- Reports update dynamically as new trials complete
