Project

General

Profile

Bug #20938 » timed_openrw_read.c

C test program to reproduce issue - Andras Pataki, 08/07/2017 03:26 PM

 
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#define INTERVAL 2


double now()
{
struct timeval tv;
gettimeofday(&tv, NULL);

return tv.tv_sec + tv.tv_usec / 1e6;
}


int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <filename> r|rw\n", argv[0]);
exit(1);
}

const char *filename = argv[1];

int mode = 0;
if (strcmp(argv[2], "r") == 0) {
mode = O_RDONLY;
} else if (strcmp(argv[2], "rw") == 0) {
mode = O_RDWR;
} else {
fprintf(stderr, "Second argument must be 'r' or 'rw'\n");
exit(1);
}

while (1) {
char buffer[100];
double t0 = now();
double dt;
int count = 0;

while (1) {
dt = now() - t0;
if (dt > INTERVAL) {
break;
}

int fd = open(filename, mode);
if (fd < 0) {
printf("Could not open file '%s' for read/write", filename);
exit(1);
}
read(fd, buffer, 100);
close(fd);
count++;
}

printf("File open rate: %8.2f\n", count / dt);

}

return 0;
}
(1-1/2)