import os
import subprocess

# 定义脚本所在的目录
TOOL_DIR = "/root/presure-test-tool"
# 定义日志文件存放的目录
LOG_DIR = "/root/presurelog"

# 检查日志目录是否存在，如果不存在则创建
if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)

# 定义要运行的脚本列表
scripts = [
    "test_all_reduce_perf_multi_card.sh",
    "test_all_reduce_perf.sh",
    "test_cpu.sh",
    #"test_fio.sh",
    "test_gpu.sh",
    "test_mlc_latency_matrix.sh",
    "test_mlc_max_bandwidth.sh",
    "test_pcie_p2p.sh",
    "train_op.py",
]

# 遍历脚本列表并执行它们
for script in scripts:
    print(f"Running {script}...")
    log_file_name = os.path.splitext(script)[0]  # 获取不带后缀的脚本名

    # 构建完整的脚本路径和日志文件路径
    script_path = os.path.join(TOOL_DIR, script)
    log_file_path = os.path.join(LOG_DIR, f"{log_file_name}.log")

    # 根据文件扩展名选择执行命令
    command = ["python3"] if script.endswith('.py') else ["bash"]
    command.append(script_path)

    # 使用 subprocess 执行脚本并将输出重定向到日志文件
    with open(log_file_path, 'w') as log_file:
        subprocess.run(command, stdout=log_file, stderr=log_file)

# 当所有脚本都执行完毕后，在控制台输出完成的消息
print("All scripts have been executed and logs are saved in {}.".format(LOG_DIR))

