Skip to content

Instantly share code, notes, and snippets.

@ongardie
Last active August 29, 2015 13:56
Show Gist options
  • Save ongardie/9177853 to your computer and use it in GitHub Desktop.
Save ongardie/9177853 to your computer and use it in GitHub Desktop.
Microbenchmark that executes 1000 single-byte writes.
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
int fd = open("bench.dat", O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
fprintf(stderr, "open failed: %s\n", strerror(errno));
exit(1);
}
int r = posix_fallocate(fd, 0, 1000);
if (r != 0) {
fprintf(stderr, "fallocate failed: %s\n", strerror(r));
exit(1);
}
r = fsync(fd);
if (r != 0) {
fprintf(stderr, "fsync failed: %s\n", strerror(errno));
exit(1);
}
int i;
for (i = 0; i < 1000; ++i) {
r = write(fd, "a", 1);
if (r != 1) {
fprintf(stderr, "write failed: %s\n", strerror(errno));
exit(1);
}
r = fdatasync(fd);
if (r != 0) {
fprintf(stderr, "fdatasync failed: %s\n", strerror(errno));
exit(1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment