Files
ai_wht_B/查看综合测试报表.py

234 lines
6.9 KiB
Python
Raw Permalink Normal View History

2026-01-06 14:18:39 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
综合测试报表查看器
展示所有模块的测试结果统计和详细数据
"""
import os
import csv
from datetime import datetime
from collections import defaultdict
def read_csv_file(filename):
"""读取CSV文件"""
if not os.path.exists(filename):
return None
try:
with open(filename, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
return list(reader)
except Exception as e:
try:
with open(filename, 'r', encoding='gbk') as f:
reader = csv.DictReader(f)
return list(reader)
except:
return None
def analyze_performance_data(data):
"""分析性能数据"""
if not data:
return None
stats = {
'total': 0,
'success': 0,
'failed': 0,
'avg_time': 0,
'max_time': 0,
'min_time': float('inf'),
'max_endpoint': '',
'min_endpoint': '',
'error_count': 0
}
total_time = 0
for row in data:
stats['total'] += 1
# 成功率统计
success = row.get('success', '').upper() == 'TRUE'
if success:
stats['success'] += 1
else:
stats['failed'] += 1
# 时间统计
try:
elapsed_time = float(row.get('elapsed_time_ms', 0))
total_time += elapsed_time
if elapsed_time > stats['max_time']:
stats['max_time'] = elapsed_time
stats['max_endpoint'] = row.get('endpoint', '')
if elapsed_time < stats['min_time'] and elapsed_time > 0:
stats['min_time'] = elapsed_time
stats['min_endpoint'] = row.get('endpoint', '')
except:
pass
# 错误统计
if row.get('error'):
stats['error_count'] += 1
if stats['total'] > 0:
stats['avg_time'] = total_time / stats['total']
stats['success_rate'] = (stats['success'] / stats['total']) * 100
if stats['min_time'] == float('inf'):
stats['min_time'] = 0
return stats
def print_module_stats(module_name, stats):
"""打印模块统计信息"""
if not stats:
print(f"{module_name}: 无数据")
return
print(f"\n{'='*60}")
print(f"📊 {module_name}")
print(f"{'='*60}")
print(f" 测试总数: {stats['total']}")
print(f" 成功数量: {stats['success']} 个 ✓")
print(f" 失败数量: {stats['failed']} 个 ✗")
print(f" 成功率: {stats['success_rate']:.2f}%")
print(f" 平均耗时: {stats['avg_time']:.2f}ms")
print(f" 最大耗时: {stats['max_time']:.2f}ms")
print(f" └─ 接口: {stats['max_endpoint']}")
print(f" 最小耗时: {stats['min_time']:.2f}ms")
print(f" └─ 接口: {stats['min_endpoint']}")
if stats['error_count'] > 0:
print(f" ⚠️ 错误数量: {stats['error_count']}")
def analyze_api_docs(filename):
"""分析接口文档"""
data = read_csv_file(filename)
if not data:
return None
stats = {
'total': len(data),
'modules': defaultdict(int),
'methods': defaultdict(int),
'auth_required': 0
}
for row in data:
module = row.get('module', '未知')
method = row.get('method', 'GET')
auth = row.get('auth', '')
stats['modules'][module] += 1
stats['methods'][method] += 1
if auth == '需要':
stats['auth_required'] += 1
return stats
def print_api_doc_stats(module_name, stats):
"""打印接口文档统计"""
if not stats:
print(f"{module_name} 文档: 无数据")
return
print(f"\n{'='*60}")
print(f"📚 {module_name} - 接口文档")
print(f"{'='*60}")
print(f" 接口总数: {stats['total']}")
print(f" 需要认证: {stats['auth_required']}")
print(f"\n 按模块分类:")
for module, count in sorted(stats['modules'].items()):
print(f"{module}: {count}")
print(f"\n 按方法分类:")
for method, count in sorted(stats['methods'].items()):
print(f"{method}: {count}")
def main():
"""主函数"""
print("\n" + "="*80)
print("🎯 AI文章生成平台 - 综合测试报表")
print("="*80)
print(f"📅 生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# 定义要分析的模块
modules = [
('基础接口', '基础接口'),
('认证接口', '认证接口'),
('工作台接口', '工作台'),
('企业接口', '企业接口'),
('员工接口', '员工接口'),
('图片接口', '图片接口'),
('日志接口', '日志接口'),
('文章接口', '文章接口'),
('作者接口', '作者接口'),
('整体后端接口', '整体后端接口')
]
today = datetime.now().strftime('%Y%m%d')
# 汇总统计
total_stats = {
'total_tests': 0,
'total_success': 0,
'total_failed': 0,
'total_apis': 0
}
print("\n" + ""*80)
print("📈 性能测试报告")
print(""*80)
# 分析每个模块的性能数据
for display_name, module_name in modules:
perf_file = f'{module_name}性能测试_{today}.csv'
data = read_csv_file(perf_file)
if data:
stats = analyze_performance_data(data)
if stats:
print_module_stats(display_name, stats)
total_stats['total_tests'] += stats['total']
total_stats['total_success'] += stats['success']
total_stats['total_failed'] += stats['failed']
# 分析接口文档
print("\n\n" + ""*80)
print("📚 接口文档统计")
print(""*80)
for display_name, module_name in modules:
doc_file = f'{module_name}文档_{today}.csv'
stats = analyze_api_docs(doc_file)
if stats:
print_api_doc_stats(display_name, stats)
total_stats['total_apis'] += stats['total']
# 打印总体统计
print("\n\n" + ""*80)
print("🎊 总体统计")
print(""*80)
print(f"\n 📊 性能测试:")
print(f" • 测试总数: {total_stats['total_tests']}")
print(f" • 成功: {total_stats['total_success']} 个 ✓")
print(f" • 失败: {total_stats['total_failed']} 个 ✗")
if total_stats['total_tests'] > 0:
success_rate = (total_stats['total_success'] / total_stats['total_tests']) * 100
print(f" • 总体成功率: {success_rate:.2f}%")
print(f"\n 📚 接口文档:")
print(f" • 接口总数: {total_stats['total_apis']}")
print("\n" + "="*80)
print("✅ 报表生成完成")
print("="*80 + "\n")
if __name__ == '__main__':
main()