pub struct BatchProcessingStats {
pub tasks_submitted: u64,
pub tasks_processed: u64,
pub tasks_succeeded: u64,
pub tasks_failed: u64,
pub total_processing_time: Duration,
pub min_processing_time: Duration,
pub max_processing_time: Duration,
}
Expand description
批处理统计信息
Fields§
§tasks_submitted: u64
§tasks_processed: u64
§tasks_succeeded: u64
§tasks_failed: u64
§total_processing_time: Duration
§min_processing_time: Duration
§max_processing_time: Duration
Implementations§
Source§impl BatchProcessingStats
impl BatchProcessingStats
Sourcepub fn success_rate(&self) -> f64
pub fn success_rate(&self) -> f64
计算成功率
Examples found in repository?
examples/performance_test.rs (line 189)
51async fn main() -> Result<(), Box<dyn std::error::Error>> {
52 println!("🚀 HTML翻译库性能测试\n");
53
54 let large_html = generate_large_html();
55 let dom = parse_html(&large_html);
56
57 println!("📊 测试数据:");
58 println!(" - HTML大小: {} KB", large_html.len() / 1024);
59 println!(" - 包含 ~5000 个文本元素\n");
60
61 // 1. DOM遍历性能测试
62 println!("🔍 DOM遍历性能对比:");
63
64 // 原始递归收集器
65 let start = Instant::now();
66 let original_collector = TextCollector::new();
67 let original_items = original_collector.collect_from_dom(&dom)?;
68 let original_time = start.elapsed();
69 println!(" - 原始递归收集器: {}ms, 收集到 {} 项",
70 original_time.as_millis(), original_items.len());
71
72 // 优化的迭代收集器
73 let start = Instant::now();
74 let mut optimized_collector = OptimizedTextCollector::new();
75 let optimized_items = optimized_collector.collect_from_dom_optimized(&dom)?;
76 let optimized_time = start.elapsed();
77 println!(" - 优化迭代收集器: {}ms, 收集到 {} 项",
78 optimized_time.as_millis(), optimized_items.len());
79
80 let speedup = original_time.as_millis() as f64 / optimized_time.as_millis() as f64;
81 println!(" ✨ 性能提升: {:.2}x 倍速\n", speedup);
82
83 // 2. 内存管理测试
84 println!("💾 内存管理优化:");
85 let memory_manager = Arc::new(GlobalMemoryManager::new());
86
87 // 测试字符串池效率
88 let start = Instant::now();
89 let mut test_strings = Vec::new();
90 for i in 0..1000 {
91 let s = memory_manager.acquire_string(50);
92 test_strings.push(format!("Test string {}", i));
93 }
94
95 for s in test_strings {
96 memory_manager.release_string(s);
97 }
98 let pool_time = start.elapsed();
99
100 // 获取内存统计
101 let (pool_stats, _) = memory_manager.get_pool_stats();
102 println!(" - 字符串池操作: {}ms", pool_time.as_millis());
103 println!(" - 池大小: 小型={}, 大型={}, 总容量={}KB",
104 pool_stats.small_pool_size,
105 pool_stats.large_pool_size,
106 pool_stats.total_capacity / 1024);
107
108 // 3. 智能缓存测试
109 println!("\n🧠 智能缓存性能:");
110 use html_translation_lib::config::TranslationConfig;
111
112 let config = TranslationConfig::new()
113 .target_language("zh")
114 .api_url("http://example.com/translate");
115
116 let mut cache_manager = SmartCacheManager::new(&config).await?;
117
118 let start = Instant::now();
119
120 // 模拟缓存操作
121 for i in 0..500 {
122 let key = format!("text_{}", i % 100); // 重复键以测试缓存命中
123 let value = format!("translation_{}", i);
124 cache_manager.set(&key, value).await?;
125 }
126
127 // 测试缓存命中
128 let mut hits = 0;
129 for i in 0..100 {
130 let key = format!("text_{}", i);
131 if cache_manager.get(&key).await?.is_some() {
132 hits += 1;
133 }
134 }
135
136 let cache_time = start.elapsed();
137 let cache_stats = cache_manager.stats();
138
139 println!(" - 缓存操作时间: {}ms", cache_time.as_millis());
140 println!(" - 缓存命中数: {} / 100", hits);
141 println!(" - L1命中率: {:.1}%", cache_stats.l1_hit_rate() * 100.0);
142 println!(" - 总命中率: {:.1}%", cache_stats.hit_rate() * 100.0);
143
144 // 4. 并发批处理测试
145 println!("\n⚡ 并发批处理性能:");
146 let batch_config = BatchConfig {
147 batch_size: 20,
148 max_concurrency: 4,
149 timeout_duration: std::time::Duration::from_secs(10),
150 ..Default::default()
151 };
152
153 let batch_processor = ConcurrentBatchProcessor::new(
154 batch_config,
155 Arc::clone(&memory_manager)
156 );
157
158 // 准备测试文本
159 let test_texts: Vec<String> = (0..200)
160 .map(|i| format!("Test text number {}", i))
161 .collect();
162
163 let start = Instant::now();
164
165 // 提交批处理任务
166 let mut task_ids = Vec::new();
167 for chunk in test_texts.chunks(50) {
168 let task_id = batch_processor.submit_batch(
169 chunk.to_vec(),
170 Priority::Normal
171 ).await?;
172 task_ids.push(task_id);
173 }
174
175 // 模拟翻译函数
176 async fn mock_translate(texts: Vec<String>) -> html_translation_lib::error::TranslationResult<Vec<String>> {
177 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
178 Ok(texts.into_iter().map(|t| format!("翻译_{}", t)).collect())
179 }
180
181 // 处理队列
182 let _results = batch_processor.process_queue(mock_translate).await?;
183 let batch_time = start.elapsed();
184
185 let batch_stats = batch_processor.get_stats().await;
186 let queue_status = batch_processor.get_queue_status();
187
188 println!(" - 批处理总时间: {}ms", batch_time.as_millis());
189 println!(" - 处理成功率: {:.1}%", batch_stats.success_rate() * 100.0);
190 println!(" - 平均处理时间: {}ms", batch_stats.average_processing_time().as_millis());
191 println!(" - 吞吐量: {:.1} 任务/秒", batch_stats.throughput());
192 println!(" - 队列剩余: {} 任务", queue_status.total_tasks);
193
194 // 5. 总结
195 println!("\n📈 性能优化总结:");
196 println!(" ✅ DOM遍历优化: {:.1}x 性能提升", speedup);
197 println!(" ✅ 内存池管理: 减少 GC 压力");
198 println!(" ✅ 智能缓存: {:.1}% 命中率", cache_stats.hit_rate() * 100.0);
199 println!(" ✅ 并发批处理: {:.1}% 成功率", batch_stats.success_rate() * 100.0);
200 println!("\n🎉 所有性能优化测试完成!");
201
202 Ok(())
203}
Sourcepub fn average_processing_time(&self) -> Duration
pub fn average_processing_time(&self) -> Duration
计算平均处理时间
Examples found in repository?
examples/performance_test.rs (line 190)
51async fn main() -> Result<(), Box<dyn std::error::Error>> {
52 println!("🚀 HTML翻译库性能测试\n");
53
54 let large_html = generate_large_html();
55 let dom = parse_html(&large_html);
56
57 println!("📊 测试数据:");
58 println!(" - HTML大小: {} KB", large_html.len() / 1024);
59 println!(" - 包含 ~5000 个文本元素\n");
60
61 // 1. DOM遍历性能测试
62 println!("🔍 DOM遍历性能对比:");
63
64 // 原始递归收集器
65 let start = Instant::now();
66 let original_collector = TextCollector::new();
67 let original_items = original_collector.collect_from_dom(&dom)?;
68 let original_time = start.elapsed();
69 println!(" - 原始递归收集器: {}ms, 收集到 {} 项",
70 original_time.as_millis(), original_items.len());
71
72 // 优化的迭代收集器
73 let start = Instant::now();
74 let mut optimized_collector = OptimizedTextCollector::new();
75 let optimized_items = optimized_collector.collect_from_dom_optimized(&dom)?;
76 let optimized_time = start.elapsed();
77 println!(" - 优化迭代收集器: {}ms, 收集到 {} 项",
78 optimized_time.as_millis(), optimized_items.len());
79
80 let speedup = original_time.as_millis() as f64 / optimized_time.as_millis() as f64;
81 println!(" ✨ 性能提升: {:.2}x 倍速\n", speedup);
82
83 // 2. 内存管理测试
84 println!("💾 内存管理优化:");
85 let memory_manager = Arc::new(GlobalMemoryManager::new());
86
87 // 测试字符串池效率
88 let start = Instant::now();
89 let mut test_strings = Vec::new();
90 for i in 0..1000 {
91 let s = memory_manager.acquire_string(50);
92 test_strings.push(format!("Test string {}", i));
93 }
94
95 for s in test_strings {
96 memory_manager.release_string(s);
97 }
98 let pool_time = start.elapsed();
99
100 // 获取内存统计
101 let (pool_stats, _) = memory_manager.get_pool_stats();
102 println!(" - 字符串池操作: {}ms", pool_time.as_millis());
103 println!(" - 池大小: 小型={}, 大型={}, 总容量={}KB",
104 pool_stats.small_pool_size,
105 pool_stats.large_pool_size,
106 pool_stats.total_capacity / 1024);
107
108 // 3. 智能缓存测试
109 println!("\n🧠 智能缓存性能:");
110 use html_translation_lib::config::TranslationConfig;
111
112 let config = TranslationConfig::new()
113 .target_language("zh")
114 .api_url("http://example.com/translate");
115
116 let mut cache_manager = SmartCacheManager::new(&config).await?;
117
118 let start = Instant::now();
119
120 // 模拟缓存操作
121 for i in 0..500 {
122 let key = format!("text_{}", i % 100); // 重复键以测试缓存命中
123 let value = format!("translation_{}", i);
124 cache_manager.set(&key, value).await?;
125 }
126
127 // 测试缓存命中
128 let mut hits = 0;
129 for i in 0..100 {
130 let key = format!("text_{}", i);
131 if cache_manager.get(&key).await?.is_some() {
132 hits += 1;
133 }
134 }
135
136 let cache_time = start.elapsed();
137 let cache_stats = cache_manager.stats();
138
139 println!(" - 缓存操作时间: {}ms", cache_time.as_millis());
140 println!(" - 缓存命中数: {} / 100", hits);
141 println!(" - L1命中率: {:.1}%", cache_stats.l1_hit_rate() * 100.0);
142 println!(" - 总命中率: {:.1}%", cache_stats.hit_rate() * 100.0);
143
144 // 4. 并发批处理测试
145 println!("\n⚡ 并发批处理性能:");
146 let batch_config = BatchConfig {
147 batch_size: 20,
148 max_concurrency: 4,
149 timeout_duration: std::time::Duration::from_secs(10),
150 ..Default::default()
151 };
152
153 let batch_processor = ConcurrentBatchProcessor::new(
154 batch_config,
155 Arc::clone(&memory_manager)
156 );
157
158 // 准备测试文本
159 let test_texts: Vec<String> = (0..200)
160 .map(|i| format!("Test text number {}", i))
161 .collect();
162
163 let start = Instant::now();
164
165 // 提交批处理任务
166 let mut task_ids = Vec::new();
167 for chunk in test_texts.chunks(50) {
168 let task_id = batch_processor.submit_batch(
169 chunk.to_vec(),
170 Priority::Normal
171 ).await?;
172 task_ids.push(task_id);
173 }
174
175 // 模拟翻译函数
176 async fn mock_translate(texts: Vec<String>) -> html_translation_lib::error::TranslationResult<Vec<String>> {
177 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
178 Ok(texts.into_iter().map(|t| format!("翻译_{}", t)).collect())
179 }
180
181 // 处理队列
182 let _results = batch_processor.process_queue(mock_translate).await?;
183 let batch_time = start.elapsed();
184
185 let batch_stats = batch_processor.get_stats().await;
186 let queue_status = batch_processor.get_queue_status();
187
188 println!(" - 批处理总时间: {}ms", batch_time.as_millis());
189 println!(" - 处理成功率: {:.1}%", batch_stats.success_rate() * 100.0);
190 println!(" - 平均处理时间: {}ms", batch_stats.average_processing_time().as_millis());
191 println!(" - 吞吐量: {:.1} 任务/秒", batch_stats.throughput());
192 println!(" - 队列剩余: {} 任务", queue_status.total_tasks);
193
194 // 5. 总结
195 println!("\n📈 性能优化总结:");
196 println!(" ✅ DOM遍历优化: {:.1}x 性能提升", speedup);
197 println!(" ✅ 内存池管理: 减少 GC 压力");
198 println!(" ✅ 智能缓存: {:.1}% 命中率", cache_stats.hit_rate() * 100.0);
199 println!(" ✅ 并发批处理: {:.1}% 成功率", batch_stats.success_rate() * 100.0);
200 println!("\n🎉 所有性能优化测试完成!");
201
202 Ok(())
203}
Sourcepub fn throughput(&self) -> f64
pub fn throughput(&self) -> f64
计算吞吐量(任务/秒)
Examples found in repository?
examples/performance_test.rs (line 191)
51async fn main() -> Result<(), Box<dyn std::error::Error>> {
52 println!("🚀 HTML翻译库性能测试\n");
53
54 let large_html = generate_large_html();
55 let dom = parse_html(&large_html);
56
57 println!("📊 测试数据:");
58 println!(" - HTML大小: {} KB", large_html.len() / 1024);
59 println!(" - 包含 ~5000 个文本元素\n");
60
61 // 1. DOM遍历性能测试
62 println!("🔍 DOM遍历性能对比:");
63
64 // 原始递归收集器
65 let start = Instant::now();
66 let original_collector = TextCollector::new();
67 let original_items = original_collector.collect_from_dom(&dom)?;
68 let original_time = start.elapsed();
69 println!(" - 原始递归收集器: {}ms, 收集到 {} 项",
70 original_time.as_millis(), original_items.len());
71
72 // 优化的迭代收集器
73 let start = Instant::now();
74 let mut optimized_collector = OptimizedTextCollector::new();
75 let optimized_items = optimized_collector.collect_from_dom_optimized(&dom)?;
76 let optimized_time = start.elapsed();
77 println!(" - 优化迭代收集器: {}ms, 收集到 {} 项",
78 optimized_time.as_millis(), optimized_items.len());
79
80 let speedup = original_time.as_millis() as f64 / optimized_time.as_millis() as f64;
81 println!(" ✨ 性能提升: {:.2}x 倍速\n", speedup);
82
83 // 2. 内存管理测试
84 println!("💾 内存管理优化:");
85 let memory_manager = Arc::new(GlobalMemoryManager::new());
86
87 // 测试字符串池效率
88 let start = Instant::now();
89 let mut test_strings = Vec::new();
90 for i in 0..1000 {
91 let s = memory_manager.acquire_string(50);
92 test_strings.push(format!("Test string {}", i));
93 }
94
95 for s in test_strings {
96 memory_manager.release_string(s);
97 }
98 let pool_time = start.elapsed();
99
100 // 获取内存统计
101 let (pool_stats, _) = memory_manager.get_pool_stats();
102 println!(" - 字符串池操作: {}ms", pool_time.as_millis());
103 println!(" - 池大小: 小型={}, 大型={}, 总容量={}KB",
104 pool_stats.small_pool_size,
105 pool_stats.large_pool_size,
106 pool_stats.total_capacity / 1024);
107
108 // 3. 智能缓存测试
109 println!("\n🧠 智能缓存性能:");
110 use html_translation_lib::config::TranslationConfig;
111
112 let config = TranslationConfig::new()
113 .target_language("zh")
114 .api_url("http://example.com/translate");
115
116 let mut cache_manager = SmartCacheManager::new(&config).await?;
117
118 let start = Instant::now();
119
120 // 模拟缓存操作
121 for i in 0..500 {
122 let key = format!("text_{}", i % 100); // 重复键以测试缓存命中
123 let value = format!("translation_{}", i);
124 cache_manager.set(&key, value).await?;
125 }
126
127 // 测试缓存命中
128 let mut hits = 0;
129 for i in 0..100 {
130 let key = format!("text_{}", i);
131 if cache_manager.get(&key).await?.is_some() {
132 hits += 1;
133 }
134 }
135
136 let cache_time = start.elapsed();
137 let cache_stats = cache_manager.stats();
138
139 println!(" - 缓存操作时间: {}ms", cache_time.as_millis());
140 println!(" - 缓存命中数: {} / 100", hits);
141 println!(" - L1命中率: {:.1}%", cache_stats.l1_hit_rate() * 100.0);
142 println!(" - 总命中率: {:.1}%", cache_stats.hit_rate() * 100.0);
143
144 // 4. 并发批处理测试
145 println!("\n⚡ 并发批处理性能:");
146 let batch_config = BatchConfig {
147 batch_size: 20,
148 max_concurrency: 4,
149 timeout_duration: std::time::Duration::from_secs(10),
150 ..Default::default()
151 };
152
153 let batch_processor = ConcurrentBatchProcessor::new(
154 batch_config,
155 Arc::clone(&memory_manager)
156 );
157
158 // 准备测试文本
159 let test_texts: Vec<String> = (0..200)
160 .map(|i| format!("Test text number {}", i))
161 .collect();
162
163 let start = Instant::now();
164
165 // 提交批处理任务
166 let mut task_ids = Vec::new();
167 for chunk in test_texts.chunks(50) {
168 let task_id = batch_processor.submit_batch(
169 chunk.to_vec(),
170 Priority::Normal
171 ).await?;
172 task_ids.push(task_id);
173 }
174
175 // 模拟翻译函数
176 async fn mock_translate(texts: Vec<String>) -> html_translation_lib::error::TranslationResult<Vec<String>> {
177 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
178 Ok(texts.into_iter().map(|t| format!("翻译_{}", t)).collect())
179 }
180
181 // 处理队列
182 let _results = batch_processor.process_queue(mock_translate).await?;
183 let batch_time = start.elapsed();
184
185 let batch_stats = batch_processor.get_stats().await;
186 let queue_status = batch_processor.get_queue_status();
187
188 println!(" - 批处理总时间: {}ms", batch_time.as_millis());
189 println!(" - 处理成功率: {:.1}%", batch_stats.success_rate() * 100.0);
190 println!(" - 平均处理时间: {}ms", batch_stats.average_processing_time().as_millis());
191 println!(" - 吞吐量: {:.1} 任务/秒", batch_stats.throughput());
192 println!(" - 队列剩余: {} 任务", queue_status.total_tasks);
193
194 // 5. 总结
195 println!("\n📈 性能优化总结:");
196 println!(" ✅ DOM遍历优化: {:.1}x 性能提升", speedup);
197 println!(" ✅ 内存池管理: 减少 GC 压力");
198 println!(" ✅ 智能缓存: {:.1}% 命中率", cache_stats.hit_rate() * 100.0);
199 println!(" ✅ 并发批处理: {:.1}% 成功率", batch_stats.success_rate() * 100.0);
200 println!("\n🎉 所有性能优化测试完成!");
201
202 Ok(())
203}
Trait Implementations§
Source§impl Clone for BatchProcessingStats
impl Clone for BatchProcessingStats
Source§fn clone(&self) -> BatchProcessingStats
fn clone(&self) -> BatchProcessingStats
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for BatchProcessingStats
impl Debug for BatchProcessingStats
Source§impl Default for BatchProcessingStats
impl Default for BatchProcessingStats
Source§fn default() -> BatchProcessingStats
fn default() -> BatchProcessingStats
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for BatchProcessingStats
impl RefUnwindSafe for BatchProcessingStats
impl Send for BatchProcessingStats
impl Sync for BatchProcessingStats
impl Unpin for BatchProcessingStats
impl UnwindSafe for BatchProcessingStats
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more