Created
January 30, 2018 11:40
-
-
Save jsteemann/c1f80caef331a6d10469bc066ade78aa to your computer and use it in GitHub Desktop.
quick test program to assess disk write performance with and without fdatasync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
write performance test | |
tests disk write performance with and without fdatasync | |
will write a configurable amount of 1MB data blocks to disk | |
and then optionally call fdatasync after each | |
usage: | |
compile with | |
gcc -O3 -Wall -Wextra -std=c11 test-write-performance.c | |
and then run with | |
time ./a.out /tmp/out 1000 0 | |
or | |
time ./a.out /tmp/out 1000 1 | |
The invocation parameters are: | |
- output file (make sure to use an outfile on the disk of interest) | |
- number of 1MB blocks to write | |
- use fdatasync (0 = off, 1 = on) | |
*/ | |
#define _POSIX_C_SOURCE 200112L | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#define MB (1024ULL * 1024ULL) | |
int main(int argc, char* argv[]) { | |
if (argc < 4) { | |
fprintf(stderr, "usage: %s outfile mb sync\n", argv[0]); | |
exit(1); | |
} | |
char const* outfile = argv[1]; | |
int mb = atoi(argv[2]); | |
if (mb < 0) { | |
fprintf(stderr, "usage: %s outfile mb sync\n", argv[0]); | |
exit(1); | |
} | |
int sync = atoi(argv[3]); | |
if (sync != 0 && sync != 1) { | |
fprintf(stderr, "usage: %s outfile mb sync\n", argv[0]); | |
exit(1); | |
} | |
char block[MB]; // 1 mb; | |
memset(&block[0], 0xa5, sizeof(block)); | |
FILE* fp = fopen(outfile, "w"); | |
if (fp == NULL) { | |
fprintf(stderr, "error while opening file: %s\n", strerror(errno)); | |
exit(1); | |
} | |
int fd = fileno(fp); | |
if (fd < 0) { | |
fprintf(stderr, "error determining file descriptor\n"); | |
exit(1); | |
} | |
fprintf(stdout, "about to write %llu bytes to %s %s sync\n", (unsigned long long) (mb * MB), outfile, sync ? "with" : "without"); | |
int total = 0; | |
for (int i = 0; i < mb; ++i) { | |
size_t written = fwrite(&block[0], sizeof(block), 1, fp); | |
if (ferror(fp) != 0) { | |
fprintf(stderr, "file output error\n"); | |
exit(1); | |
} | |
if (sync) { | |
if (fdatasync(fd) == -1) { | |
fprintf(stderr, "error while syncing file: %s\n", strerror(errno)); | |
exit(1); | |
} | |
} | |
total += written; | |
fprintf(stdout, "written %llu bytes of %llu\n", (unsigned long long) (total * MB), (unsigned long long) (mb * MB)); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment