/* $Id: bench_read.c,v 1.1 2012/02/03 15:53:01 kostik Exp kostik $ */ #include #include #include #include #include #include static void do_reads(int fd, char *buf, size_t bufsize, int count) { int i, error; for (i = 0; i < count; i++) { error = lseek(fd, 0, SEEK_SET); if (error != 0) err(1, "lseek"); error = read(fd, buf, bufsize); if (error == -1) err(1, "read"); else if (error != (int)bufsize) errx(1, "truncated read %d %d", bufsize, error); } } int main(int argc, char *argv[]) { struct timeval start, end; char *buf; size_t bufsize; const char *filename; int fd; int count; double sp; if (argc != 4) errx(1, "Usage: %s file bufsize count", argv[0]); filename = argv[1]; bufsize = atoi(argv[2]); count = atoi(argv[3]); buf = malloc(bufsize); fd = open(filename, O_RDONLY); if (fd == -1) err(1, "open %s", filename); gettimeofday(&start, NULL); do_reads(fd, buf, bufsize, count); gettimeofday(&end, NULL); sp = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) * 1.0e-6; sp /= count; printf("%e\n", sp); return (0); }