Is it reasonable that a random disk seek & read costs ~16ms?
- by fzhang
I am frustrated about the latency of random reading from a non-ssd disk. Based on results from following test program, it speeds ~16 ms for a random read of just 512 bytes without help of os cache.
I tried changing 512 to larger values, such as 25k, and the latency did not increase as much. I guess it is because the disk seek dominates the time.
I understand that random reading is inherently slow, but just want to be sure that ~16ms is reasonable, even for non-ssd disk.
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
int fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Failed open %s\n", argv[1]);
return -1;
}
const size_t count = 512;
const off_t offset = 25990611 / 2;
char buffer[count] = { '\0' };
struct timeval start_time;
gettimeofday(&start_time, NULL);
off_t ret = lseek(fd, offset, SEEK_SET);
if (ret != offset) {
perror("lseek error");
close(fd);
return -1;
}
ret = read(fd, buffer, count);
if (ret != count) {
fprintf(stderr, "Failed reading all: %ld\n", ret);
close(fd);
return -1;
}
struct timeval end_time;
gettimeofday(&end_time, NULL);
printf("tv_sec: %ld, tv_usec: %ld\n",
end_time.tv_sec - start_time.tv_sec,
end_time.tv_usec - start_time.tv_usec);
close(fd);
return 0;
}