增强日志
This commit is contained in:
@@ -402,6 +402,9 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
Returns:
|
||||
Dict[str, Any]: 测试结果
|
||||
"""
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
result = {
|
||||
"passed": False,
|
||||
"duration_seconds": duration,
|
||||
@@ -417,10 +420,13 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
if check_command_exists('stress-ng'):
|
||||
result["tool_used"] = "stress-ng"
|
||||
try:
|
||||
logger.info(f"[CPU STRESS TEST] 开始使用 stress-ng 进行压力测试,持续时间: {duration}秒")
|
||||
result["start_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# 获取测试前温度
|
||||
temp_before = get_cpu_temperature()
|
||||
temp_before_val = temp_before.get("max_c", "N/A")
|
||||
logger.info(f"[CPU STRESS TEST] 测试前温度: {temp_before_val}°C")
|
||||
|
||||
# 运行 stress-ng
|
||||
# --cpu 0 使用所有 CPU 核心
|
||||
@@ -433,6 +439,8 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
'--metrics-brief'
|
||||
]
|
||||
|
||||
logger.info(f"[CPU STRESS TEST] 执行命令: {' '.join(cmd)}")
|
||||
|
||||
_, stdout, stderr = execute_command(
|
||||
cmd,
|
||||
timeout=duration + 30, # 给一些额外时间
|
||||
@@ -440,9 +448,12 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
)
|
||||
|
||||
result["end_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
logger.info("[CPU STRESS TEST] stress-ng 执行完成")
|
||||
|
||||
# 获取测试后温度
|
||||
temp_after = get_cpu_temperature()
|
||||
temp_after_val = temp_after.get("max_c", "N/A")
|
||||
logger.info(f"[CPU STRESS TEST] 测试后温度: {temp_after_val}°C")
|
||||
|
||||
# 分析输出
|
||||
output = stdout + stderr
|
||||
@@ -451,13 +462,16 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
if 'error' in output.lower() or 'fail' in output.lower():
|
||||
result["passed"] = False
|
||||
result["errors"].append("压力测试过程中发现错误")
|
||||
logger.error("[CPU STRESS TEST] 压力测试执行过程中发现错误")
|
||||
else:
|
||||
result["passed"] = True
|
||||
logger.info("[CPU STRESS TEST] 压力测试通过")
|
||||
|
||||
# 提取性能指标
|
||||
bogo_ops = re.search(r'stress-ng:\s+cpu:\s+(\d+)\s+bogo ops', output)
|
||||
if bogo_ops:
|
||||
result["bogo_ops"] = safe_int(bogo_ops.group(1))
|
||||
logger.info(f"[CPU STRESS TEST] Bogo ops: {result['bogo_ops']}")
|
||||
|
||||
bogo_ops_per_sec = re.search(r'(\d+\.\d+)\s+bogo ops per second', output)
|
||||
if bogo_ops_per_sec:
|
||||
@@ -475,16 +489,22 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
except Exception as e:
|
||||
result["passed"] = False
|
||||
result["errors"].append(str(e))
|
||||
logger.exception(f"[CPU STRESS TEST] stress-ng 执行异常: {e}")
|
||||
|
||||
# 备选: 使用 stress
|
||||
elif check_command_exists('stress'):
|
||||
result["tool_used"] = "stress"
|
||||
try:
|
||||
logger.info(f"[CPU STRESS TEST] 开始使用 stress 进行压力测试,持续时间: {duration}秒")
|
||||
result["start_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
temp_before = get_cpu_temperature()
|
||||
temp_before_val = temp_before.get("max_c", "N/A")
|
||||
logger.info(f"[CPU STRESS TEST] 测试前温度: {temp_before_val}°C")
|
||||
|
||||
num_cores = os.cpu_count() or 1
|
||||
logger.info(f"[CPU STRESS TEST] 使用 {num_cores} 个 CPU 核心")
|
||||
|
||||
_, stdout, stderr = execute_command(
|
||||
['stress', '--cpu', str(num_cores), '--timeout', str(duration)],
|
||||
timeout=duration + 30,
|
||||
@@ -492,7 +512,11 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
)
|
||||
|
||||
result["end_time"] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
logger.info("[CPU STRESS TEST] stress 执行完成")
|
||||
|
||||
temp_after = get_cpu_temperature()
|
||||
temp_after_val = temp_after.get("max_c", "N/A")
|
||||
logger.info(f"[CPU STRESS TEST] 测试后温度: {temp_after_val}°C")
|
||||
|
||||
result["passed"] = True
|
||||
result["temperature_before"] = temp_before
|
||||
@@ -504,11 +528,13 @@ def run_cpu_stress_test(duration: int = 300) -> Dict[str, Any]:
|
||||
except Exception as e:
|
||||
result["passed"] = False
|
||||
result["errors"].append(str(e))
|
||||
logger.exception(f"[CPU STRESS TEST] stress 执行异常: {e}")
|
||||
|
||||
else:
|
||||
result["passed"] = False
|
||||
result["errors"].append("未找到压力测试工具 (stress-ng 或 stress)")
|
||||
result["note"] = "请安装 stress-ng 或 stress: yum install stress / apt install stress-ng"
|
||||
logger.error("[CPU STRESS TEST] 未找到压力测试工具 (stress-ng 或 stress)")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -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