Skip to content
This repository
tag: v0.4.3
Fetching contributors…

Cannot retrieve contributors at this time

file 110 lines (95 sloc) 2.234 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
* gcc -o iotest io.c
*/

#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
 
int tsize = 1000 * 1048576;
const char *path = "/tmp/wt.dat";

int c = 0;

char* bufit(size_t l)
{
  char *p = malloc(l);
  memset(p, '!', l);
  return p;
}

void writetest(int size, size_t bsize)
{
  int i;
  char *buf = bufit(bsize);
  struct timeval start, end;
  double elapsed;
  double mbps;

  int fd = open(path, O_CREAT|O_WRONLY, 0644);
  if (fd < 0) {
    perror("open failed");
    exit(254);
  }

  assert(0 == gettimeofday(&start, NULL));
  for (i = 0; i < size; i += bsize) {
    int rv = write(fd, buf, bsize);
    if (c++ % 2000 == 0) fprintf(stderr, ".");
    if (rv < 0) {
      perror("write failed");
      exit(254);
    }
  }
#ifdef __linux__
  fdatasync(fd);
#else
  fsync(fd);
#endif
  close(fd);
  assert(0 == gettimeofday(&end, NULL));
  elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
  mbps = ((tsize/elapsed)) / 1048576;
  fprintf(stderr, "\nWrote %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);

  free(buf);
}

void readtest(int size, size_t bsize)
{
  int i;
  char *buf = bufit(bsize);
  struct timeval start, end;
  double elapsed;
  double mbps;

  int fd = open(path, O_RDONLY, 0644);
  if (fd < 0) {
    perror("open failed");
    exit(254);
  }

  assert(0 == gettimeofday(&start, NULL));
  for (i = 0; i < size; i += bsize) {
    int rv = read(fd, buf, bsize);
    if (rv < 0) {
      perror("write failed");
      exit(254);
    }
  }
  close(fd);
  assert(0 == gettimeofday(&end, NULL));
  elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
  mbps = ((tsize/elapsed)) / 1048576;
  fprintf(stderr, "Read %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);

  free(buf);
}

void cleanup() {
  unlink(path);
}

int main()
{
  int i;
  int bsizes[] = {1024, 4096, 8192, 16384, 32768, 65536, 0};

  for (i = 0; bsizes[i] != 0; i++) {
    writetest(tsize, bsizes[i]);
  }
  for (i = 0; bsizes[i] != 0; i++) {
    readtest(tsize, bsizes[i]);
  }
  atexit(cleanup);
  return 0;
}
Something went wrong with that request. Please try again.