增强日志
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user