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
// 性能监控器模块
// 负责实时收集、聚合和分析性能指标

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::hash::Hash;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use std::time::{Duration, Instant};

/// 性能指标类型
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MetricType {
    /// CPU 使用率 (0-100%)
    CpuUsage,
    /// 内存使用量 (字节)
    MemoryUsage,
    /// 堆内存使用量 (字节)
    HeapMemory,
    /// 脚本执行时间 (微秒)
    ExecutionTime,
    /// 并发任务数
    ConcurrentTasks,
    /// 每秒请求数
    RequestsPerSecond,
    /// 缓存命中率 (0-100%)
    CacheHitRate,
    /// 垃圾回收时间 (微秒)
    GcTime,
    /// V8 堆大小 (字节)
    V8HeapSize,
    /// 自定义指标
    Custom(String),
}
/// 性能指标值
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricValue {
    /// 指标类型
    pub metric_type: MetricType,
    /// 指标值
    pub value: f64,
    /// 时间戳
    pub timestamp: u64,
    /// 标签
    pub tags: HashMap<String, _>,
}
/// 性能指标聚合结果
#[derive(Debug, Clone)]
pub struct AggregatedMetric {
    /// 指标类型
    pub metric_type: MetricType,
    /// 平均值
    pub avg: f64,
    /// 最小值
    pub min: f64,
    /// 最大值
    pub max: f64,
    /// 百分位数
    pub p50: f64,
    pub p90: f64,
    pub p95: f64,
    pub p99: f64,
    /// 样本数
    pub count: u64,
    /// 时间窗口
    pub window: Duration,
}
/// 阈值配置
#[derive(Debug, Clone)]
pub struct ThresholdConfig {
    /// 指标类型
    pub metric_type: MetricType,
    /// 警告阈值
    pub warning: f64,
    /// 严重阈值
    pub critical: f64,
    /// 是否启用
    pub enabled: bool,
}
/// 监控配置
#[derive(Debug, Clone)]
pub struct MonitorConfig {
    /// 聚合时间窗口
    pub aggregation_window: Duration,
    /// 指标保留时间
    pub retention_period: Duration,
    /// 最大指标数量
    pub max_metrics: usize,
    /// 阈值配置列表
    pub thresholds: Vec<ThresholdConfig>,
}
/// 性能监控器
#[derive(Debug)]
pub struct PerformanceMonitor {
    /// 配置
    config: MonitorConfig,
    /// 原始指标队列
    raw_metrics: Arc<Mutex<VecDeque<MetricValue>>>,
    /// 聚合指标缓存
    aggregated_metrics: Arc<Mutex<HashMap<String, _>>>,
    /// 指标收集统计
    stats: Arc<Mutex<CollectionStats>>,
}
/// 收集统计信息
#[derive(Debug, Clone)]
pub struct CollectionStats {
    /// 总收集次数
    pub total_collections: u64,
    /// 指标总数
    pub total_metrics: u64,
    /// 最后收集时间
    pub last_collection_time: Option<Instant>,
    /// 错误次数
    pub error_count: u64,
}
impl PerformanceMonitor {
    /// 创建新的性能监控器
    pub fn new(config: MonitorConfig) -> Self {
        Self {
            config,
            raw_metrics: Arc::new(Mutex::new(VecDeque::new())),
            aggregated_metrics: Arc::new(Mutex::new(HashMap::new())),
            stats: Arc::new(Mutex::new(CollectionStats {
                total_collections: 0,
                total_metrics: 0,
                last_collection_time: None,
                error_count: 0,
            })),
        }
    }
    /// 创建默认配置的性能监控器
    pub fn with_default_config() -> Self {
        let config: _ = MonitorConfig {
            aggregation_window: Duration::from_secs(10),
            retention_period: Duration::from_secs(3600), // 1小时
            max_metrics: 10000,
            thresholds: vec![
                ThresholdConfig {
                    metric_type: MetricType::CpuUsage,
                    warning: 70.0,
                    critical: 90.0,
                    enabled: true,
                },
                ThresholdConfig {
                    metric_type: MetricType::MemoryUsage,
                    warning: 80.0,
                    critical: 95.0,
                    enabled: true,
                },
                ThresholdConfig {
                    metric_type: MetricType::ExecutionTime,
                    warning: 1000.0,  // 1ms
                    critical: 5000.0, // 5ms
                    enabled: true,
                },
            ],
        };
        Self::new(config)
    }
    /// 收集性能指标
    pub fn collect_metric(&self, metric: MetricValue) -> Result<(), String> {
        let mut raw_metrics = self.raw_metrics.lock().map_err(|e| e.to_string())?;
        let mut stats = self.stats.lock().map_err(|e| e.to_string())?;
        // 添加到原始指标队列
        raw_metrics.push_back(metric.clone());
        stats.total_metrics += 1;
        // 限制队列大小
        while raw_metrics.len() > self.config.max_metrics {
            raw_metrics.pop_front();
        }
        // 清理过期指标
        self.cleanup_old_metrics()?;
        // 更新统计
        stats.total_collections += 1;
        stats.last_collection_time = Some(Instant::now());
        Ok(())
    }
    /// 收集多个性能指标
    pub fn collect_metrics(&self, metrics: Vec<MetricValue>) -> Result<(), String> {
        for metric in metrics {
            self.collect_metric(metric)?;
        }
        Ok(())
    }
    /// 获取实时指标
    pub fn get_real_time_metrics(&self) -> Result<Vec<MetricValue>, String> {
        let raw_metrics: _ = self.raw_metrics.lock().map_err(|e| e.to_string())?;
        Ok(raw_metrics.iter().cloned().collect())
    }
    /// 获取聚合指标
    pub fn get_aggregated_metrics(&self) -> Result<HashMap<String, _>, String> {
        let aggregated_metrics: _ = self.aggregated_metrics.lock().map_err(|e| e.to_string())?;
        Ok(aggregated_metrics.clone())
    }
    /// 计算聚合指标
    pub fn aggregate_metrics(&self) -> Result<(), String> {
        let raw_metrics: _ = self.raw_metrics.lock().map_err(|e| e.to_string())?;
        let mut aggregated_metrics = self.aggregated_metrics.lock().map_err(|e| e.to_string())?;
        // 按指标类型分组
        let mut grouped_metrics: HashMap<String, _> = HashMap::new();
        for metric in raw_metrics.iter() {
            grouped_metrics
                .entry(metric.metric_type.clone())
                .or_insert_with(Vec::new)
                .push(metric.value);
        }
        // 计算每个指标类型的聚合值
        for (metric_type, values) in grouped_metrics {
            if values.is_empty() {
                continue;
            }
            let mut sorted_values = values.clone();
            sorted_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            let count: _ = sorted_values.len() as u64;
            let sum: f64 = sorted_values.iter().sum();
            let avg: _ = sum / count as f64;
            let min: _ = sorted_values[0];
            let max: _ = sorted_values[count as usize - 1];
            // 计算百分位数
            let p50: _ = Self::calculate_percentile(&sorted_values, 0.50);
            let p90: _ = Self::calculate_percentile(&sorted_values, 0.90);
            let p95: _ = Self::calculate_percentile(&sorted_values, 0.95);
            let p99: _ = Self::calculate_percentile(&sorted_values, 0.99);
            let aggregated: _ = AggregatedMetric {
                metric_type,
                avg,
                min,
                max,
                p50,
                p90,
                p95,
                p99,
                count,
                window: self.config.aggregation_window,
            };
            aggregated_metrics.insert(aggregated.metric_type.clone(), aggregated);
        }
        Ok(())
    }
    /// 计算百分位数
    fn calculate_percentile(sorted_values: &[f64], percentile: f64) -> f64 {
        if sorted_values.is_empty() {
            return 0.0;
        }
        let index: _ = (sorted_values.len() as f64 * percentile).floor() as usize;
        let index: _ = std::cmp::min(index, sorted_values.len() - 1);
        sorted_values[index]
    }
    /// 检查阈值
    pub fn check_thresholds(&self) -> Result<Vec<ThresholdViolation>, String> {
        let aggregated_metrics: _ = self.aggregated_metrics.lock().map_err(|e| e.to_string())?;
        let mut violations = Vec::new();
        for threshold in &self.config.thresholds {
            if !threshold.enabled {
                continue;
            }
            if let Some(metric) = aggregated_metrics.get(&threshold.metric_type) {
                let severity: _ = if metric.avg >= threshold.critical {
                    ThresholdSeverity::Critical
                } else if metric.avg >= threshold.warning {
                    ThresholdSeverity::Warning
                } else {
                    continue;
                };
                violations.push(ThresholdViolation {
                    metric_type: threshold.metric_type.clone(),
                    current_value: metric.avg,
                    threshold_value: match severity {
                        ThresholdSeverity::Critical => threshold.critical,
                        ThresholdSeverity::Warning => threshold.warning,
                    },
                    severity,
                    timestamp: SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .unwrap()
                        .as_secs(),
                });
            }
        }
        Ok(violations)
    }
    /// 清理过期指标
    fn cleanup_old_metrics(&self) -> Result<(), String> {
        let mut raw_metrics = self.raw_metrics.lock().map_err(|e| e.to_string())?;
        let _cutoff_time: _ = Instant::now()
            .checked_sub(self.config.retention_period)
            .unwrap();
        // 这里简化处理,实际应该根据时间戳清理
        // 保留最近的数据
        while raw_metrics.len() > self.config.max_metrics / 2 {
            raw_metrics.pop_front();
        }
        Ok(())
    }
    /// 获取统计信息
    pub fn get_stats(&self) -> Result<CollectionStats, String> {
        let stats: _ = self.stats.lock().map_err(|e| e.to_string())?;
        Ok(stats.clone())
    }
    /// 获取当前时间戳
    pub fn get_current_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }
}
/// 阈值违规
#[derive(Debug, Clone)]
pub struct ThresholdViolation {
    pub metric_type: MetricType,
    pub current_value: f64,
    pub threshold_value: f64,
    pub severity: ThresholdSeverity,
    pub timestamp: u64,
}
/// 阈值严重程度
#[derive(Debug, Clone, PartialEq)]
pub enum ThresholdSeverity {
    Warning,
    Critical,
}
impl ThresholdSeverity {
    pub fn as_str(&self) -> &'static str {
        match self {
            ThresholdSeverity::Warning => "WARNING",
            ThresholdSeverity::Critical => "CRITICAL",
        }
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_performance_monitor_creation() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        assert!(monitor.get_stats().is_ok());
    }
    #[test]
    fn test_collect_single_metric() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        let metric: _ = MetricValue {
            metric_type: MetricType::CpuUsage,
            value: 50.0,
            timestamp: PerformanceMonitor::get_current_timestamp(),
            tags: HashMap::new(),
        };
        assert!(monitor.collect_metric(metric).is_ok());
        let stats: _ = monitor.get_stats().unwrap();
        assert_eq!(stats.total_metrics, 1);
    }
    #[test]
    fn test_collect_multiple_metrics() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        let metrics: _ = vec![
            MetricValue {
                metric_type: MetricType::CpuUsage,
                value: 50.0,
                timestamp: PerformanceMonitor::get_current_timestamp(),
                tags: HashMap::new(),
            },
            MetricValue {
                metric_type: MetricType::MemoryUsage,
                value: 100.0,
                timestamp: PerformanceMonitor::get_current_timestamp(),
                tags: HashMap::new(),
            },
        ];
        assert!(monitor.collect_metrics(metrics).is_ok());
        let stats: _ = monitor.get_stats().unwrap();
        assert_eq!(stats.total_metrics, 2);
    }
    #[test]
    fn test_real_time_metrics() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        let metric: _ = MetricValue {
            metric_type: MetricType::CpuUsage,
            value: 50.0,
            timestamp: PerformanceMonitor::get_current_timestamp(),
            tags: HashMap::new(),
        };
        monitor.collect_metric(metric).unwrap();
        let real_time_metrics: _ = monitor.get_real_time_metrics().unwrap();
        assert_eq!(real_time_metrics.len(), 1);
    }
    #[test]
    fn test_aggregate_metrics() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        // 收集多个 CPU 使用率指标
        for i in 0..10 {
            let metric: _ = MetricValue {
                metric_type: MetricType::CpuUsage,
                value: 50.0 + i as f64,
                timestamp: PerformanceMonitor::get_current_timestamp(),
                tags: HashMap::new(),
            };
            monitor.collect_metric(metric).unwrap();
        }
        monitor.aggregate_metrics().unwrap();
        let aggregated: _ = monitor.get_aggregated_metrics().unwrap();
        assert!(aggregated.contains_key(&MetricType::CpuUsage));
        let cpu_metric: _ = aggregated.get(&MetricType::CpuUsage).unwrap();
        assert_eq!(cpu_metric.count, 10);
        assert!(cpu_metric.avg > 0.0);
    }
    #[test]
    fn test_threshold_detection() {
        let monitor: _ = PerformanceMonitor::with_default_config();
        // 收集超过阈值的指标
        let metric: _ = MetricValue {
            metric_type: MetricType::CpuUsage,
            value: 95.0, // 超过 critical 阈值 90.0
            timestamp: PerformanceMonitor::get_current_timestamp(),
            tags: HashMap::new(),
        };
        monitor.collect_metric(metric).unwrap();
        monitor.aggregate_metrics().unwrap();
        let violations: _ = monitor.check_thresholds().unwrap();
        assert!(!violations.is_empty());
        assert_eq!(violations[0].severity, ThresholdSeverity::Critical);
    }
    #[test]
    fn test_percentile_calculation() {
        let values: _ = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let p50: _ = PerformanceMonitor::calculate_percentile(&values, 0.50);
        let p90: _ = PerformanceMonitor::calculate_percentile(&values, 0.90);
        let p95: _ = PerformanceMonitor::calculate_percentile(&values, 0.95);
        assert_eq!(p50, 5.0);
        assert_eq!(p90, 9.0);
        assert_eq!(p95, 9.5);
    }
    #[test]
    fn test_custom_metric_type() {
        let custom_type: _ = MetricType::Custom("custom_metric".to_string());
        assert_eq!(custom_type, MetricType::Custom("custom_metric".to_string()));
    }
}