ShopTRAINING/test/simple_training_test.py
2025-07-02 11:05:23 +08:00

90 lines
2.8 KiB
Python

#\!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单训练测试 - 验证API日志输出修复
"""
import os
import sys
import urllib.request
import json
import time
# 设置UTF-8编码
os.environ['PYTHONIOENCODING'] = 'utf-8'
os.environ['PYTHONLEGACYWINDOWSSTDIO'] = '0'
# Windows系统额外配置
if os.name == 'nt':
try:
os.system('chcp 65001 >nul 2>&1')
if hasattr(sys.stdout, 'reconfigure'):
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
except Exception as e:
print(f'Warning: Failed to set UTF-8 encoding: {e}')
def send_training_request():
"""发送训练请求"""
training_data = {
'product_id': 'P001',
'model_type': 'transformer',
'epochs': 2, # 短期训练以便快速验证
'training_mode': 'product'
}
try:
json_data = json.dumps(training_data).encode('utf-8')
req = urllib.request.Request(
'http://127.0.0.1:5000/api/training',
data=json_data,
headers={'Content-Type': 'application/json'}
)
with urllib.request.urlopen(req, timeout=30) as response:
return response.getcode(), json.loads(response.read().decode('utf-8'))
except Exception as e:
return None, str(e)
def main():
"""主测试函数"""
print('简单训练日志验证测试')
print('=' * 50)
print('目标: 验证API训练时的控制台输出')
print('观察重点: API服务器控制台应该显示:')
print('1. 训练任务线程启动消息')
print('2. 参数配置信息')
print('3. 训练器内部的详细进度')
print('4. 带时间戳和线程ID的格式化输出')
print('=' * 50)
print('发送训练请求...')
status, response = send_training_request()
if status == 200:
task_id = response.get('task_id')
print(f'训练请求成功')
print(f'任务ID: {task_id}')
print(f'响应: {response.get("message")}')
print('\n请观察API服务器控制台输出...')
print('等待15秒让训练完成...')
# 等待训练完成
for i in range(15):
time.sleep(1)
print(f'等待中... {i+1}/15秒', end='\r')
print('\n\n训练应该已完成')
print('验证要点:')
print('1. API服务器控制台是否显示了完整的训练日志')
print('2. 日志格式是否包含 [时间戳][线程ID][任务ID][标签]')
print('3. 中文字符和emoji是否正常显示')
print('4. 训练器的进度信息是否可见')
else:
print(f'训练请求失败: {response}')
if __name__ == '__main__':
main()