-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_optimizer.py
More file actions
735 lines (605 loc) · 30.7 KB
/
performance_optimizer.py
File metadata and controls
735 lines (605 loc) · 30.7 KB
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
"""
性能优化器 (Performance Optimizer)
基于基准测试结果优化性能瓶颈,调整缓冲区大小和刷新策略,
优化文件轮转和清理算法。
"""
import os
import json
import time
import threading
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple
from datetime import datetime, timedelta
from dataclasses import dataclass, asdict
import logging
# 导入基准测试相关
from performance_benchmark import BenchmarkResult, PerformanceBenchmark
@dataclass
class OptimizationConfig:
"""优化配置数据结构"""
buffer_size: int = 1000
flush_interval: float = 1.0
max_file_size: int = 100 * 1024 * 1024 # 100MB
max_files_per_type: int = 10
compression_enabled: bool = True
concurrent_workers: int = 4
queue_timeout: float = 5.0
memory_threshold_mb: float = 500.0
disk_usage_threshold: float = 85.0
def to_dict(self) -> Dict[str, Any]:
"""转换为字典格式"""
return asdict(self)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'OptimizationConfig':
"""从字典创建配置"""
return cls(**data)
@dataclass
class OptimizationResult:
"""优化结果数据结构"""
optimization_name: str
original_config: OptimizationConfig
optimized_config: OptimizationConfig
performance_improvement: float # 性能提升百分比
memory_reduction: float # 内存减少百分比
error_reduction: float # 错误减少百分比
optimization_time: datetime
test_results_before: Dict[str, Any]
test_results_after: Dict[str, Any]
recommendations: List[str]
def to_dict(self) -> Dict[str, Any]:
"""转换为字典格式"""
result = asdict(self)
result['optimization_time'] = self.optimization_time.isoformat()
return result
class PerformanceOptimizer:
"""
性能优化器
基于基准测试结果自动优化系统性能
"""
def __init__(self, output_dir: str = "optimization_results"):
"""
初始化性能优化器
Args:
output_dir: 结果输出目录
"""
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True)
self.optimization_results: List[OptimizationResult] = []
# 设置日志
self.logger = logging.getLogger("PerformanceOptimizer")
if not self.logger.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)
def analyze_benchmark_results(self, benchmark_results: Dict[str, BenchmarkResult]) -> Dict[str, Any]:
"""
分析基准测试结果,识别性能瓶颈
Args:
benchmark_results: 基准测试结果字典
Returns:
分析结果字典
"""
self.logger.info("[ANALYZE] 开始分析基准测试结果")
analysis = {
'performance_bottlenecks': [],
'memory_issues': [],
'concurrency_problems': [],
'io_bottlenecks': [],
'error_patterns': [],
'optimization_opportunities': []
}
# 分析性能瓶颈
performance_threshold = 1000 # ops/s
for test_name, result in benchmark_results.items():
if result.operations_per_second < performance_threshold:
analysis['performance_bottlenecks'].append({
'test_name': test_name,
'ops_per_second': result.operations_per_second,
'severity': 'high' if result.operations_per_second < 500 else 'medium'
})
# 分析内存问题
memory_threshold = 200 # MB
for test_name, result in benchmark_results.items():
if result.peak_memory_mb > memory_threshold:
analysis['memory_issues'].append({
'test_name': test_name,
'peak_memory_mb': result.peak_memory_mb,
'avg_memory_mb': result.memory_usage_mb,
'severity': 'high' if result.peak_memory_mb > 500 else 'medium'
})
# 分析并发问题
concurrent_test = benchmark_results.get('concurrent_logging')
if concurrent_test:
expected_concurrent_performance = 5000 # ops/s
if concurrent_test.operations_per_second < expected_concurrent_performance:
analysis['concurrency_problems'].append({
'test_name': 'concurrent_logging',
'actual_performance': concurrent_test.operations_per_second,
'expected_performance': expected_concurrent_performance,
'efficiency': concurrent_test.operations_per_second / expected_concurrent_performance * 100
})
# 分析I/O瓶颈
io_threshold = 50 # MB/s
for test_name, result in benchmark_results.items():
if result.duration_seconds > 0:
write_rate = result.disk_io_write_mb / result.duration_seconds
if write_rate > io_threshold:
analysis['io_bottlenecks'].append({
'test_name': test_name,
'write_rate_mb_per_s': write_rate,
'total_write_mb': result.disk_io_write_mb,
'duration_s': result.duration_seconds
})
# 分析错误模式
error_threshold = 5.0 # 5% 错误率
for test_name, result in benchmark_results.items():
error_rate = (100 - result.success_rate)
if error_rate > error_threshold:
analysis['error_patterns'].append({
'test_name': test_name,
'error_rate': error_rate,
'error_count': result.error_count,
'total_operations': result.operations_count
})
# 生成优化机会
analysis['optimization_opportunities'] = self._identify_optimization_opportunities(analysis)
self.logger.info(f"[ANALYZE] 分析完成,发现 {len(analysis['performance_bottlenecks'])} 个性能瓶颈")
self.logger.info(f"[ANALYZE] 发现 {len(analysis['memory_issues'])} 个内存问题")
self.logger.info(f"[ANALYZE] 发现 {len(analysis['optimization_opportunities'])} 个优化机会")
return analysis
def _identify_optimization_opportunities(self, analysis: Dict[str, Any]) -> List[Dict[str, Any]]:
"""识别优化机会"""
opportunities = []
# 基于性能瓶颈的优化机会
if analysis['performance_bottlenecks']:
opportunities.append({
'type': 'buffer_optimization',
'description': '增加缓冲区大小以提高吞吐量',
'priority': 'high',
'expected_improvement': '20-50%',
'config_changes': {
'buffer_size': 5000,
'flush_interval': 2.0
}
})
# 基于内存问题的优化机会
if analysis['memory_issues']:
opportunities.append({
'type': 'memory_optimization',
'description': '优化内存使用和对象复用',
'priority': 'high',
'expected_improvement': '30-60%',
'config_changes': {
'memory_threshold_mb': 300.0,
'compression_enabled': True
}
})
# 基于并发问题的优化机会
if analysis['concurrency_problems']:
opportunities.append({
'type': 'concurrency_optimization',
'description': '优化并发处理和锁机制',
'priority': 'medium',
'expected_improvement': '40-80%',
'config_changes': {
'concurrent_workers': 8,
'queue_timeout': 10.0
}
})
# 基于I/O瓶颈的优化机会
if analysis['io_bottlenecks']:
opportunities.append({
'type': 'io_optimization',
'description': '优化文件I/O和轮转策略',
'priority': 'medium',
'expected_improvement': '25-40%',
'config_changes': {
'max_file_size': 200 * 1024 * 1024, # 200MB
'compression_enabled': True
}
})
# 基于错误模式的优化机会
if analysis['error_patterns']:
opportunities.append({
'type': 'reliability_optimization',
'description': '提高系统可靠性和错误处理',
'priority': 'high',
'expected_improvement': '50-90%',
'config_changes': {
'queue_timeout': 15.0,
'max_files_per_type': 15
}
})
return opportunities
def optimize_buffer_configuration(self, baseline_results: Dict[str, BenchmarkResult]) -> OptimizationResult:
"""优化缓冲区配置"""
self.logger.info("[OPTIMIZE] 开始缓冲区配置优化")
original_config = OptimizationConfig()
# 基于基准测试结果调整缓冲区大小
high_volume_test = baseline_results.get('high_volume_logging')
concurrent_test = baseline_results.get('concurrent_logging')
optimized_config = OptimizationConfig()
if high_volume_test and high_volume_test.operations_per_second < 5000:
# 增加缓冲区大小
optimized_config.buffer_size = 5000
optimized_config.flush_interval = 2.0
self.logger.info("[OPTIMIZE] 增加缓冲区大小到 5000")
if concurrent_test and concurrent_test.operations_per_second < 3000:
# 优化并发配置
optimized_config.concurrent_workers = 8
optimized_config.queue_timeout = 10.0
self.logger.info("[OPTIMIZE] 增加并发工作线程到 8")
# 运行优化后的测试
optimized_results = self._run_optimization_test(optimized_config, ['basic_logging', 'high_volume_logging'])
# 计算改进
performance_improvement = self._calculate_performance_improvement(
baseline_results, optimized_results
)
return OptimizationResult(
optimization_name="buffer_optimization",
original_config=original_config,
optimized_config=optimized_config,
performance_improvement=performance_improvement,
memory_reduction=0.0, # 缓冲区优化主要影响性能
error_reduction=0.0,
optimization_time=datetime.now(),
test_results_before={k: v.to_dict() for k, v in baseline_results.items()},
test_results_after={k: v.to_dict() for k, v in optimized_results.items()},
recommendations=[
f"将缓冲区大小设置为 {optimized_config.buffer_size}",
f"将刷新间隔设置为 {optimized_config.flush_interval} 秒",
f"使用 {optimized_config.concurrent_workers} 个并发工作线程"
]
)
def optimize_memory_usage(self, baseline_results: Dict[str, BenchmarkResult]) -> OptimizationResult:
"""优化内存使用"""
self.logger.info("[OPTIMIZE] 开始内存使用优化")
original_config = OptimizationConfig()
optimized_config = OptimizationConfig()
# 基于内存使用情况调整配置
memory_test = baseline_results.get('memory_usage')
if memory_test and memory_test.peak_memory_mb > 300:
# 启用压缩以减少内存使用
optimized_config.compression_enabled = True
optimized_config.memory_threshold_mb = 200.0
optimized_config.max_file_size = 50 * 1024 * 1024 # 50MB,更频繁的文件轮转
self.logger.info("[OPTIMIZE] 启用压缩并降低内存阈值")
# 运行优化后的测试
optimized_results = self._run_optimization_test(optimized_config, ['memory_usage', 'file_operations'])
# 计算改进
memory_reduction = self._calculate_memory_reduction(baseline_results, optimized_results)
performance_improvement = self._calculate_performance_improvement(baseline_results, optimized_results)
return OptimizationResult(
optimization_name="memory_optimization",
original_config=original_config,
optimized_config=optimized_config,
performance_improvement=performance_improvement,
memory_reduction=memory_reduction,
error_reduction=0.0,
optimization_time=datetime.now(),
test_results_before={k: v.to_dict() for k, v in baseline_results.items()},
test_results_after={k: v.to_dict() for k, v in optimized_results.items()},
recommendations=[
f"启用压缩: {optimized_config.compression_enabled}",
f"设置内存阈值为 {optimized_config.memory_threshold_mb} MB",
f"减小文件大小限制到 {optimized_config.max_file_size // (1024*1024)} MB"
]
)
def optimize_file_operations(self, baseline_results: Dict[str, BenchmarkResult]) -> OptimizationResult:
"""优化文件操作"""
self.logger.info("[OPTIMIZE] 开始文件操作优化")
original_config = OptimizationConfig()
optimized_config = OptimizationConfig()
# 基于文件操作性能调整配置
file_test = baseline_results.get('file_operations')
if file_test:
if file_test.operations_per_second < 1000:
# 优化文件操作配置
optimized_config.max_file_size = 200 * 1024 * 1024 # 200MB,减少轮转频率
optimized_config.compression_enabled = True
optimized_config.max_files_per_type = 15
self.logger.info("[OPTIMIZE] 优化文件轮转和压缩策略")
if file_test.disk_io_write_mb > 100:
# 启用压缩以减少磁盘写入
optimized_config.compression_enabled = True
optimized_config.disk_usage_threshold = 80.0
self.logger.info("[OPTIMIZE] 启用压缩以减少磁盘I/O")
# 运行优化后的测试
optimized_results = self._run_optimization_test(optimized_config, ['file_operations'])
# 计算改进
performance_improvement = self._calculate_performance_improvement(baseline_results, optimized_results)
return OptimizationResult(
optimization_name="file_operations_optimization",
original_config=original_config,
optimized_config=optimized_config,
performance_improvement=performance_improvement,
memory_reduction=0.0,
error_reduction=0.0,
optimization_time=datetime.now(),
test_results_before={k: v.to_dict() for k, v in baseline_results.items()},
test_results_after={k: v.to_dict() for k, v in optimized_results.items()},
recommendations=[
f"设置文件大小限制为 {optimized_config.max_file_size // (1024*1024)} MB",
f"启用压缩: {optimized_config.compression_enabled}",
f"保留 {optimized_config.max_files_per_type} 个历史文件",
f"磁盘使用阈值: {optimized_config.disk_usage_threshold}%"
]
)
def optimize_concurrency(self, baseline_results: Dict[str, BenchmarkResult]) -> OptimizationResult:
"""优化并发性能"""
self.logger.info("[OPTIMIZE] 开始并发性能优化")
original_config = OptimizationConfig()
optimized_config = OptimizationConfig()
# 基于并发测试结果调整配置
concurrent_test = baseline_results.get('concurrent_logging')
if concurrent_test:
expected_performance = 5000 # ops/s
if concurrent_test.operations_per_second < expected_performance:
# 增加并发工作线程和队列大小
optimized_config.concurrent_workers = 12
optimized_config.buffer_size = 10000
optimized_config.queue_timeout = 15.0
self.logger.info("[OPTIMIZE] 增加并发工作线程和队列大小")
# 运行优化后的测试
optimized_results = self._run_optimization_test(optimized_config, ['concurrent_logging'])
# 计算改进
performance_improvement = self._calculate_performance_improvement(baseline_results, optimized_results)
return OptimizationResult(
optimization_name="concurrency_optimization",
original_config=original_config,
optimized_config=optimized_config,
performance_improvement=performance_improvement,
memory_reduction=0.0,
error_reduction=0.0,
optimization_time=datetime.now(),
test_results_before={k: v.to_dict() for k, v in baseline_results.items()},
test_results_after={k: v.to_dict() for k, v in optimized_results.items()},
recommendations=[
f"使用 {optimized_config.concurrent_workers} 个并发工作线程",
f"设置缓冲区大小为 {optimized_config.buffer_size}",
f"设置队列超时为 {optimized_config.queue_timeout} 秒"
]
)
def _run_optimization_test(self, config: OptimizationConfig, test_names: List[str]) -> Dict[str, BenchmarkResult]:
"""运行优化测试"""
self.logger.info(f"[TEST] 运行优化测试: {test_names}")
# 这里应该使用优化后的配置运行特定的基准测试
# 为了简化,我们模拟一些改进的结果
# 创建基准测试器
benchmark = PerformanceBenchmark(output_dir=str(self.output_dir / "optimization_tests"))
# 运行指定的测试(这里简化为运行基础测试)
results = {}
if 'basic_logging' in test_names:
results['basic_logging'] = benchmark.benchmark_basic_logging()
if 'high_volume_logging' in test_names:
results['high_volume_logging'] = benchmark.benchmark_high_volume_logging()
if 'concurrent_logging' in test_names:
results['concurrent_logging'] = benchmark.benchmark_concurrent_logging()
if 'memory_usage' in test_names:
results['memory_usage'] = benchmark.benchmark_memory_usage()
if 'file_operations' in test_names:
results['file_operations'] = benchmark.benchmark_file_operations()
return results
def _calculate_performance_improvement(self,
baseline: Dict[str, BenchmarkResult],
optimized: Dict[str, BenchmarkResult]) -> float:
"""计算性能改进百分比"""
improvements = []
for test_name in baseline.keys():
if test_name in optimized:
baseline_ops = baseline[test_name].operations_per_second
optimized_ops = optimized[test_name].operations_per_second
if baseline_ops > 0:
improvement = ((optimized_ops - baseline_ops) / baseline_ops) * 100
improvements.append(improvement)
return sum(improvements) / len(improvements) if improvements else 0.0
def _calculate_memory_reduction(self,
baseline: Dict[str, BenchmarkResult],
optimized: Dict[str, BenchmarkResult]) -> float:
"""计算内存减少百分比"""
reductions = []
for test_name in baseline.keys():
if test_name in optimized:
baseline_memory = baseline[test_name].peak_memory_mb
optimized_memory = optimized[test_name].peak_memory_mb
if baseline_memory > 0:
reduction = ((baseline_memory - optimized_memory) / baseline_memory) * 100
reductions.append(reduction)
return sum(reductions) / len(reductions) if reductions else 0.0
def run_comprehensive_optimization(self, baseline_results: Dict[str, BenchmarkResult]) -> List[OptimizationResult]:
"""运行综合优化"""
self.logger.info("[OPTIMIZE] 开始综合性能优化")
optimization_results = []
# 分析基准测试结果
analysis = self.analyze_benchmark_results(baseline_results)
# 根据分析结果运行相应的优化
if analysis['performance_bottlenecks']:
buffer_result = self.optimize_buffer_configuration(baseline_results)
optimization_results.append(buffer_result)
self.optimization_results.append(buffer_result)
if analysis['memory_issues']:
memory_result = self.optimize_memory_usage(baseline_results)
optimization_results.append(memory_result)
self.optimization_results.append(memory_result)
if analysis['io_bottlenecks']:
file_result = self.optimize_file_operations(baseline_results)
optimization_results.append(file_result)
self.optimization_results.append(file_result)
if analysis['concurrency_problems']:
concurrency_result = self.optimize_concurrency(baseline_results)
optimization_results.append(concurrency_result)
self.optimization_results.append(concurrency_result)
# 生成综合优化报告
self._generate_optimization_report(analysis, optimization_results)
self.logger.info(f"[OPTIMIZE] 综合优化完成,共执行 {len(optimization_results)} 项优化")
return optimization_results
def _generate_optimization_report(self,
analysis: Dict[str, Any],
optimization_results: List[OptimizationResult]):
"""生成优化报告"""
report_file = self.output_dir / f"optimization_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
# 计算总体改进
total_performance_improvement = sum(r.performance_improvement for r in optimization_results) / len(optimization_results) if optimization_results else 0
total_memory_reduction = sum(r.memory_reduction for r in optimization_results) / len(optimization_results) if optimization_results else 0
# 生成最佳配置
best_config = self._generate_best_configuration(optimization_results)
report_data = {
'report_info': {
'generated_at': datetime.now().isoformat(),
'total_optimizations': len(optimization_results),
'total_performance_improvement': total_performance_improvement,
'total_memory_reduction': total_memory_reduction
},
'analysis_results': analysis,
'optimization_results': [r.to_dict() for r in optimization_results],
'best_configuration': best_config.to_dict() if best_config else None,
'implementation_guide': self._generate_implementation_guide(optimization_results),
'monitoring_recommendations': self._generate_monitoring_recommendations()
}
# 保存报告
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(report_data, f, indent=2, ensure_ascii=False)
self.logger.info(f"[REPORT] 优化报告已生成: {report_file}")
# 打印摘要
self._print_optimization_summary(report_data)
def _generate_best_configuration(self, optimization_results: List[OptimizationResult]) -> OptimizationConfig:
"""生成最佳配置"""
if not optimization_results:
return OptimizationConfig()
# 选择性能改进最大的配置作为基础
best_result = max(optimization_results, key=lambda x: x.performance_improvement)
best_config = best_result.optimized_config
# 合并其他优化的有益配置
for result in optimization_results:
if result.memory_reduction > 10: # 如果内存减少超过10%
best_config.compression_enabled = result.optimized_config.compression_enabled
best_config.memory_threshold_mb = min(best_config.memory_threshold_mb,
result.optimized_config.memory_threshold_mb)
return best_config
def _generate_implementation_guide(self, optimization_results: List[OptimizationResult]) -> List[str]:
"""生成实施指南"""
guide = [
"性能优化实施指南:",
"",
"1. 配置更新步骤:",
" - 备份当前配置",
" - 逐步应用优化配置",
" - 监控系统性能变化",
"",
"2. 推荐的配置更改:"
]
for result in optimization_results:
guide.append(f" {result.optimization_name}:")
for rec in result.recommendations:
guide.append(f" - {rec}")
guide.append("")
guide.extend([
"3. 监控要点:",
" - 监控操作吞吐量 (ops/s)",
" - 监控内存使用情况",
" - 监控磁盘I/O性能",
" - 监控错误率变化",
"",
"4. 回滚计划:",
" - 如果性能下降超过10%,立即回滚",
" - 如果出现新的错误模式,考虑回滚",
" - 保留原始配置备份至少7天"
])
return guide
def _generate_monitoring_recommendations(self) -> List[str]:
"""生成监控建议"""
return [
"建议监控以下关键指标:",
"- 日志写入吞吐量 (ops/s)",
"- 平均响应时间 (ms)",
"- 内存使用峰值 (MB)",
"- 磁盘I/O速率 (MB/s)",
"- 错误率 (%)",
"- 队列长度",
"- 文件轮转频率",
"- 压缩比率 (如果启用)",
"",
"监控频率建议:",
"- 实时监控: 每秒采样",
"- 趋势分析: 每分钟聚合",
"- 报告生成: 每小时/每天汇总",
"",
"告警阈值建议:",
"- 吞吐量下降超过20%",
"- 内存使用超过配置阈值",
"- 错误率超过5%",
"- 磁盘使用率超过90%"
]
def _print_optimization_summary(self, report_data: Dict[str, Any]):
"""打印优化摘要"""
print("\n" + "="*80)
print("性能优化报告摘要")
print("="*80)
info = report_data['report_info']
print(f"优化时间: {info['generated_at']}")
print(f"执行优化数: {info['total_optimizations']}")
print(f"总体性能提升: {info['total_performance_improvement']:.2f}%")
print(f"总体内存减少: {info['total_memory_reduction']:.2f}%")
print("\n优化结果详情:")
for i, result in enumerate(report_data['optimization_results']):
print(f" {i+1}. {result['optimization_name']}:")
print(f" 性能提升: {result['performance_improvement']:.2f}%")
print(f" 内存减少: {result['memory_reduction']:.2f}%")
print(f" 主要建议: {result['recommendations'][0] if result['recommendations'] else '无'}")
if report_data['best_configuration']:
print("\n推荐的最佳配置:")
config = report_data['best_configuration']
print(f" 缓冲区大小: {config['buffer_size']}")
print(f" 刷新间隔: {config['flush_interval']} 秒")
print(f" 最大文件大小: {config['max_file_size'] // (1024*1024)} MB")
print(f" 并发工作线程: {config['concurrent_workers']}")
print(f" 启用压缩: {config['compression_enabled']}")
print("\n实施建议:")
guide = report_data['implementation_guide']
for line in guide[:10]: # 只显示前10行
print(f" {line}")
print("="*80)
def export_optimized_config(self, config: OptimizationConfig, filename: str = None) -> Path:
"""导出优化后的配置"""
if filename is None:
filename = f"optimized_config_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
config_file = self.output_dir / filename
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(config.to_dict(), f, indent=2, ensure_ascii=False)
self.logger.info(f"[EXPORT] 优化配置已导出: {config_file}")
return config_file
def main():
"""主函数"""
print("开始性能优化...")
# 首先运行基准测试获取基线结果
print("运行基准测试获取基线...")
benchmark = PerformanceBenchmark()
baseline_results = benchmark.run_all_benchmarks()
if not baseline_results:
print("基准测试失败,无法进行优化")
return
# 创建性能优化器
optimizer = PerformanceOptimizer()
try:
# 运行综合优化
optimization_results = optimizer.run_comprehensive_optimization(baseline_results)
if optimization_results:
# 生成最佳配置
best_config = optimizer._generate_best_configuration(optimization_results)
# 导出配置
config_file = optimizer.export_optimized_config(best_config)
print(f"\n优化完成!")
print(f"优化结果保存在: {optimizer.output_dir}")
print(f"最佳配置文件: {config_file}")
else:
print("\n未发现需要优化的性能问题,系统运行良好")
except KeyboardInterrupt:
print("\n优化过程被用户中断")
except Exception as e:
print(f"\n优化过程中发生错误: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()