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
// AI批量处理器
// 专为AI推理工作负载设计的高性能批量处理系统

use std::collections::VecDeque;
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;

/// AI任务类型
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum AiTaskType {
    /// 文本生成任务
    TextGeneration {
        prompt: String,
        max_tokens: Option<usize>,
        temperature: f32,
    },
    /// 图像分类任务
    ImageClassification {
        image_data: Vec<u8>,
        top_k: Option<usize>,
    },
    /// 嵌入向量生成
    Embedding { text: String, model_name: String },
    /// 翻译任务
    Translation {
        text: String,
        source_lang: String,
        target_lang: String,
    },
}
/// AI任务结果
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum AiTaskResult {
    TextGeneration {
        generated_text: String,
        tokens_used: usize,
        processing_time: Duration,
    },
    ImageClassification {
        predictions: Vec<(String, f32)>,
        processing_time: Duration,
    },
    Embedding {
        vector: Vec<f32>,
        dimensions: usize,
        processing_time: Duration,
    },
    Translation {
        translated_text: String,
        source_lang: String,
        target_lang: String,
        processing_time: Duration,
    },
}
/// 批处理配置
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// 最大批处理大小
    pub max_batch_size: usize,
    /// 批处理超时时间(毫秒)
    #[allow(dead_code)]
    pub batch_timeout_ms: u64,
    /// 最大并发批次数
    pub max_concurrent_batches: usize,
    /// 动态批处理调整是否启用
    #[allow(dead_code)]
    pub enable_dynamic_batching: bool,
    /// 预热批处理大小
    #[allow(dead_code)]
    pub warmup_batch_size: usize,
}
impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 100,
            batch_timeout_ms: 100,
            max_concurrent_batches: 10,
            enable_dynamic_batching: true,
            warmup_batch_size: 10,
        }
    }
}
/// AI批量处理器
pub struct AiBatchProcessor {
    config: BatchConfig,
    pending_tasks: Arc<Mutex<VecDeque<(usize, AiTaskType)>>>,
    active_batches: Arc<Mutex<usize>>,
    batch_semaphore: Arc<Semaphore>,
    next_task_id: Arc<Mutex<usize>>,
    stats: Arc<Mutex<BatchStats>>,
    results: Arc<Mutex<Vec<(usize, AiTaskResult)>>>,
}
/// 批处理统计信息
#[derive(Debug, Clone, Default)]
pub struct BatchStats {
    pub total_tasks_processed: usize,
    pub total_batches_processed: usize,
    pub total_processing_time: Duration,
    pub average_batch_size: f64,
    pub peak_memory_usage: usize,
    #[allow(dead_code)]
    pub cache_hits: usize,
    #[allow(dead_code)]
    pub cache_misses: usize,
}
impl BatchStats {
    pub fn record_batch(
        &mut self,
        batch_size: usize,
        processing_time: Duration,
        memory_used: usize,
    ) {
        self.total_tasks_processed += batch_size;
        self.total_batches_processed += 1;
        self.total_processing_time += processing_time;
        self.average_batch_size =
            self.total_tasks_processed as f64 / self.total_batches_processed as f64;
        self.peak_memory_usage = self.peak_memory_usage.max(memory_used);
    }
    #[allow(dead_code)]
    pub fn cache_hit_rate(&self) -> f64 {
        let total: _ = self.cache_hits + self.cache_misses;
        if total > 0 {
            self.cache_hits as f64 / total as f64
        } else {
            0.0
        }
    }
}
#[allow(dead_code)]
impl AiBatchProcessor {
    /// 创建新的AI批量处理器
    pub fn new(config: BatchConfig) -> Self {
        let max_concurrent_batches = config.max_concurrent_batches;
        Self {
            config,
            pending_tasks: Arc::new(Mutex::new(VecDeque::new())),
            active_batches: Arc::new(Mutex::new(0)),
            batch_semaphore: Arc::new(Semaphore::new(max_concurrent_batches)),
            next_task_id: Arc::new(Mutex::new(0)),
            stats: Arc::new(Mutex::new(BatchStats::default())),
            results: Arc::new(Mutex::new(Vec::new())),
        }
    }
    /// 添加任务到批处理队列
    pub async fn add_task(&self, task: AiTaskType) -> usize {
        let task_id: _ = {
            let mut next_id = self.next_task_id.lock().unwrap();
            let id: _ = *next_id;
            *next_id += 1;
            id
        };
        {
            let mut tasks = self.pending_tasks.lock().unwrap();
            tasks.push_back((task_id, task));
        }
        // 尝试立即处理批次
        self.try_process_batch().await;
        task_id
    }
    /// 批量添加任务
    pub async fn add_tasks(&self, tasks: Vec<AiTaskType>) -> Vec<usize> {
        let mut task_ids = Vec::with_capacity(tasks.len());
        for task in tasks {
            let task_id: _ = self.add_task(task).await;
            task_ids.push(task_id);
        }
        task_ids
    }
    /// 尝试处理批次
    async fn try_process_batch(&self) {
        // 检查是否有可用的并发批次和待处理任务
        if *self.active_batches.lock().unwrap() >= self.config.max_concurrent_batches {
            return; // 达到最大并发限制
        }
        if self.pending_tasks_count() == 0 {
            return; // 没有待处理任务
        }
        let permit: _ = self.batch_semaphore.clone().try_acquire_owned();
        if permit.is_err() {
            return; // 无法获取信号量
        }
        let permit: _ = permit.unwrap();
        let pending_tasks: _ = self.pending_tasks.clone();
        let active_batches: _ = self.active_batches.clone();
        let stats: _ = self.stats.clone();
        let config: _ = self.config.clone();
        let results: _ = self.results.clone();
        tokio::spawn(async move {
            let _permit: _ = permit;
            *active_batches.lock().unwrap() += 1;
            let batch_results: _ = Self::run_batch(pending_tasks, stats, config).await;
            // 将批次结果添加到全局结果集合
            {
                let mut all_results = results.lock().unwrap();
                all_results.extend(batch_results);
            }
            *active_batches.lock().unwrap() -= 1;
        });
    }
    /// 运行单个批次
    async fn run_batch(
        pending_tasks: Arc<Mutex<VecDeque<(usize, AiTaskType)>>>,
        stats: Arc<Mutex<BatchStats>>,
        config: BatchConfig,
    ) -> Vec<(usize, AiTaskResult)> {
        let start_time: _ = Instant::now();
        // 收集批次任务
        let batch_size: _ = config.max_batch_size;
        let mut batch_tasks = Vec::with_capacity(batch_size);
        {
            let mut tasks = pending_tasks.lock().unwrap();
            for _ in 0..batch_size {
                if let Some(task) = tasks.pop_front() {
                    batch_tasks.push(task);
                } else {
                    break;
                }
            }
        }
        // 即使没有任务也要处理,避免无限等待
        // 处理批次
        let results: _ = Self::process_batch(&batch_tasks).await;
        // 更新统计信息
        {
            let mut stats_guard = stats.lock().unwrap();
            stats_guard.record_batch(
                batch_tasks.len(),
                start_time.elapsed(),
                batch_tasks.len() * 1024, // 估算内存使用
            );
        }
        // 记录结果(实际应用中会发送到结果通道)
        println!(
            "处理批次: {} 个任务, 耗时: {:.2}ms",
            batch_tasks.len(),
            start_time.elapsed().as_secs_f64() * 1000.0
        );
        results
    }
    /// 处理单个批次
    async fn process_batch(tasks: &[(usize, AiTaskType)]) -> Vec<(usize, AiTaskResult)> {
        let mut results = Vec::with_capacity(tasks.len());
        for (task_id, task) in tasks {
            let result: _ = Self::process_single_task(task).await;
            results.push((*task_id, result));
        }
        results
    }
    /// 处理单个AI任务
    async fn process_single_task(task: &AiTaskType) -> AiTaskResult {
        let start_time: _ = Instant::now();
        let result: _ = match task {
            AiTaskType::TextGeneration {
                prompt,
                max_tokens: _,
                temperature: _,
            } => {
                // 极优化:进一步减少模拟时间
                tokio::task::yield_now().await;
                AiTaskResult::TextGeneration {
                    generated_text: format!("Generated text for: {}", prompt),
                    tokens_used: prompt.len() / 4,
                    processing_time: start_time.elapsed(),
                }
            }
            AiTaskType::ImageClassification {
                image_data: _,
                top_k,
            } => {
                // 极优化:使用 yield_now 代替 sleep
                tokio::task::yield_now().await;
                let predictions: _ = vec![
                    ("cat".to_string(), 0.85),
                    ("dog".to_string(), 0.75),
                    ("bird".to_string(), 0.65),
                ];
                let predictions: _ = if let Some(k) = top_k {
                    predictions.into_iter().take(*k).collect()
                } else {
                    predictions
                };
                AiTaskResult::ImageClassification {
                    predictions,
                    processing_time: start_time.elapsed(),
                }
            }
            AiTaskType::Embedding {
                text: _,
                model_name: _,
            } => {
                // 极优化:使用 yield_now 代替 sleep
                tokio::task::yield_now().await;
                let dimensions: _ = 384;
                let vector: _ = vec![0.1; dimensions];
                AiTaskResult::Embedding {
                    vector,
                    dimensions,
                    processing_time: start_time.elapsed(),
                }
            }
            AiTaskType::Translation {
                text,
                source_lang,
                target_lang,
            } => {
                // 极优化:使用 yield_now 代替 sleep
                tokio::task::yield_now().await;
                let translated: _ = format!(
                    "Translated: {} from {} to {}",
                    text, source_lang, target_lang
                );
                AiTaskResult::Translation {
                    translated_text: translated,
                    source_lang: source_lang.clone(),
                    target_lang: target_lang.clone(),
                    processing_time: start_time.elapsed(),
                }
            }
        };
        result
    }
    /// 获取批处理统计信息
    pub fn get_stats(&self) -> BatchStats {
        self.stats.lock().unwrap().clone()
    }
    /// 重置统计信息
    pub fn reset_stats(&self) {
        *self.stats.lock().unwrap() = BatchStats::default();
    }
    /// 获取待处理任务数量
    pub fn pending_tasks_count(&self) -> usize {
        self.pending_tasks.lock().unwrap().len()
    }
    /// 获取活跃批次数
    pub fn active_batches_count(&self) -> usize {
        *self.active_batches.lock().unwrap()
    }
    /// 等待所有任务完成并返回结果
    pub async fn flush(&self) -> Vec<(usize, AiTaskResult)> {
        // 立即处理所有待处理任务(同步方式)
        self.process_all_tasks().await;
        // 等待活跃批次完成
        while *self.active_batches.lock().unwrap() > 0 {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
        // 收集所有结果
        let all_results: _ = {
            let results: _ = self.results.lock().unwrap();
            results.clone()
        };
        all_results
    }
    /// 同步处理所有任务
    async fn process_all_tasks(&self) {
        // 持续处理直到没有待处理任务
        loop {
            // 检查是否有待处理任务
            if self.pending_tasks_count() == 0 {
                break;
            }
            // 获取信号量
            let permit: _ = self.batch_semaphore.clone().try_acquire_owned();
            if permit.is_err() {
                tokio::time::sleep(Duration::from_millis(1)).await;
                continue;
            }
            let permit: _ = permit.unwrap();
            let pending_tasks: _ = self.pending_tasks.clone();
            let active_batches: _ = self.active_batches.clone();
            let stats: _ = self.stats.clone();
            let config: _ = self.config.clone();
            let results: _ = self.results.clone();
            // 异步处理批次
            let handle: _ = tokio::spawn(async move {
                let _permit: _ = permit;
                *active_batches.lock().unwrap() += 1;
                let batch_results: _ = Self::run_batch(pending_tasks, stats, config).await;
                // 将批次结果添加到全局结果集合
                {
                    let mut all_results = results.lock().unwrap();
                    all_results.extend(batch_results);
                }
                *active_batches.lock().unwrap() -= 1;
            });
            // 等待批次完成
            let _: _ = handle.await;
        }
    }
}
/// 便利函数:创建文本生成批量处理器
#[allow(dead_code)]
pub fn create_text_generation_processor(max_batch_size: usize) -> AiBatchProcessor {
    let config: _ = BatchConfig {
        max_batch_size,
        batch_timeout_ms: 50, // 文本生成需要快速响应
        max_concurrent_batches: 20,
        enable_dynamic_batching: true,
        warmup_batch_size: 5,
    };
    AiBatchProcessor::new(config)
}
/// 便利函数:创建图像分类批量处理器
#[allow(dead_code)]
pub fn create_image_classification_processor(max_batch_size: usize) -> AiBatchProcessor {
    let config: _ = BatchConfig {
        max_batch_size,
        batch_timeout_ms: 200, // 图像分类耗时较长
        max_concurrent_batches: 5,
        enable_dynamic_batching: false, // 图像批处理通常固定大小
        warmup_batch_size: 2,
    };
    AiBatchProcessor::new(config)
}
/// 便利函数:创建嵌入向量批量处理器
#[allow(dead_code)]
pub fn create_embedding_processor(max_batch_size: usize) -> AiBatchProcessor {
    let config: _ = BatchConfig {
        max_batch_size,
        batch_timeout_ms: 100,
        max_concurrent_batches: 15,
        enable_dynamic_batching: true,
        warmup_batch_size: 8,
    };
    AiBatchProcessor::new(config)
}
#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_add_single_task() {
        let processor: _ = AiBatchProcessor::new(BatchConfig::default());
        let task: _ = AiTaskType::TextGeneration {
            prompt: "Hello, world!".to_string(),
            max_tokens: Some(100),
            temperature: 0.7,
        };
        let task_id: _ = processor.add_task(task).await;
        assert_eq!(task_id, 0);
        // 等待任务被处理(批次处理是异步的)
        tokio::time::sleep(Duration::from_millis(10)).await;
        assert_eq!(processor.pending_tasks_count(), 0); // 任务已被处理
    }
    #[tokio::test]
    async fn test_add_multiple_tasks() {
        let processor: _ = AiBatchProcessor::new(BatchConfig::default());
        let tasks: _ = vec![
            AiTaskType::TextGeneration {
                prompt: "Task 1".to_string(),
                max_tokens: Some(50),
                temperature: 0.5,
            },
            AiTaskType::TextGeneration {
                prompt: "Task 2".to_string(),
                max_tokens: Some(50),
                temperature: 0.5,
            },
        ];
        let task_ids: _ = processor.add_tasks(tasks).await;
        assert_eq!(task_ids.len(), 2);
        assert_eq!(task_ids, vec![0, 1]);
    }
    #[test]
    fn test_batch_stats() {
        let mut stats = BatchStats::default();
        stats.record_batch(10, Duration::from_millis(100), 10240);
        stats.record_batch(20, Duration::from_millis(200), 20480);
        assert_eq!(stats.total_tasks_processed, 30);
        assert_eq!(stats.total_batches_processed, 2);
        assert_eq!(stats.average_batch_size, 15.0);
        assert_eq!(stats.peak_memory_usage, 20480);
    }
    #[tokio::test]
    async fn test_processor_creation() {
        let processor: _ = create_text_generation_processor(50);
        assert_eq!(processor.config.max_batch_size, 50);
        assert_eq!(processor.config.batch_timeout_ms, 50);
        assert!(processor.config.enable_dynamic_batching);
    }
    #[tokio::test]
    async fn test_flush_processor() {
        let processor: _ = AiBatchProcessor::new(BatchConfig::default());
        let task: _ = AiTaskType::Embedding {
            text: "Test embedding".to_string(),
            model_name: "test-model".to_string(),
        };
        let _task_id: _ = processor.add_task(task).await;
        // 等待一小段时间让异步任务开始执行
        tokio::time::sleep(Duration::from_millis(50)).await;
        processor.flush().await;
        assert_eq!(processor.pending_tasks_count(), 0);
        assert_eq!(processor.active_batches_count(), 0);
    }
}