(unnamed)

text/plain

Filename: (unnamed)
Type: text/plain
Part: 1
Message: Re: [HACKERS] WAL logging problem in 9.4.3?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <fcntl.h>

//#define FILE_SIZE (16 * 1024 * 1024)
//#define LOOPS 100

#define FILE_SIZE (64 * 1024)
#define LOOPS 1000

//#define FILE_SIZE (8 * 1024)
//#define LOOPS 1000

//#define FILE_SIZE (1 * 1024)
//#define LOOPS 1000

//#define FILE_SIZE (512)
//#define LOOPS 1000

//#define FILE_SIZE (128)
//#define LOOPS 1000

char buf[FILE_SIZE];
char fname[256];

int main(void) {
  int i, j;
  int fd = -1;
  struct timeval st, ed;
  double accum = 0.0;
  int bufperfile = (int)((16.0 * 1024 * 1024) / FILE_SIZE);

  for (i = 0 ; i < LOOPS ; i++) {
	snprintf(fname, 256, "test%03d.file", i);
	unlink(fname); // ignore errors
  }

  for (i = 0 ; i < LOOPS ; i++) {
	for (j = 0 ; j < FILE_SIZE ; j++)
	  buf[j] = random()* 256;

	if (i % bufperfile == 0) {
	  if (fd >= 0)
		close(fd);

	  snprintf(fname, 256, "test%03d.file", i / bufperfile);
	  fd = open(fname, O_CREAT | O_RDWR, 0644);
	  if (fd < 0) {
		fprintf(stderr, "open error: %m\n");
		exit(1);
	  }
	  memset(buf, 0, sizeof(buf));
	  if (write(fd, buf, sizeof(buf)) < 0) {
		fprintf(stderr, "init write error: %m\n");
		exit(1);
	  }
	  if (fsync(fd) < 0) {
		fprintf(stderr, "init fsync error: %m\n");
		exit(1);
	  }
	  if (lseek(fd, 0, SEEK_SET) < 0) {
		fprintf(stderr, "init lseek error: %m\n");
		exit(1);
	  }
	  
	}

	gettimeofday(&st, NULL);
	if (write(fd, buf, FILE_SIZE) < 0) {
	  fprintf(stderr, "write error: %m\n");
	  exit(1);
	}
	if (fdatasync(fd) < 0) {
	  fprintf(stderr, "fdatasync error: %m\n");
	  exit(1);
	}
	gettimeofday(&ed, NULL);

	accum += (double)((ed.tv_sec - st.tv_sec) * 1000.0 +
					  (ed.tv_usec - st.tv_usec) / 1000.0);
  }

  printf("%.2lf ms for %d %dkB-records (%d MB), %.2lf ms per %dkB)\n",
		 accum, i, FILE_SIZE / 1024, i * FILE_SIZE, accum / i, FILE_SIZE / 1024);

  return 0;
}