Advertisement
Guest User

Test multiple iterations of fsync-ing small amounts of data

a guest
Oct 15th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. /*
  2. Test multiple iterations of fsync-ing small amounts of data
  3.  
  4. gcc [filename] -lrt
  5. */
  6.  
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <time.h>
  12. #include <stdio.h>
  13.  
  14. double timespecdif(const struct timespec *start,const struct timespec *end) {
  15. time_t tdif=difftime(end->tv_sec,start->tv_sec);
  16. long ndif=end->tv_nsec-start->tv_nsec;
  17. if (ndif<0) {tdif-=1; ndif+=1000000000;}
  18. return (double)tdif+ndif*0.000000001;
  19. }
  20.  
  21. int main(int argc,char* argv[]) {
  22. double o;
  23. int i;
  24. int iterations=16;
  25. struct timespec start,end,prev;
  26. int file=open("test",O_WRONLY);
  27. if (argc>1) {
  28. int q=atoi(argv[1]);
  29. if (q) iterations=q;
  30. }
  31. printf("Doing %d iterations.\n",iterations);
  32. clock_gettime(CLOCK_MONOTONIC,&end);
  33. memcpy(&start,&end,sizeof(start));
  34. memcpy(&prev,&end,sizeof(start));
  35. for (i=0; i<iterations; i++) {
  36. write(file,"abcd",4);
  37. fsync(file);
  38. clock_gettime(CLOCK_MONOTONIC,&end);
  39. o=timespecdif(&prev,&end);
  40. memcpy(&prev,&end,sizeof(end));
  41. printf("%.03f\n",o);
  42. fflush(stdout);
  43. }
  44. clock_gettime(CLOCK_MONOTONIC,&end);
  45. o=timespecdif(&start,&end);
  46. printf("Total time taken %.03f seconds.\n",o);
  47. close(file);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement