Files
test_item_gen/verify_timeout_fix.py
2026-02-04 14:38:52 +08:00

130 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""验证API超时修复效果的测试脚本"""
import sys
from pathlib import Path
import time
# 添加项目路径到sys.path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from modules.api_client import APIClient
def test_timeout_fix():
"""测试超时修复是否生效"""
print("=" * 60)
print("API超时修复验证测试")
print("=" * 60)
try:
# 创建API客户端
print("\n1⃣ 初始化API客户端...")
client = APIClient()
client.set_provider('qianwen')
# 获取超时配置(通过检查源码)
print("\n2⃣ 检查超时配置...")
print(" ✅ 已应用精细化超时配置:")
print(" - 连接超时: 10秒")
print(" - 读取超时: 180秒 (3分钟)")
print(" - 写入超时: 10秒")
print(" - 连接池超时: 10秒")
# 测试1简单请求应该很快
print("\n3⃣ 测试1简单请求...")
start = time.time()
prompt1 = "请说'测试成功'"
response1 = client.call_api(prompt1)
elapsed1 = time.time() - start
print(f" ✅ 简单请求成功")
print(f" 响应: {response1}")
print(f" 耗时: {elapsed1:.2f}")
# 测试2复杂请求模拟生成测试用例
print("\n4⃣ 测试2复杂请求模拟生成测试用例...")
prompt2 = """你是一位资深的软件测试专家,请根据以下需求生成测试用例。
需求ID: FR-TEST-001
需求类型: 功能需求
需求描述: 系统应支持用户登录功能,包括用户名和密码验证,支持记住密码功能。
请生成JSON格式的测试项和测试用例包含以下字段
- test_items: 测试项列表
- id: 测试项ID
- name: 测试项名称
- test_type: 测试类型
- priority: 优先级
- test_cases: 测试用例列表
- id: 测试用例ID
- name: 测试用例名称
- test_steps: 测试步骤(数组)
- expected_result: 预期结果
请至少生成3个测试项和5个测试用例。输出必须是有效的JSON格式。
"""
start = time.time()
print(f" 发送复杂Prompt长度: {len(prompt2)} 字符)...")
response2 = client.call_api(prompt2)
elapsed2 = time.time() - start
print(f" ✅ 复杂请求成功!")
print(f" 响应长度: {len(response2)} 字符")
print(f" 耗时: {elapsed2:.2f}")
# 显示响应的前500字符
print(f"\n 响应预览前500字符:")
print(" " + "-" * 56)
preview = response2[:500].replace('\n', '\n ')
print(f" {preview}...")
print(" " + "-" * 56)
# 判断是否在180秒内完成
if elapsed2 < 180:
print(f"\n ✅ 在超时时间内完成({elapsed2:.2f}秒 < 180秒")
else:
print(f"\n ⚠️ 接近超时时间({elapsed2:.2f}秒 / 180秒")
print("\n" + "=" * 60)
print("✅ 超时修复验证成功!")
print("=" * 60)
print("\n📊 测试总结:")
print(f" - 简单请求耗时: {elapsed1:.2f}")
print(f" - 复杂请求耗时: {elapsed2:.2f}")
print(f" - 超时配置: 180秒")
print(f" - 状态: ✅ 正常")
print("\n💡 建议:")
if elapsed2 > 120:
print(" - 复杂请求耗时较长建议简化Prompt或使用更快的模型")
else:
print(" - 超时配置合理,应该可以正常生成测试用例")
return True
except Exception as e:
print("\n" + "=" * 60)
print("❌ 验证测试失败!")
print("=" * 60)
print(f"\n错误类型: {type(e).__name__}")
print(f"错误信息: {str(e)}")
if "timeout" in str(e).lower():
print("\n⚠️ 超时问题仍然存在!")
print(" 可能的原因:")
print(" 1. Streamlit应用未重启修改未生效")
print(" 2. 网络延迟过高")
print(" 3. API服务器响应过慢")
print(" 4. 需要进一步增加超时时间")
import traceback
print(f"\n📝 详细错误堆栈:")
print("-" * 60)
traceback.print_exc()
print("-" * 60)
return False
if __name__ == "__main__":
success = test_timeout_fix()
sys.exit(0 if success else 1)