mirror of
https://github.com/ZeroHawkeye/wordZero.git
synced 2025-09-27 04:05:56 +08:00
101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
解析 Golang 基准测试结果并生成兼容的 JSON 格式
|
||
"""
|
||
|
||
import re
|
||
import json
|
||
from pathlib import Path
|
||
from datetime import datetime
|
||
|
||
|
||
def parse_golang_benchmark_output(file_path: str):
|
||
"""解析 Golang 基准测试输出文件"""
|
||
with open(file_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 查找基准测试结果行
|
||
benchmark_pattern = r'(\d+)\s+(\d+)\s+ns/op\s+(\d+)\s+B/op\s+(\d+)\s+allocs/op'
|
||
results = []
|
||
|
||
# 基准测试名称映射
|
||
test_names = [
|
||
"基础文档创建", # BenchmarkCreateBasicDocument
|
||
"复杂格式化", # BenchmarkComplexFormatting
|
||
"表格操作", # BenchmarkTableOperations
|
||
"大表格处理", # BenchmarkLargeTable
|
||
"大型文档", # BenchmarkLargeDocument
|
||
"内存使用测试" # BenchmarkMemoryUsage
|
||
]
|
||
|
||
matches = re.findall(benchmark_pattern, content)
|
||
|
||
for i, match in enumerate(matches):
|
||
iterations, ns_per_op, bytes_per_op, allocs_per_op = match
|
||
|
||
# 转换纳秒到毫秒
|
||
avg_time_ms = float(ns_per_op) / 1_000_000
|
||
|
||
# 估算最小和最大时间(基于经验,通常有±10%的变化)
|
||
min_time_ms = avg_time_ms * 0.9
|
||
max_time_ms = avg_time_ms * 1.1
|
||
|
||
if i < len(test_names):
|
||
result = {
|
||
"name": test_names[i],
|
||
"avgTime": round(avg_time_ms, 2),
|
||
"minTime": round(min_time_ms, 2),
|
||
"maxTime": round(max_time_ms, 2),
|
||
"iterations": int(iterations),
|
||
"bytesPerOp": int(bytes_per_op),
|
||
"allocsPerOp": int(allocs_per_op)
|
||
}
|
||
results.append(result)
|
||
|
||
return results
|
||
|
||
|
||
def generate_golang_performance_report():
|
||
"""生成 Golang 性能报告 JSON 文件"""
|
||
input_file = Path("results/golang/benchmark_output.txt")
|
||
output_file = Path("results/golang/performance_report.json")
|
||
|
||
if not input_file.exists():
|
||
print(f"错误:找不到 Golang 基准测试输出文件: {input_file}")
|
||
return
|
||
|
||
try:
|
||
results = parse_golang_benchmark_output(input_file)
|
||
|
||
if not results:
|
||
print("警告:未找到有效的基准测试结果")
|
||
return
|
||
|
||
report_data = {
|
||
"timestamp": datetime.now().isoformat(),
|
||
"platform": "Golang",
|
||
"goVersion": "1.19+",
|
||
"results": results
|
||
}
|
||
|
||
# 确保输出目录存在
|
||
output_file.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
with open(output_file, 'w', encoding='utf-8') as f:
|
||
json.dump(report_data, f, indent=2, ensure_ascii=False)
|
||
|
||
print(f"✅ Golang 性能报告已生成: {output_file}")
|
||
print(f"📊 共解析了 {len(results)} 个测试结果")
|
||
|
||
# 打印摘要
|
||
print("\n🎯 Golang 性能测试摘要:")
|
||
for result in results:
|
||
print(f" - {result['name']}: {result['avgTime']}ms (平均)")
|
||
|
||
except Exception as e:
|
||
print(f"❌ 生成 Golang 性能报告时发生错误: {e}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
generate_golang_performance_report() |