最新消息:

Linux 标准C IO函数压力测试

IO admin 3529浏览 0评论

做系统性能调优确实不好做,不仅要从算法上去解决,还有就是系统的一个库函数的IO能力到底怎么样,虽然有些资料提到,但到底差多少,心里还是没谱,下面是我做的一个linux系统的IO函数的处理效率的压力测试,各执行一百万次,看消耗的时间,详情请看代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/time.h>
#define LOOPNUM 1000000
#define IOSIZE 8

static double currtime(void){
struct timeval tv;

gettimeofday(&tv, NULL);
        return tv.tv_sec + (double)tv.tv_usec / (1000 * 1000);
}

int main(int argc, char *argv[])
{
        int i;
        double stime, etime;

        // open and close
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                int fd = open("TESTFILE", O_RDWR | O_CREAT, 00644);
                close(fd);
        }

        etime = currtime() - stime;
        printf("open&close: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // open and write and close
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                int fd = open("TESTFILE", O_RDWR | O_CREAT, 00644);
                write(fd, "hogehoge", IOSIZE);
                close(fd);
        }

        etime = currtime() - stime;
        printf("open&write&close: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // open and read and close
        char buf[256];
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                int fd = open("TESTFILE", O_RDWR | O_CREAT, 00644);
                read(fd, buf, IOSIZE);
                close(fd);
        }

        etime = currtime() - stime;
        printf("open&read&close: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // lseek and write
        int fd = open("TESTFILE", O_RDWR | O_CREAT, 00644);
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                lseek(fd, 0, SEEK_SET);
                write(fd, "hogehoge", IOSIZE);
        }

        etime = currtime() - stime;
        printf("lseek&write: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // lseek and read
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                lseek(fd, 0, SEEK_SET);
                read(fd, buf, IOSIZE);
        }

        etime = currtime() - stime;
        printf("lseek&read: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // pwrite
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++) {
                pwrite(fd, "hogehoge", IOSIZE, 0);
        }

        etime = currtime() - stime;
        printf("pwrite: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // pwrite
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                pread(fd, buf, IOSIZE, 0);
        }

        etime = currtime() - stime;
        printf("pread: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // mmap and output
        void *map = mmap(NULL, IOSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                memcpy(map, "hogehoge", IOSIZE);
        }

        etime = currtime() - stime;
        printf("mmap&output: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        // mmap and input
        stime = currtime();
        for(i = 0; i < LOOPNUM; i++)
        {
                memcpy(buf, map, IOSIZE);
        }

        etime = currtime() - stime;
        printf("mmap&input: total=%.8f qps=%.0fn", etime, LOOPNUM / etime);

        munmap(map, IOSIZE);
        close(fd);

        return 0;
}

[root@selboo ~]# gcc c.c
[root@selboo ~]# ./a.out
open&close: total=5.41255403 qps=184756
open&write&close: total=9.34075689 qps=107058
open&read&close: total=7.12383413 qps=140374
lseek&write: total=4.33155084 qps=230864
lseek&read: total=2.23808098 qps=446811
pwrite: total=3.41541409 qps=292790
pread: total=1.42290711 qps=702787
mmap&output: total=0.01358390 qps=73616569
mmap&input: total=0.01553297 qps=64379186

 

转载请注明:爱开源 » Linux 标准C IO函数压力测试

您必须 登录 才能发表评论!