V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jieke0205
V2EX  ›  问与答

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

  •  
  •   jieke0205 · 2022-02-18 02:54:20 +08:00 · 1195 次点击
    这是一个创建于 796 天前的主题,其中的信息可能已经有所发展或是发生改变。

    测试的脚本有 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 [滑稽] 小弟我疑惑万分, 不知是不是目前的测试仍然在硬盘的缓存之上,所以测不出实际性能,还是其他什么原因.希望有知晓老哥的提点一二.

    6 条回复    2022-02-18 10:02:27 +08:00
    haiyang1992
        1
    haiyang1992  
       2022-02-18 03:29:07 +08:00 via Android
    原因之一可能你这 load 太小了,又跑这么多线程,处理 system call 的开销占了大部分的运行时间,并且 cpu context switch 过程也能掩盖掉一部分 io 的时间
    Sylv
        2
    Sylv  
       2022-02-18 06:45:32 +08:00
    系统对读写是有缓存的。
    liprais
        3
    liprais  
       2022-02-18 08:23:27 +08:00 via iPhone
    都写到 file system cache 里了,测了个寂寞
    murmur
        4
    murmur  
       2022-02-18 09:11:36 +08:00
    不建议自己折腾这些,如果你想测云就佛系,云的硬盘就是离谱
    如果你想有参考,用 windows 的 crystal 或者是 hdtunes 等测试软件,一定有相对可信的、大家都用的,才好有参考对比
    msg7086
        5
    msg7086  
       2022-02-18 09:56:58 +08:00 via Android
    测内存读写速度吗?
    destinyzou
        6
    destinyzou  
       2022-02-18 10:02:27 +08:00
    建议用 fio 吧,资料多,文档丰富。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2306 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:01 · PVG 08:01 · LAX 17:01 · JFK 20:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.