zoukankan      html  css  js  c++  java
  • 磁盘文件读性能测试

    未缓存前:

    time ./x bin.tar 

    file size is 816322560

    816322560 bytes read now

    real    0m3.378s

    user    0m0.000s

    sys     0m0.996s

    被缓存后:

    time ./x bin.tar 

    file size is 816322560

    816322560 bytes read now

    real    0m0.770s

    user    0m0.000s

    sys     0m0.768s

    硬盘读取性能:

    hdparm -t /dev/sdb 

    /dev/sdb:

     Timing buffered disk reads: 2454 MB in  3.00 seconds = 817.84 MB/sec

    10块物理磁盘,做了Raid10,因此读性能高,达每秒817.84MB

    测试程序:

    // 非优化方式编译:g++ -g -o x x.cpp

    #include <errno.h>

    #include <fcntl.h>

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <sys/stat.h>

    #include <sys/types.h>

    #include <unistd.h>

     

    // 带一个参数,为被读取文件的大小

    int main(int argc, char* argv[])

    {

            int fd = open(argv[1], O_RDONLY);

            if (-1 == fd)

            {

                    perror(strerror(errno));

                    exit(1);

            }

     

            struct stat st;

            if (0 == fstat(fd, &st))

            {

                    // 输出文件大小,单位为字节

                    printf("file size is %d ", st.st_size);

     

                    // 一次性将整个文件读到内存中

                    char* bytes = new char[st.st_size];

                    int bytes_read = read(fd, bytes, st.st_size);

     

                    // 显示实际成功读取的字节数

                    printf("%d bytes read now ", bytes_read);

                    delete []bytes;

            }

     

            close(fd);

            return 0;

    }

    清缓存:

    使用free命令观察下列操作的变化,以root用户执行:先执行下sync命令,以将数据更新到磁盘,再执行echo 3 > /proc/sys/vm/drop_caches,以清除系统的cached

    文件内存的缓存会反应出free命令输出的cached值的变化,实际就是Page cache,文件内容的读取会缓存在这里。如果读取一个大文件,可以看到cached的值明显增涨,并且增涨大小差不多就是文件的大小,buffers相当于cached的元信息,比如文件的inode

    cached影响文件的读取性能,而buffers影响到文件的打开性能。

    drop_caches使用汇总:

    echo 0 > /proc/sys/vm/drop_caches

    不做释放

    echo 1 > /proc/sys/vm/drop_caches

    释放Page Cache

    echo 2 > /proc/sys/vm/drop_caches

    释放Dentries/inodes Cache(其中,Dentry是目录信息,inode是文件信息)

    echo 3 > /proc/sys/vm/drop_caches

    释放PageDentries/inodes Cache

    这个特性由2.6.16内核开始提供,参考资料:

    1) /proc/sys/vm/drop_caches

    http://linux-mm.org/Drop_Caches

    2) Linux Buffer vs Cache explained

    http://random-techbits.blogspot.com/2012/06/linux-buffer-vs-cache-explained.html

  • 相关阅读:
    P1908 逆序对
    P1967 货车运输
    成也DP,败也DP(AFO?)
    Review Before THUWC2020
    THUWC2020游记
    loj6295. 无意识之外的捉迷藏
    loj6504. 「雅礼集训 2018 Day5」Convex
    某道XJ题
    loj2304. 「NOI2017」泳池
    loj6435. 「PKUSC2018」星际穿越
  • 原文地址:https://www.cnblogs.com/gergro/p/3668870.html
Copyright © 2011-2022 走看看