一、测试标准化的核心原则
一致性:确保测试流程的可重复性
可维护性:编写易于维护的测试代码
可扩展性:支持项目的持续发展
透明性:清晰的测试报告和日志
流程的可重复性很重要,不仅方便自己的使用、以及自己后续的使用,也方便别人的使用,需要别人能够看得懂。这就需要算法有着清晰的模块,能够解耦合。这些模块拆分开来,以后还能重复地用于其他地方。
二、具体例子
def load_config(config_path):
"""加载测试配置文件"""
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
def chattime_test(questions, chatmodel, output_prefix, model_name, retrieve_number):
"""
运行时间测试,并输出回答、运行时间的json形式文件。
Args:
questions (list): 问题列表
chatmodel (LLMRAGServer): LLMRAGServer类对象
output_prefix (string): 输出文件名前缀
model_name (string): 当前测试的模型名称
retrieve_number (int): 当前测试的检索数量
"""
data = []
for question in questions:
question_data = {
"问题": question,
"模型": model_name,
}
print(f"处理问题: {question}")
print(f"当前配置 - 模型: {model_name}, 检索数量: {retrieve_number}")
try:
# 调用 chat_route 函数,获取返回值
sources = chatmodel.chat_route(question)
# 使用tiktoken计算token数
# import tiktoken
# encoding = tiktoken.get_encoding("cl100k_base")
# tokens = encoding.encode(response)
# token_count = len(tokens)
# question_data["回答"] = response
question_data["sources"] = sources
# question_data["问题预处理时间"] = f"{process_time:.2f}秒"
# question_data["检索链时间"] = f"{rag_chain_time:.5f}秒"
# question_data["回答token数"] = token_count
# question_data["预处理后的问题"] = question_preprocess
except Exception as e:
print(f"处理问题时出错: {e}")
question_data["错误"] = str(e)
data.append(question_data)
# 生成包含模型名称和检索数量的输出文件名
# output_filename = f"{output_prefix}_model_{model_name}_retrieve_{retrieve_number}.json"
# # 将数据写入 JSON 文件
# with open(output_filename, "w", encoding="utf-8") as json_file:
# json.dump(data, json_file, ensure_ascii=False, indent=4)
# 将数据写入Excel文件
# 创建DataFrame
df = pd.DataFrame(data)
print(df)
# 生成Excel文件名
excel_filename = f"{output_prefix}_model_{model_name}.xlsx"
# 将数据写入Excel
df.to_excel(excel_filename, engine='openpyxl')
print(f"结果已保存到: {excel_filename}")
def run_tests(config_path, dataset_path, questions):
主要就是分成了三个模块的函数,第一个模块用于加载测试文件,测试文件是一个json用于批处理测试参数。
然后第二个模块用于运行问题测试,执行具体的业务和测试逻辑,并生成一个文件。其实还可以再次解耦合,数据流统一用字典或列表转为df或者Json,把存取文件到excel单独写一个模块。
第三个模块用于测试参数的组合。加载配置文件和第二个模块所需参数。完成不同参数下的对比测试,其实就是for循环下执行第二个模块。