随机磁盘寻道和读取成本约为 16 毫秒是否合理?

Fan*_*Fan 2 linux performance hard-drive

我对从非 ssd 磁盘随机读取的延迟感到沮丧。根据以下测试程序的结果,在没有操作系统缓存帮助的情况下,随机读取仅 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;
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*den 5

当然 - 你会想看看你的驱动器的寻道时间规格,但 16ms 完全在旋转磁介质的“正常”范围内。