我对从非ssd磁盘随机读取的延迟感到沮丧。 根据以下testing程序的结果,在没有OS高速caching的情况下,只需512字节的随机读取速度就可以达到16毫秒。
我尝试将512更改为更大的值,例如25k,延迟并没有增加太多。 我想这是因为磁盘寻找占据了时间。
我明白,随机读取本质上是缓慢的,但只是要确定〜16ms是合理的,即使对于非ssd磁盘。
#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; }
当然 – 你会想看看你的驱动器的寻找时间规格,但16毫秒是在“正常”的范围内旋转磁介质。
如果你正在做的任何事情都将在生产中结束,应该有一层caching,一个真正的RAID控制器和其他因素。 你在跑什么? 这个练习的重点是什么? 你能详细说明这个设置的细节吗?