amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// 自动化测试运行器
// Stage 31.3.2: 自动化性能测试套件
//
// 该模块提供自动化测试执行能力,包括:
// - 智能测试调度
// - 并行测试执行
// - 测试结果收集
// - 错误处理和重试机制

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};
use thiserror::Error;
use std::time::Duration;
use std::time::SystemTime;

/// 测试运行错误
#[derive(Error, Debug)]
pub enum TestRunnerError {
    #[error("Test execution failed: {0}")]
    ExecutionFailed(String),
    #[error("Test timeout: {0}")]
    Timeout(String),
    #[error("Invalid test configuration: {0}")]
    ConfigError(String),
}
/// 测试类型
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TestType {
    Startup,
    Execution,
    Memory,
    Concurrent,
    All,
}
/// 测试状态
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TestStatus {
    Pending,
    Running,
    Completed,
    Failed,
    Skipped,
}
/// 测试执行结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestExecutionResult {
    pub test_name: String,
    pub test_type: TestType,
    pub status: TestStatus,
    pub result: Option<BenchmarkResult>,
    pub error: Option<String>,
    pub execution_time: Duration,
    pub timestamp: u64,
}
/// 测试计划配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestPlanConfig {
    pub test_types: Vec<TestType>,
    pub parallel_execution: bool,
    pub max_concurrent_tests: usize,
    pub timeout_per_test: Option<Duration>,
    pub retry_failed_tests: bool,
    pub max_retries: usize,
    pub save_results: bool,
    pub results_directory: String,
}
impl Default for TestPlanConfig {
    fn default() -> Self {
        Self {
            test_types: vec![
                TestType::Startup,
                TestType::Execution,
                TestType::Memory,
                TestType::Concurrent,
            ],
            parallel_execution: true,
            max_concurrent_tests: 4,
            timeout_per_test: Some(Duration::from_secs(300)), // 5 minutes
            retry_failed_tests: true,
            max_retries: 3,
            save_results: true,
            results_directory: "test_results".to_string(),
        }
    }
}
/// 自动化测试运行器
pub struct AutomatedTestRunner {
    config: TestPlanConfig,
    framework: BenchmarkFramework,
    regression_detector: Arc<Mutex<PerformanceRegressionDetector>>,
    execution_results: Arc<Mutex<Vec<TestExecutionResult>>>,
}
/// 自动化测试运行器统计
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestRunnerStats {
    pub total_tests: usize,
    pub completed_tests: usize,
    pub failed_tests: usize,
    pub skipped_tests: usize,
    pub total_execution_time: Duration,
    pub average_test_time: Duration,
    pub parallel_efficiency: f64, // 百分比
}
impl AutomatedTestRunner {
    /// 创建新的自动化测试运行器
    pub fn new(
        config: TestPlanConfig,
        framework: BenchmarkFramework,
        regression_detector: Arc<Mutex<PerformanceRegressionDetector>>,
    ) -> Self {
        Self {
            config,
            framework,
            regression_detector,
            execution_results: Arc::new(Mutex::new(Vec::new())),
        }
    }
    /// 创建默认配置的运行器
    pub fn new_default(regression_detector: Arc<Mutex<PerformanceRegressionDetector>>) -> Self {
        let config: _ = TestPlanConfig::default();
        let framework: _ = BenchmarkFramework::new_default();
        Self::new(config, framework, regression_detector)
    }
    /// 运行完整的测试套件
    pub async fn run_full_test_suite(&self) -> Result<TestSuiteResults, TestRunnerError> {
        println!("🚀 Starting automated performance test suite...");
        let start_time: _ = SystemTime::now();
        // 创建所有测试任务
        let test_tasks: _ = self.create_test_tasks();
        // 执行测试
        let results: _ = if self.config.parallel_execution {
            self.run_tests_parallel(test_tasks).await?
        } else {
            self.run_tests_sequential(test_tasks).await?
        };
        let total_time: _ = start_time.elapsed().unwrap_or_default();
        // 生成统计信息
        let stats: _ = self.generate_stats(&results, total_time);
        println!("✅ Test suite completed in {:.2}s", total_time.as_secs_f64());
        println!("📊 Tests: {} completed, {} failed, {} skipped",
            stats.completed_tests,
            stats.failed_tests,
            stats.skipped_tests
        );
        Ok(TestSuiteResults {
            results,
            stats,
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        })
    }
    /// 创建测试任务列表
    fn create_test_tasks(&self) -> Vec<ScheduledTest> {
        let mut tasks = Vec::new();
        for test_type in &self.config.test_types {
            match test_type {
                TestType::Startup => {
                    tasks.extend(self.create_startup_tests());
                }
                TestType::Execution => {
                    tasks.extend(self.create_execution_tests());
                }
                TestType::Memory => {
                    tasks.extend(self.create_memory_tests());
                }
                TestType::Concurrent => {
                    tasks.extend(self.create_concurrent_tests());
                }
                TestType::All => {
                    tasks.extend(self.create_startup_tests());
                    tasks.extend(self.create_execution_tests());
                    tasks.extend(self.create_memory_tests());
                    tasks.extend(self.create_concurrent_tests());
                }
            }
        }
        tasks
    }
    /// 创建启动时间测试
    fn create_startup_tests(&self) -> Vec<ScheduledTest> {
        vec![
            ScheduledTest {
                name: "cold_start".to_string(),
                test_type: TestType::Startup,
                config: BenchmarkConfig {
                    iterations: 100,
                    warmup_iterations: 5,
                    timeout: Some(Duration::from_secs(30)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "warm_start".to_string(),
                test_type: TestType::Startup,
                config: BenchmarkConfig {
                    iterations: 1000,
                    warmup_iterations: 10,
                    timeout: Some(Duration::from_secs(30)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "v8_init".to_string(),
                test_type: TestType::Startup,
                config: BenchmarkConfig {
                    iterations: 500,
                    warmup_iterations: 10,
                    timeout: Some(Duration::from_secs(60)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
        ]
    }
    /// 创建执行速度测试
    fn create_execution_tests(&self) -> Vec<ScheduledTest> {
        vec![
            ScheduledTest {
                name: "expression_eval".to_string(),
                test_type: TestType::Execution,
                config: BenchmarkConfig {
                    iterations: 10000,
                    warmup_iterations: 100,
                    timeout: Some(Duration::from_secs(120)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "function_call".to_string(),
                test_type: TestType::Execution,
                config: BenchmarkConfig {
                    iterations: 5000,
                    warmup_iterations: 50,
                    timeout: Some(Duration::from_secs(120)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "object_creation".to_string(),
                test_type: TestType::Execution,
                config: BenchmarkConfig {
                    iterations: 3000,
                    warmup_iterations: 30,
                    timeout: Some(Duration::from_secs(120)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
        ]
    }
    /// 创建内存使用测试
    fn create_memory_tests(&self) -> Vec<ScheduledTest> {
        vec![
            ScheduledTest {
                name: "memory_allocation".to_string(),
                test_type: TestType::Memory,
                config: BenchmarkConfig {
                    iterations: 1000,
                    warmup_iterations: 10,
                    timeout: Some(Duration::from_secs(180)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "memory_pool".to_string(),
                test_type: TestType::Memory,
                config: BenchmarkConfig {
                    iterations: 2000,
                    warmup_iterations: 20,
                    timeout: Some(Duration::from_secs(180)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
        ]
    }
    /// 创建并发性能测试
    fn create_concurrent_tests(&self) -> Vec<ScheduledTest> {
        vec![
            ScheduledTest {
                name: "concurrent_execution".to_string(),
                test_type: TestType::Concurrent,
                config: BenchmarkConfig {
                    iterations: 100,
                    warmup_iterations: 5,
                    timeout: Some(Duration::from_secs(240)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
            ScheduledTest {
                name: "async_operations".to_string(),
                test_type: TestType::Concurrent,
                config: BenchmarkConfig {
                    iterations: 500,
                    warmup_iterations: 10,
                    timeout: Some(Duration::from_secs(240)),
                    save_raw_data: true,
                    compare_with_baseline: true,
                },
            },
        ]
    }
    /// 并行执行测试
    async fn run_tests_parallel(
        &self,
        tasks: Vec<ScheduledTest>,
    ) -> Result<Vec<TestExecutionResult>, TestRunnerError> {
        let semaphore: _ = Arc::new(Mutex::new(tokio::sync::Semaphore::new(self.config.max_concurrent_tests)));
        let mut handles: Vec<JoinHandle<Result<TestExecutionResult, TestRunnerError>>> = Vec::new();
        for task in tasks {
            let semaphore: _ = semaphore.clone();
            let handle: _ = tokio::spawn(async move {
                let _permit: _ = semaphore.acquire().await.map_err(|e| {
                    TestRunnerError::ExecutionFailed(e.to_string())
                })?;
                // We can't easily capture self in async block, so we need to refactor
                // For now, return an error to indicate this needs fixing
                Err(TestRunnerError::ExecutionFailed(
                    "Async test execution needs refactoring".to_string()))
            });
            handles.push(handle);
        }
        // 等待所有测试完成
        let mut results = Vec::new();
        for handle in handles {
            match handle.await {
                Ok(result) => results.push(result?),
                Err(e) => {
                    return Err(TestRunnerError::ExecutionFailed(e.to_string()));
                }
            }
        }
        Ok(results)
    }
    /// 顺序执行测试
    async fn run_tests_sequential(
        &self,
        tasks: Vec<ScheduledTest>,
    ) -> Result<Vec<TestExecutionResult>, TestRunnerError> {
        let mut results = Vec::new();
        for task in tasks {
            let result: _ = self.run_single_test(task).await?;
            results.push(result);
        }
        Ok(results)
    }
    /// 执行单个测试
    async fn run_single_test(
        &self,
        task: ScheduledTest,
    ) -> Result<TestExecutionResult, TestRunnerError> {
        println!("🔄 Running test: {} ({:?})", task.name, task.test_type);
        let start_time: _ = SystemTime::now();
        let timestamp: _ = start_time
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let mut status = TestStatus::Running;
        let mut result = None;
        let mut error = None;
        // 设置超时
        let timeout_duration: _ = self.config.timeout_per_test.unwrap_or(Duration::from_secs(300));
        // 执行测试(使用 tokio::time::timeout 实现超时)
        let test_future: _ = self.execute_test(&task);
        match tokio::time::timeout(timeout_duration, test_future).await {
            Ok(Ok(res)) => {
                result = Some(res);
                status = TestStatus::Completed;
                println!("✅ Test completed: {}", task.name);
            }
            Ok(Err(e)) => {
                error = Some(e.to_string());
                status = TestStatus::Failed;
                println!("❌ Test failed: {} - {}", task.name, e);
            }
            Err(_) => {
                error = Some("Test timeout".to_string());
                status = TestStatus::Failed;
                println!("⏱️  Test timeout: {}", task.name);
            }
        }
        let execution_time: _ = start_time.elapsed().unwrap_or_default();
        Ok(TestExecutionResult {
            test_name: task.name,
            test_type: task.test_type,
            status,
            result,
            error,
            execution_time,
            timestamp,
        })
    }
    /// 实际执行测试逻辑
    async fn execute_test(
        &self,
        task: &ScheduledTest,
    ) -> Result<BenchmarkResult, TestRunnerError> {
        // 根据测试类型执行相应的基准测试
        match task.test_type {
            TestType::Startup => {
                // 这里应该调用实际的启动时间基准测试
                // 目前返回模拟结果
                Ok(self.framework.run_benchmark(
                    &task.name,
                    MetricType::StartupTime,
                    || {
                        // 模拟启动测试
                        std::thread::sleep(Duration::from_millis(10));
                    },
                ))
            }
            TestType::Execution => {
                Ok(self.framework.run_benchmark(
                    &task.name,
                    MetricType::ExecutionTime,
                    || {
                        // 模拟执行测试
                        let mut sum = 0;
                        for i in 0..1000 {
                            sum += i;
                        }
                        sum
                    },
                ))
            }
            TestType::Memory => {
                Ok(self.framework.run_benchmark_with_memory(
                    &task.name,
                    MetricType::MemoryUsage,
                    || {
                        // 模拟内存测试
                        let vec: _ = vec![0u64; 1000];
                        vec
                    },
                ))
            }
            TestType::Concurrent => {
                Ok(self.framework.run_benchmark(
                    &task.name,
                    MetricType::OperationsPerSecond,
                    || {
                        // 模拟并发测试
                        std::thread::sleep(Duration::from_millis(1));
                    },
                ))
            }
            TestType::All => {
                Err(TestRunnerError::ConfigError(
                    "TestType::All should be expanded before execution".to_string(),
                ))
            }
        }
    }
    /// 生成测试统计信息
    fn generate_stats(
        &self,
        results: &[TestExecutionResult],
        total_time: Duration,
    ) -> TestRunnerStats {
        let completed_tests: _ = results.iter().filter(|r| r.status == TestStatus::Completed).count();
        let failed_tests: _ = results.iter().filter(|r| r.status == TestStatus::Failed).count();
        let skipped_tests: _ = results.iter().filter(|r| r.status == TestStatus::Skipped).count();
        let total_execution_time: Duration = results
            .iter()
            .map(|r| r.execution_time)
            .sum();
        let average_test_time: _ = if !results.is_empty() {
            Duration::from_nanos(total_execution_time.as_nanos() as u64 / results.len() as u64)
        } else {
            Duration::default()
        };
        // 计算并行效率
        let parallel_efficiency: _ = if total_execution_time.as_secs_f64() > 0.0 {
            (total_execution_time.as_secs_f64() / total_time.as_secs_f64()) * 100.0
        } else {
            0.0
        };
        TestRunnerStats {
            total_tests: results.len(),
            completed_tests,
            failed_tests,
            skipped_tests,
            total_execution_time,
            average_test_time,
            parallel_efficiency,
        }
    }
    /// 获取测试结果
    pub fn get_results(&self) -> Vec<TestExecutionResult> {
        self.execution_results.lock().unwrap().clone()
    }
    /// 运行特定类型的测试
    pub async fn run_test_type(&self, test_type: TestType) -> Result<TestSuiteResults, TestRunnerError> {
        let config: _ = TestPlanConfig {
            test_types: vec![test_type],
            parallel_execution: self.config.parallel_execution,
            max_concurrent_tests: self.config.max_concurrent_tests,
            timeout_per_test: self.config.timeout_per_test,
            retry_failed_tests: self.config.retry_failed_tests,
            max_retries: self.config.max_retries,
            save_results: self.config.save_results,
            results_directory: self.config.results_directory.clone(),
        };
        let runner: _ = AutomatedTestRunner::new(
            config,
            self.framework.clone(),
            self.regression_detector.clone(),
        );
        runner.run_full_test_suite().await
    }
}
/// 计划中的测试
#[derive(Debug, Clone)]
pub struct ScheduledTest {
    pub name: String,
    pub test_type: TestType,
    pub config: BenchmarkConfig,
}
/// 测试套件结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestSuiteResults {
    pub results: Vec<TestExecutionResult>,
    pub stats: TestRunnerStats,
    pub timestamp: u64,
}
impl TestSuiteResults {
    /// 生成测试套件总结报告
    pub fn generate_summary(&self) -> String {
        let mut report = String::new();
        report.push_str("\n=== Automated Test Suite Results ===\n");
        report.push_str(&format!("Timestamp: {}\n", self.timestamp));
        report.push_str(&format!("Total Tests: {}\n", self.stats.total_tests));
        report.push_str(&format!("Completed: {}\n", self.stats.completed_tests));
        report.push_str(&format!("Failed: {}\n", self.stats.failed_tests));
        report.push_str(&format!("Skipped: {}\n", self.stats.skipped_tests));
        report.push_str(&format!("Total Execution Time: {:.2}s\n", self.stats.total_execution_time.as_secs_f64()));
        report.push_str(&format!("Average Test Time: {:.2}ms\n", self.stats.average_test_time.as_secs_f64() * 1000.0));
        report.push_str(&format!("Parallel Efficiency: {:.1}%\n\n", self.stats.parallel_efficiency));
        // 详细结果
        for result in &self.results {
            report.push_str(&format!("Test: {} ({:?})\n", result.test_name, result.test_type));
            report.push_str(&format!("  Status: {:?}\n", result.status));
            report.push_str(&format!("  Execution Time: {:.2}ms\n", result.execution_time.as_secs_f64() * 1000.0));
            if let Some(error) = &result.error {
                report.push_str(&format!("  Error: {}\n", error));
            }
            if let Some(benchmark_result) = &result.result {
                report.push_str(&format!("  Avg Duration: {:.2}μs\n", benchmark_result.avg_duration.as_secs_f64() * 1_000_000.0));
                report.push_str(&format!("  Ops/sec: {:.0}\n", benchmark_result.operations_per_second));
            }
            report.push('\n');
        }
        report
    }
}