增强日志
This commit is contained in:
@@ -358,6 +358,9 @@ def run_memtester(duration: int = 300) -> Dict[str, Any]:
|
||||
Returns:
|
||||
Dict[str, Any]: 测试结果
|
||||
"""
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
result = {
|
||||
"passed": False,
|
||||
"size_mb": 0,
|
||||
@@ -371,9 +374,12 @@ def run_memtester(duration: int = 300) -> Dict[str, Any]:
|
||||
|
||||
if not check_command_exists('memtester'):
|
||||
result["errors"].append("memtester 未安装")
|
||||
logger.warning("[MEMORY STRESS TEST] memtester 未安装")
|
||||
return result
|
||||
|
||||
try:
|
||||
logger.info("[MEMORY STRESS TEST] 开始使用 memtester 进行内存测试")
|
||||
|
||||
# 计算测试内存大小
|
||||
# 留出一些内存给系统和 stress-ng 使用
|
||||
with open('/proc/meminfo', 'r') as f:
|
||||
@@ -391,8 +397,11 @@ def run_memtester(duration: int = 300) -> Dict[str, Any]:
|
||||
result["start_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
start_ts = time.time()
|
||||
|
||||
logger.info(f"[MEMORY STRESS TEST] 测试内存大小: {test_size_mb}MB")
|
||||
|
||||
# 运行 memtester
|
||||
cmd = ['memtester', f'{test_size_mb}M', '1']
|
||||
logger.info(f"[MEMORY STRESS TEST] 执行命令: {' '.join(cmd)}")
|
||||
|
||||
_, stdout, stderr = execute_command(
|
||||
cmd,
|
||||
@@ -403,25 +412,32 @@ def run_memtester(duration: int = 300) -> Dict[str, Any]:
|
||||
result["end_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
result["duration_seconds"] = round(time.time() - start_ts, 2)
|
||||
|
||||
logger.info(f"[MEMORY STRESS TEST] memtester 执行完成,耗时: {result['duration_seconds']}秒")
|
||||
|
||||
output = stdout + stderr
|
||||
result["raw_output"] = output[:2000] # 保存部分原始输出
|
||||
|
||||
# 分析结果
|
||||
if 'FAILURE' in output.upper():
|
||||
result["passed"] = False
|
||||
logger.error("[MEMORY STRESS TEST] 测试失败: 发现 FAILURE")
|
||||
# 提取错误信息
|
||||
for line in output.split('\n'):
|
||||
if 'FAILURE' in line.upper() or 'error' in line.lower():
|
||||
result["errors"].append(line.strip())
|
||||
logger.error(f"[MEMORY STRESS TEST] 错误详情: {line.strip()}")
|
||||
elif 'SUCCESS' in output.upper() or 'ok' in output.lower() or 'finished' in output.lower():
|
||||
result["passed"] = True
|
||||
logger.info("[MEMORY STRESS TEST] 测试通过")
|
||||
else:
|
||||
# 检查是否完成所有测试
|
||||
if 'Done' in output or 'finished' in output.lower():
|
||||
result["passed"] = True
|
||||
logger.info("[MEMORY STRESS TEST] 测试完成")
|
||||
else:
|
||||
result["passed"] = False
|
||||
result["errors"].append("测试可能未完成")
|
||||
logger.warning("[MEMORY STRESS TEST] 测试可能未完成")
|
||||
|
||||
# 提取运行的测试
|
||||
test_names = [
|
||||
@@ -436,9 +452,12 @@ def run_memtester(duration: int = 300) -> Dict[str, Any]:
|
||||
if test in output:
|
||||
result["tests_run"].append(test)
|
||||
|
||||
logger.info(f"[MEMORY STRESS TEST] 执行的测试项: {', '.join(result['tests_run'])}")
|
||||
|
||||
except Exception as e:
|
||||
result["passed"] = False
|
||||
result["errors"].append(str(e))
|
||||
logger.exception(f"[MEMORY STRESS TEST] memtester 执行异常: {e}")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user