(unnamed)
text/plain
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
//#define FILE_SIZE (16 * 1024 * 1024)
//#define LOOPS 100
//#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];
int main(void) {
int i;
int fd = -1;
double accum = 0.0;
struct timeval st, ed;
for (i = 0 ; i < LOOPS ; i++) {
char fname[256];
snprintf(fname, 256, "test%03d.file", i);
unlink(fname); // ignore errors
}
for (i = 0 ; i < LOOPS ; i++) {
char fname[256];
int j;
snprintf(fname, 256, "test%03d.file", i);
for (j = 0 ; j < FILE_SIZE ; j++)
buf[j] = random()* 256;
if (fd >= 0)
close(fd);
gettimeofday(&st, NULL);
fd = open(fname, O_CREAT | O_RDWR, 0644);
if (fd < 0) {
fprintf(stderr, "open error: %m\n");
exit(1);
}
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);
}
if (lseek(fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "lseek 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-files (%d MB), %.2lf ms per %dkB)\n",
accum, i, FILE_SIZE / 1024, i * FILE_SIZE, accum / i, FILE_SIZE / 1024);
return 0;
}