seqtest.c

text/x-csrc

Filename: seqtest.c
Type: text/x-csrc
Part: 0
Message: Re: [HACKERS] No heap lookups on index
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>

#define BLOCKSIZE 8192

int main(int argc, char *argv[], char *arge[]) 
{
  char *fn;
  int fd;
  int perc;
  struct stat statbuf;
  struct timeval tv1,tv2;
  off_t size, offset;
  char *buf[BLOCKSIZE];
  int b_toread, b_toskip, b_read=0, b_skipped=0;
  long us;

  fn = argv[1];
  perc = atoi(argv[2]);

  fd = open(fn, O_RDONLY);
  fstat(fd, &statbuf);
  size = statbuf.st_size;
  
  size = size/BLOCKSIZE*BLOCKSIZE;
  
  gettimeofday(&tv1, NULL);

  srandom(getpid()^tv1.tv_sec^tv1.tv_usec);

  b_toread = size/BLOCKSIZE*perc/100;
  b_toskip = size/BLOCKSIZE-b_toread;

  for(offset=0;offset<size;offset+=BLOCKSIZE) {
    if (random()%(b_toread+b_toskip) < b_toread) {
      lseek(fd, offset, SEEK_SET);
      read(fd, buf, BLOCKSIZE);
      b_toread--;
      b_read++;
    } else {
      b_toskip--;
      b_skipped++;
    }
  }
  
  gettimeofday(&tv2, NULL);
  
  us = (tv2.tv_sec-tv1.tv_sec)*1000000 + (tv2.tv_usec-tv1.tv_usec);
  
  fprintf(stderr,
	  "Reading %d%% (%d/%d blocks %ld bytes) total time %ldus MB/s %.2f effective MB/s %.2f\n",
	  perc,
	  b_read, b_read+b_skipped, size,
	  us,
	  (double)b_read*BLOCKSIZE/us,
	  (double)size/us
	  );
  exit(0);
}