feat(性能分析): 实现性能分析工具模块并添加相关测试
添加性能分析工具模块,包括时间测量、内存分析和性能统计功能 添加测试文件和示例配置,完善性能分析工具的使用场景 在工具模块中实现单例装饰器并导出到__init__.py
This commit is contained in:
94
profile_main.py
Normal file
94
profile_main.py
Normal file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
性能分析入口文件
|
||||
|
||||
用于启动带有性能分析功能的应用程序。
|
||||
|
||||
使用方法:
|
||||
python profile_main.py [options]
|
||||
|
||||
选项:
|
||||
-h, --help 显示帮助信息
|
||||
--profile, -p 启用详细性能分析(使用 pyinstrument)
|
||||
--memory, -m 启用内存使用分析
|
||||
--output, -o FILE 性能分析报告输出文件(HTML格式)
|
||||
--threshold, -t SEC 设置性能监控阈值(秒)
|
||||
--stats, -s 在程序结束时输出性能统计报告
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import os
|
||||
|
||||
# 将项目根目录添加到 sys.path
|
||||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, ROOT_DIR)
|
||||
|
||||
# 解析命令行参数
|
||||
parser = argparse.ArgumentParser(description='性能分析入口文件')
|
||||
parser.add_argument('--profile', '-p', action='store_true', help='启用详细性能分析(使用 pyinstrument)')
|
||||
parser.add_argument('--memory', '-m', action='store_true', help='启用内存使用分析')
|
||||
parser.add_argument('--output', '-o', type=str, default='performance_report.html', help='性能分析报告输出文件(HTML格式)')
|
||||
parser.add_argument('--threshold', '-t', type=float, default=0.5, help='设置性能监控阈值(秒)')
|
||||
parser.add_argument('--stats', '-s', action='store_true', help='在程序结束时输出性能统计报告')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# 设置全局性能分析配置
|
||||
os.environ['PERFORMANCE_PROFILE'] = '1' if args.profile else '0'
|
||||
os.environ['PERFORMANCE_MEMORY'] = '1' if args.memory else '0'
|
||||
os.environ['PERFORMANCE_OUTPUT'] = args.output
|
||||
os.environ['PERFORMANCE_THRESHOLD'] = str(args.threshold)
|
||||
os.environ['PERFORMANCE_STATS'] = '1' if args.stats else '0'
|
||||
|
||||
# 导入并运行主程序
|
||||
from core.utils.performance import profile, aprofile
|
||||
from main import main
|
||||
import asyncio
|
||||
|
||||
async def main_with_profile():
|
||||
"""
|
||||
带有性能分析的主函数入口
|
||||
"""
|
||||
if args.profile:
|
||||
# 使用 pyinstrument 进行详细性能分析
|
||||
from pyinstrument import Profiler
|
||||
from pyinstrument.renderers import HTMLRenderer
|
||||
|
||||
profiler = Profiler()
|
||||
profiler.start()
|
||||
|
||||
try:
|
||||
await main()
|
||||
finally:
|
||||
profiler.stop()
|
||||
|
||||
# 输出分析结果到控制台
|
||||
print("\n" + "=" * 80)
|
||||
print("性能分析结果")
|
||||
print("=" * 80)
|
||||
print(profiler.print())
|
||||
|
||||
# 保存HTML报告
|
||||
try:
|
||||
html = profiler.render(HTMLRenderer())
|
||||
with open(args.output, 'w', encoding='utf-8') as f:
|
||||
f.write(html)
|
||||
print(f"\n性能分析报告已保存到: {args.output}")
|
||||
except Exception as e:
|
||||
print(f"\n保存性能分析报告失败: {e}")
|
||||
else:
|
||||
# 不使用详细分析,直接运行
|
||||
await main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(main_with_profile())
|
||||
finally:
|
||||
# 输出性能统计报告
|
||||
if args.stats:
|
||||
from core.utils.performance import performance_stats
|
||||
print("\n" + "=" * 80)
|
||||
print("性能统计报告")
|
||||
print("=" * 80)
|
||||
print(performance_stats.report())
|
||||
Reference in New Issue
Block a user