Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
What OS distribution, kernel and version? –  ewwhite Nov 28 '12 at 3:08
    
What hard drive? –  Michael Hampton Nov 28 '12 at 3:15
add comment

2 Answers 2

up vote 5 down vote accepted

Sure - you'll want to take a look at your drive's seek time specification, but 16ms is well within the realm of "normal" for spinning magnetic media.

share|improve this answer
add comment

If you're doing anything that will end up in production, there should be a layer of caching, a real RAID controller and other factors in play. What are you running on? What is the point of this exercise? Can you elaborate on the details of this setup?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.