88 lines
3.5 KiB
Python
88 lines
3.5 KiB
Python
import os
|
|
import json
|
|
import logging
|
|
from .api_client import APIClient
|
|
from .visualizer import generate_visualization, sanitize_filename
|
|
|
|
# 配置日志
|
|
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
|
|
def run_interactive_test():
|
|
client = APIClient()
|
|
print("\n🚀 进入交互式测试模式 (输入 'exit' 或 'q' 退出)")
|
|
|
|
while True:
|
|
try:
|
|
prompt = input("\n请输入测试指令: ").strip()
|
|
if prompt.lower() in ['exit', 'q']:
|
|
break
|
|
if not prompt:
|
|
continue
|
|
|
|
print("⏳ 正在请求后端 API...")
|
|
result = client.send_request(prompt)
|
|
|
|
if result['success']:
|
|
print(f"✅ 请求成功 (耗时: {result['latency']:.2f}s)")
|
|
|
|
# 创建输出目录
|
|
sanitized_name = sanitize_filename(prompt)
|
|
output_dir = os.path.join(
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
"validation",
|
|
"temporary",
|
|
sanitized_name
|
|
)
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
# 保存 JSON
|
|
json_path = os.path.join(output_dir, "response.json")
|
|
with open(json_path, 'w', encoding='utf-8') as f:
|
|
json.dump(result['data'], f, indent=2, ensure_ascii=False)
|
|
|
|
# 保存日志
|
|
log_path = os.path.join(output_dir, "process.log")
|
|
with open(log_path, 'w', encoding='utf-8') as f:
|
|
f.write(f"Prompt: {prompt}\n")
|
|
f.write(f"Status: {result['http_status']}\n")
|
|
f.write(f"Latency: {result['latency']}\n")
|
|
f.write(f"Response: {json.dumps(result['data'], ensure_ascii=False)}\n")
|
|
|
|
# 生成图片
|
|
if result['data'] and 'root' in result['data']:
|
|
png_path = os.path.join(output_dir, "plan.png")
|
|
if generate_visualization(result['data']['root'], png_path):
|
|
print(f"🖼️ 可视化图已生成: {png_path}")
|
|
else:
|
|
print("⚠️ 可视化生成失败")
|
|
|
|
print(f"📂 结果已保存至: {output_dir}")
|
|
else:
|
|
print(f"❌ 请求失败: {result['error']}")
|
|
|
|
# 即使失败也保存日志,以便排查
|
|
sanitized_name = sanitize_filename(prompt)
|
|
output_dir = os.path.join(
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
"validation",
|
|
"temporary",
|
|
sanitized_name
|
|
)
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
log_path = os.path.join(output_dir, "process.log")
|
|
with open(log_path, 'w', encoding='utf-8') as f:
|
|
f.write(f"Prompt: {prompt}\n")
|
|
f.write(f"Status: {result['http_status']}\n")
|
|
f.write(f"Latency: {result['latency']}\n")
|
|
f.write(f"Error: {result['error']}\n")
|
|
# 如果有部分数据,也记录下来
|
|
if result['data']:
|
|
f.write(f"Partial Response: {json.dumps(result['data'], ensure_ascii=False)}\n")
|
|
print(f"⚠️ 错误日志已保存至: {log_path}")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n已取消")
|
|
break
|
|
|