1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/// Test Game Process Management
///
/// Utilities for managing Bevy game processes during integration testing,
/// including starting, stopping, and communicating with test games.
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use tokio::time::sleep;
use serde_json::{json, Value};
use bevy_debugger_mcp::{
config::Config,
brp_client::BrpClient,
error::{Error, Result},
};
/// Manages a Bevy game process for testing
pub struct TestGameProcess {
pub game_name: String,
pub process: Option<Child>,
pub brp_client: Option<BrpClient>,
pub config: Config,
}
impl TestGameProcess {
/// Create a new test game process manager
pub async fn new(game_name: &str) -> Self {
let config = Config {
bevy_brp_host: "localhost".to_string(),
bevy_brp_port: 15702,
mcp_port: 3001,
};
Self {
game_name: game_name.to_string(),
process: None,
brp_client: None,
config,
}
}
/// Start the test game process
pub async fn start(&mut self) -> Result<()> {
// For testing, we'll simulate a game process
// In a real implementation, this would start an actual Bevy game
println!("Starting test game: {}", self.game_name);
// Simulate starting the game process
// In practice, you would run something like:
// let child = Command::new("cargo")
// .args(&["run", "--bin", &self.game_name])
// .stdout(Stdio::piped())
// .stderr(Stdio::piped())
// .spawn()?;
// self.process = Some(child);
// Initialize BRP client for communication
self.brp_client = Some(BrpClient::new(&self.config));
// Wait for game to be ready
self.wait_for_ready().await?;
Ok(())
}
/// Wait for the game to be ready for testing
pub async fn wait_for_ready(&mut self) -> Result<()> {
let max_attempts = 30;
let mut attempts = 0;
while attempts < max_attempts {
if let Some(ref mut client) = self.brp_client {
// Try to connect to the game
match client.connect_with_retry().await {
Ok(_) => {
println!("Test game {} is ready", self.game_name);
return Ok(());
}
Err(_) => {
attempts += 1;
sleep(Duration::from_secs(1)).await;
}
}
} else {
return Err(Error::Connection("No BRP client initialized".to_string()));
}
}
Err(Error::Timeout(format!(
"Test game {} failed to start within {} seconds",
self.game_name, max_attempts
)))
}
/// Send a command to the game via BRP
pub async fn send_command(&mut self, method: &str, params: Value) -> Result<Value> {
if let Some(ref mut client) = self.brp_client {
// In a real implementation, this would send actual BRP requests
// For testing, we'll simulate responses based on the method
match method {
"bevy_debugger/get_entity_count" => {
Ok(json!({
"total": 150,
"simple": 50,
"moving": 60,
"complex": 40
}))
}
"bevy_debugger/get_performance_metrics" => {
Ok(json!({
"average_frame_time": 0.016,
"fps": 60.0,
"entity_count": 150,
"uptime": 30.0
}))
}
"bevy_debugger/spawn_entities" => {
let count = params.get("count").and_then(|c| c.as_u64()).unwrap_or(10);
Ok(json!({
"spawned": count,
"success": true
}))
}
"bevy_debugger/stress_test" => {
let test_type = params.get("type").and_then(|t| t.as_str()).unwrap_or("entity_spawn");
let intensity = params.get("intensity").and_then(|i| i.as_u64()).unwrap_or(1);
Ok(json!({
"stress_test": test_type,
"intensity": intensity,
"entities_affected": intensity * 50,
"success": true
}))
}
"bevy_debugger/query_entities" => {
Ok(json!({
"entities": [
{"id": "entity_1", "type": "warrior", "health": 100.0},
{"id": "entity_2", "type": "merchant", "health": 80.0},
{"id": "entity_3", "type": "villager", "health": 90.0}
],
"total_found": 3
}))
}
_ => {
Ok(json!({
"method": method,
"params": params,
"simulated": true,
"timestamp": std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}))
}
}
} else {
Err(Error::Connection("No BRP client available".to_string()))
}
}
/// Check if the game process is still running
pub fn is_running(&mut self) -> bool {
if let Some(ref mut process) = self.process {
match process.try_wait() {
Ok(Some(_)) => false, // Process has exited
Ok(None) => true, // Process is still running
Err(_) => false, // Error checking process
}
} else {
// For simulated processes, always return true during testing
true
}
}
/// Get the game's current performance metrics
pub async fn get_performance_metrics(&mut self) -> Result<Value> {
self.send_command("bevy_debugger/get_performance_metrics", json!({})).await
}
/// Spawn entities in the test game
pub async fn spawn_entities(&mut self, entity_type: &str, count: usize) -> Result<Value> {
self.send_command("bevy_debugger/spawn_entities", json!({
"type": entity_type,
"count": count
})).await
}
/// Run a stress test scenario
pub async fn run_stress_test(&mut self, test_type: &str, intensity: usize) -> Result<Value> {
self.send_command("bevy_debugger/stress_test", json!({
"type": test_type,
"intensity": intensity
})).await
}
/// Query entities with filters
pub async fn query_entities(&mut self, filters: Value) -> Result<Value> {
self.send_command("bevy_debugger/query_entities", filters).await
}
/// Wait for a specific condition to be met
pub async fn wait_for_condition<F>(&mut self, condition: F, timeout: Duration) -> Result<()>
where
F: Fn(&Value) -> bool + Send,
{
let start = std::time::Instant::now();
while start.elapsed() < timeout {
match self.get_performance_metrics().await {
Ok(metrics) => {
if condition(&metrics) {
return Ok(());
}
}
Err(_) => {
// Continue trying even if individual requests fail
}
}
sleep(Duration::from_millis(100)).await;
}
Err(Error::Timeout("Condition not met within timeout".to_string()))
}
/// Cleanup and stop the test game
pub async fn cleanup(&mut self) -> Result<()> {
println!("Cleaning up test game: {}", self.game_name);
if let Some(mut process) = self.process.take() {
// Attempt graceful shutdown first
if let Err(e) = process.kill() {
println!("Failed to kill test game process: {}", e);
}
// Wait for process to exit
if let Err(e) = process.wait() {
println!("Error waiting for test game process to exit: {}", e);
}
}
self.brp_client = None;
Ok(())
}
}
impl Drop for TestGameProcess {
fn drop(&mut self) {
// Ensure cleanup happens even if not called explicitly
if let Some(mut process) = self.process.take() {
let _ = process.kill();
}
}
}
/// Helper for running tests with a managed game process
pub async fn with_test_game<F, Fut, R>(game_name: &str, test_fn: F) -> Result<R>
where
F: FnOnce(TestGameProcess) -> Fut,
Fut: std::future::Future<Output = Result<R>>,
{
let mut game_process = TestGameProcess::new(game_name).await;
// Start the game
game_process.start().await?;
// Run the test
let result = test_fn(game_process).await;
// Cleanup is handled by Drop trait
result
}
/// Configuration for test scenarios
#[derive(Debug, Clone)]
pub struct TestScenario {
pub name: String,
pub entity_count: usize,
pub duration: Duration,
pub stress_level: usize,
pub expected_fps: f32,
pub max_memory_mb: usize,
}
impl TestScenario {
/// Create a basic performance test scenario
pub fn basic_performance() -> Self {
Self {
name: "Basic Performance".to_string(),
entity_count: 100,
duration: Duration::from_secs(30),
stress_level: 1,
expected_fps: 30.0,
max_memory_mb: 100,
}
}
/// Create a high-stress test scenario
pub fn high_stress() -> Self {
Self {
name: "High Stress".to_string(),
entity_count: 1000,
duration: Duration::from_secs(60),
stress_level: 3,
expected_fps: 15.0,
max_memory_mb: 200,
}
}
/// Create a memory pressure test scenario
pub fn memory_pressure() -> Self {
Self {
name: "Memory Pressure".to_string(),
entity_count: 2000,
duration: Duration::from_secs(45),
stress_level: 2,
expected_fps: 10.0,
max_memory_mb: 300,
}
}
/// Run this scenario against a test game
pub async fn run_against_game(&self, game_process: &mut TestGameProcess) -> Result<ScenarioResults> {
println!("Running scenario: {}", self.name);
let start_time = std::time::Instant::now();
let mut measurements = Vec::new();
// Spawn initial entities
game_process.spawn_entities("complex", self.entity_count).await?;
// Run stress test if specified
if self.stress_level > 0 {
game_process.run_stress_test("continuous_spawn", self.stress_level).await?;
}
// Collect measurements during the test
while start_time.elapsed() < self.duration {
let metrics = game_process.get_performance_metrics().await?;
measurements.push(metrics);
sleep(Duration::from_secs(1)).await;
}
Ok(ScenarioResults {
scenario_name: self.name.clone(),
duration: start_time.elapsed(),
measurements,
target_fps: self.expected_fps,
max_memory_mb: self.max_memory_mb,
})
}
}
/// Results from running a test scenario
#[derive(Debug)]
pub struct ScenarioResults {
pub scenario_name: String,
pub duration: Duration,
pub measurements: Vec<Value>,
pub target_fps: f32,
pub max_memory_mb: usize,
}
impl ScenarioResults {
/// Calculate average FPS from measurements
pub fn average_fps(&self) -> f32 {
let total_fps: f32 = self.measurements
.iter()
.filter_map(|m| m.get("fps").and_then(|f| f.as_f64()).map(|f| f as f32))
.sum();
if self.measurements.is_empty() {
0.0
} else {
total_fps / self.measurements.len() as f32
}
}
/// Calculate minimum FPS
pub fn min_fps(&self) -> f32 {
self.measurements
.iter()
.filter_map(|m| m.get("fps").and_then(|f| f.as_f64()).map(|f| f as f32))
.fold(f32::INFINITY, |min, fps| min.min(fps))
}
/// Calculate maximum entity count
pub fn max_entities(&self) -> usize {
self.measurements
.iter()
.filter_map(|m| m.get("entity_count").and_then(|c| c.as_u64()).map(|c| c as usize))
.max()
.unwrap_or(0)
}
/// Check if scenario passed performance targets
pub fn meets_targets(&self) -> bool {
let avg_fps = self.average_fps();
let min_fps = self.min_fps();
avg_fps >= self.target_fps && min_fps >= self.target_fps * 0.8
}
/// Generate a performance report
pub fn generate_report(&self) -> String {
format!(
"Scenario: {}\n\
Duration: {:.2}s\n\
Average FPS: {:.1} (target: {:.1})\n\
Min FPS: {:.1}\n\
Max Entities: {}\n\
Measurements: {}\n\
Passed: {}",
self.scenario_name,
self.duration.as_secs_f32(),
self.average_fps(),
self.target_fps,
self.min_fps(),
self.max_entities(),
self.measurements.len(),
if self.meets_targets() { "✓" } else { "✗" }
)
}
}