在linux中用python3写一个用fio测试固态硬盘的python脚本: (1)对待测试盘进行擦除 (2)使用fio进行测试,遍历不同的block size,并记录测试结果 (3)用for语句提取测试结果中的BW、IOPS、Lantency(us)到excel中 (4)使用logging模块将步骤(1)(2)(3)记录在测试log中,并同步打印出调试信息
时间: 2024-01-31 14:02:16 浏览: 26
您好!以下是一个用Python 3编写的脚本,可以使用fio对固态硬盘进行测试,并将结果提取到Excel中,并使用logging模块记录测试步骤和调试信息。请确保已经安装了fio和openpyxl模块。
```python
import subprocess
import openpyxl
import logging
def erase_disk(device):
logging.info("Erasing disk: {}".format(device))
subprocess.run(["blkdiscard", device])
def run_fio_test(device):
logging.info("Running fio test on disk: {}".format(device))
block_sizes = [4, 8, 16, 32, 64] # 可根据需要修改
results = []
for block_size in block_sizes:
logging.info("Running fio test with block size: {}k".format(block_size))
result = subprocess.run(["fio", "--filename={}".format(device), "--direct=1", "--rw=write", "--bs={}k".format(block_size), "--numjobs=1", "--size=1G", "--time_based", "--runtime=60", "--name=test"], capture_output=True, text=True)
results.append(result.stdout)
return results
def extract_results(results):
logging.info("Extracting results to Excel")
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "FIO Results"
for i, result in enumerate(results):
lines = result.splitlines()
for line in lines:
if "bw=" in line:
bw = line.split("bw=")[1].split(",")[0].strip()
iops = line.split("iops=")[1].split(",")[0].strip()
latency = line.split("lat=")[1].split(",")[0].strip()
sheet.cell(row=i+2, column=1).value = bw
sheet.cell(row=i+2, column=2).value = iops
sheet.cell(row=i+2, column=3).value = latency
wb.save("fio_results.xlsx")
if __name__ == "__main__":
logging.basicConfig(filename="test.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
device = "/dev/sda" # 替换为您要测试的磁盘设备
erase_disk(device)
results = run_fio_test(device)
extract_results(results)
```
此脚本首先定义了一个`erase_disk`函数,用于擦除待测试的磁盘。然后定义了`run_fio_test`函数,使用不同的块大小运行fio测试,并将结果存储在一个列表中。然后,`extract_results`函数将结果提取到Excel文件中。最后,在`main`函数中,配置logging模块记录步骤和调试信息,然后依次执行擦除磁盘、运行fio测试和提取结果。
请根据您的实际情况修改`device`变量为您要测试的磁盘设备的路径。运行脚本后,测试结果将保存在名为`fio_results.xlsx`的Excel文件中,并且步骤和调试信息将记录在`test.log`文件中。