Created
August 27, 2014 13:37
-
-
Save itiut/a03d8346e502b5c4a4c2 to your computer and use it in GitHub Desktop.
a prototype of microbenchmark for sequential write
This file contains hidden or 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
#include <fcntl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include <sys/time.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
inline void xgettimeofday(struct timeval *tv) { | |
if (gettimeofday(tv, NULL) == -1) { | |
perror("gettimeofday(2)"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
inline void xwrite(int fd, const void *buf, size_t count) { | |
if (write(fd, buf, count) == -1) { | |
perror("write(2)"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
inline void xfsync(int fd) { | |
if (fsync(fd) == -1) { | |
perror("fsync(2)"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
void do_write(int fd, size_t each_write_bytes, int count, struct timeval *tvs) { | |
char *buffer = (char *) calloc(count, sizeof(char)); | |
if (buffer == NULL) { | |
perror("calloc(3)"); | |
exit(EXIT_FAILURE); | |
} | |
xgettimeofday(&tvs[0]); | |
for (int i = 0; i < count; i++) { | |
xwrite(fd, buffer, each_write_bytes); | |
xfsync(fd); | |
xgettimeofday(&tvs[i + 1]); | |
} | |
free(buffer); | |
} | |
double to_f(struct timeval tv) { | |
return tv.tv_sec + tv.tv_usec * 1e-6; | |
} | |
int main(int argc, char *argv[]) { | |
const char *out_file = "/tmp/write.out"; | |
int each_write_bytes = 8192; | |
int count = 1000; | |
struct timeval *tvs = (struct timeval *) calloc(count + 1, sizeof(struct timeval)); | |
if (tvs == NULL) { | |
perror("calloc(3)"); | |
exit(EXIT_FAILURE); | |
} | |
int fd = open(out_file, O_WRONLY | O_CREAT, 0664); | |
if (fd == -1) { | |
perror("open(2)"); | |
exit(EXIT_FAILURE); | |
} | |
do_write(fd, each_write_bytes, count, tvs); | |
if (close(fd) == -1) { | |
perror("close(2)"); | |
exit(EXIT_FAILURE); | |
} | |
printf("start: %f\n", to_f(tvs[0])); | |
printf("end: %f\n", to_f(tvs[count])); | |
free(tvs); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment