尝试自己写了脚本测试硬盘的 4k 随机读写速度, 但得出的数据有问题[help]

2022-02-18 02:54:20 +08:00
 jieke0205

测试的脚本有 2 个文件,bash 脚本创建将来要读的文件,a.py 脚本进行读写的测试,b.py 脚本空载对比.

create_files.sh

for i in {0..1000}; do dd if=/dev/zero of=$i bs=1M count=1; done

a.py

import threading
import time

def test(fn):
        
    file = open(str(fn), "rb")

    bytes = file.read(1)
    file.close()
    # print(fn, len(bytes))

def foo():
    pass


if __name__ == "__main__":
    ts = list()
    start_time = time.perf_counter ()
    for i in range(1000):
        ts.append(threading.Thread(target=test, args=(i%1000,)))
        # ts.append(threading.Thread(target=foo))
        ts[-1].start()
    
    for t in ts:
        t.join()

    end_time = time.perf_counter ()

    print(end_time - start_time)

b.py

import threading
import time

def test(fn):
        
    file = open(str(fn), "rb")

    # bytes = file.read(1)  与 a.py 相比就仅仅注释了这一句
    file.close()
    # print(fn, len(bytes))

def foo():
    pass


if __name__ == "__main__":
    ts = list()
    start_time = time.perf_counter ()
    for i in range(1000):
        ts.append(threading.Thread(target=test, args=(i%1000,)))
        # ts.append(threading.Thread(target=foo))
        ts[-1].start()
    
    for t in ts:
        t.join()

    end_time = time.perf_counter ()

    print(end_time - start_time)

在机械硬盘,固态上以及空载的结果分别是 0.07257, 0.07181, 0.0668.二者的差距微乎其微. 我了解到 7200rpm 的机械硬盘随机 4k 的速度大概在 0.5MB 左右.[https://superuser.com/questions/1595841/very-slow-random-read-write-on-hdd]

根据我的测试可得 4kB * 1000 / (0.07257s - 0.0668s) = 693240.901 KB/s [滑稽] 小弟我疑惑万分, 不知是不是目前的测试仍然在硬盘的缓存之上,所以测不出实际性能,还是其他什么原因.希望有知晓老哥的提点一二.

1208 次点击
所在节点    问与答
6 条回复
haiyang1992
2022-02-18 03:29:07 +08:00
原因之一可能你这 load 太小了,又跑这么多线程,处理 system call 的开销占了大部分的运行时间,并且 cpu context switch 过程也能掩盖掉一部分 io 的时间
Sylv
2022-02-18 06:45:32 +08:00
系统对读写是有缓存的。
liprais
2022-02-18 08:23:27 +08:00
都写到 file system cache 里了,测了个寂寞
murmur
2022-02-18 09:11:36 +08:00
不建议自己折腾这些,如果你想测云就佛系,云的硬盘就是离谱
如果你想有参考,用 windows 的 crystal 或者是 hdtunes 等测试软件,一定有相对可信的、大家都用的,才好有参考对比
msg7086
2022-02-18 09:56:58 +08:00
测内存读写速度吗?
destinyzou
2022-02-18 10:02:27 +08:00
建议用 fio 吧,资料多,文档丰富。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/834671

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX