【发布时间】:2018-06-26 12:58:49
【问题描述】:
目前我需要做一些吞吐量测试。我的硬件设置是三星 950 Pro 连接到 NVMe 控制器,该控制器通过 PCIe 端口连接到主板。我有一个 Linux nvme 设备,对应于我安装在文件系统某个位置的设备。
我希望使用 Python 来做到这一点。我计划在安装 SSD 的文件系统上打开一个文件,记录时间,将一些 n 长度的字节流写入文件,记录时间,然后使用 os 模块文件操作实用程序关闭文件。这是衡量写入吞吐量的函数。
def perform_timed_write(num_bytes, blocksize, fd):
"""
This function writes to file and records the time
The function has three steps. The first is to write, the second is to
record time, and the third is to calculate the rate.
Parameters
----------
num_bytes: int
blocksize that needs to be written to the file
fd: string
location on filesystem to write to
Returns
-------
bytes_per_second: float
rate of transfer
"""
# generate random string
random_byte_string = os.urandom(blocksize)
# open the file
write_file = os.open(fd, os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK)
# set time, write, record time
bytes_written = 0
before_write = time.clock()
while bytes_written < num_bytes:
os.write(write_file, random_byte_string)
bytes_written += blocksize
after_write = time.clock()
#close the file
os.close(write_file)
# calculate elapsed time
elapsed_time = after_write - before_write
# calculate bytes per second
bytes_per_second = num_bytes / elapsed_time
return bytes_per_second
我的另一种测试方法是使用 Linux fio 实用程序。 https://linux.die.net/man/1/fio
在 /fsmnt/fs1 安装 SSD 后,我使用此作业文件来测试吞吐量
;Write to 1 file on partition
[global]
ioengine=libaio
buffered=0
rw=write
bs=4k
size=1g
openfiles=1
[file1]
directory=/fsmnt/fs1
我注意到Python函数返回的写入速度明显高于fio。因为 Python 是如此高级,所以你放弃了很多控制权。我想知道 Python 是否正在做一些事情来欺骗它的速度。有谁知道为什么 Python 生成的写入速度会比 fio 生成的快很多吗?
【问题讨论】:
-
os.write()返回写入的字节数,您应该在每次循环迭代时将其添加到bytes_written。可能有短写。 Python测试与fio测试的相对速度是多少? -
Python:fio 速度为 4:1。你的建议也被注意到了。我更新了软件来做到这一点,因为你没有保证写入将返回块大小。它可能会失败并返回 0。添加它后,虽然结果看起来相同
-
我也怀疑非阻塞 I/O 选项正在改变时序的含义。你为什么这样做?如果您对确定设备的实际 I/O 速度感兴趣,您真的希望仅在 I/O 真正完成后才返回 write 调用。就目前而言,您测量的可能更像是非阻塞写入系统调用所花费的时间,FIO 工具可能足够聪明以进行补偿。只是猜测。
-
也许,但结果或多或少与第一个使用带有 open() 文件描述符的经典实现相同。我这样做是因为我需要一次写入多个分区,我相信我是通过多线程完成的,尽管结果表明不是,但这本身就是另一个问题。