Skip to content

Instantly share code, notes, and snippets.

@ideawu
Created May 13, 2020 15:25
Show Gist options
  • Save ideawu/e089f785abf515a1bfb303b3c42b3160 to your computer and use it in GitHub Desktop.
Save ideawu/e089f785abf515a1bfb303b3c42b3160 to your computer and use it in GitHub Desktop.
fsync benchmark
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <thread>
#include <mutex>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
double microtime() {
struct timeval now;
gettimeofday(&now, NULL);
double ret = now.tv_sec + now.tv_usec / 1000.0 / 1000.0;
return ret;
}
int main(int argc, char** argv) {
if(argc < 2){
printf("Usage: %s filename count\n", argv[0]);
exit(1);
}
const char *filename = argv[1];
int fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0644);
if(fd == -1){
printf("%d error: %s\n", __LINE__, strerror(errno));
exit(1);
}
int SIZE = 1000;
if(argc > 2){
SIZE = atoi(argv[2]);
}
double stime = microtime();
for(int i=0; i<SIZE; i++){
int ret;
ret = write(fd, "1234567890\n", 11);
if(ret == -1){
printf("%d error: %s\n", __LINE__, strerror(errno));
exit(1);
}
ret = fsync(fd);
if(ret == -1){
printf("%d error: %s\n", __LINE__, strerror(errno));
exit(1);
}
}
double etime = microtime();
printf("time: %.3fs, qps: %.0f\n", (etime - stime), SIZE / (etime - stime));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment