Skip to content

Commit 97710a9

Browse files
authored
(improve)(benchmark) improve benchmark, add analysis of parsing results (#2215)
1 parent 0ab7643 commit 97710a9

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ chm_db/
2020
__pycache__/
2121
/dict
2222
assembly/build/*-SNAPSHOT
23-
**/node_modules/
23+
**/node_modules/
24+
benchmark/res/

benchmark/benchmark.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,68 @@
1515
import time
1616
import jwt
1717
import traceback
18+
import os
19+
from datetime import datetime
20+
21+
22+
class DataFrameAppender:
23+
def __init__(self,file_name = "output"):
24+
# 定义表头
25+
columns = ['问题', '解析状态', '解析耗时', '执行状态', '执行耗时', '总耗时']
26+
# 创建只有表头的 DataFrame
27+
self.df = pd.DataFrame(columns=columns)
28+
self.file_name = file_name
29+
30+
def append_data(self, new_data):
31+
# 假设 new_data 是一维数组,将其转换为字典
32+
columns = ['问题', '解析状态', '解析耗时', '执行状态', '执行耗时', '总耗时']
33+
new_dict = dict(zip(columns, new_data))
34+
# 使用 loc 方法追加数据
35+
self.df.loc[len(self.df)] = new_dict
36+
def print_analysis_result(self):
37+
# 测试样例总数
38+
total_samples = len(self.df)
39+
40+
# 解析成功数量
41+
parse_success_count = (self.df['解析状态'] == '解析成功').sum()
42+
43+
# 执行成功数量
44+
execute_success_count = (self.df['执行状态'] == '执行成功').sum()
45+
46+
# 解析平均耗时,保留两位小数
47+
avg_parse_time = round(self.df['解析耗时'].mean(), 2)
48+
49+
# 执行平均耗时,保留两位小数
50+
avg_execute_time = round(self.df['执行耗时'].mean(), 2)
51+
52+
# 总平均耗时,保留两位小数
53+
avg_total_time = round(self.df['总耗时'].mean(), 2)
54+
55+
# 最长耗时,保留两位小数
56+
max_time = round(self.df['总耗时'].max(), 2)
57+
58+
# 最短耗时,保留两位小数
59+
min_time = round(self.df['总耗时'].min(), 2)
60+
61+
print(f"测试样例总数 : {total_samples}")
62+
print(f"解析成功数量 : {parse_success_count}")
63+
print(f"执行成功数量 : {execute_success_count}")
64+
print(f"解析平均耗时 : {avg_parse_time} 秒")
65+
print(f"执行平均耗时 : {avg_execute_time} 秒")
66+
print(f"总平均耗时 : {avg_total_time} 秒")
67+
print(f"最长耗时 : {max_time} 秒")
68+
print(f"最短耗时 : {min_time} 秒")
69+
70+
def write_to_csv(self):
71+
# 检查 data 文件夹是否存在,如果不存在则创建
72+
if not os.path.exists('res'):
73+
os.makedirs('res')
74+
# 获取当前时间戳
75+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
76+
# 生成带时间戳的文件名
77+
file_path = os.path.join('res', f'{self.file_name}_{timestamp}.csv')
78+
self.df.to_csv(file_path, index=False)
79+
print(f"测试结果已保存到 {file_path}")
1880

1981
class BatchTest:
2082
def __init__(self, url, agentId, chatId, userName):
@@ -70,18 +132,35 @@ def __get_authorization(self, userName):
70132
def benchmark(url:str, agentId:str, chatId:str, filePath:str, userName:str):
71133
batch_test = BatchTest(url, agentId, chatId, userName)
72134
df = batch_test.read_question_from_csv(filePath)
135+
appender = DataFrameAppender(os.path.basename(filePath))
73136
for index, row in df.iterrows():
74137
question = row['question']
75138
print('start to ask question:', question)
76139
# 捕获异常,防止程序中断
77140
try:
78141
parse_resp = batch_test.parse(question)
79-
batch_test.execute(agentId, question, parse_resp['data']['queryId'])
142+
parse_status = '解析失败'
143+
if parse_resp.get('data').get('errorMsg') is None:
144+
parse_status = '解析成功'
145+
parse_cost = parse_resp.get('data').get('parseTimeCost').get('parseTime')
146+
execute_resp = batch_test.execute(agentId, question, parse_resp['data']['queryId'])
147+
execute_status = '执行失败'
148+
execute_cost = 0
149+
if parse_status == '解析成功' and execute_resp.get('data').get('errorMsg') is None:
150+
execute_status = '执行成功'
151+
execute_cost = execute_resp.get('data').get('queryTimeCost')
152+
res = [question.replace(',', '#'),parse_status,parse_cost/1000,execute_status,execute_cost/1000,(parse_cost+execute_cost)/1000]
153+
appender.append_data(res)
154+
80155
except Exception as e:
81156
print('error:', e)
82157
traceback.print_exc()
83158
continue
84159
time.sleep(1)
160+
# 打印分析结果
161+
appender.print_analysis_result()
162+
# 分析明细输出
163+
appender.write_to_csv()
85164

86165
if __name__ == '__main__':
87166

0 commit comments

Comments
 (0)