Linux基础IO函数压力测试

Posted: 2010年6月10日星期四
做系统性能调优确实不好做,不仅要从算法上去解决,还有就是系统的一个库函数的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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", 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=%.0f\n", etime, LOOPNUM / etime);

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

return 0;
}



编译执行,在我笔记本上的执行结果如下:
open&close: total=2.59459996 qps=385416
open&write&close: total=4.47499609 qps=223464
open&read&close: total=3.04626703 qps=328271
lseek&write: total=1.35459495 qps=738228
lseek&read: total=0.51210380 qps=1952729
pwrite: total=1.15936184 qps=862543
pread: total=0.32837701 qps=3045280
mmap&output: total=0.00775003 qps=129031686
mmap&input: total=0.00818515 qps=122172497
从上面结果看,还是mmap速度快,跟其它的少一个数量级。

0 评论: