Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Test multiple iterations of fsync-ing small amounts of data
- gcc [filename] -lrt
- */
- #include <unistd.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <time.h>
- #include <stdio.h>
- double timespecdif(const struct timespec *start,const struct timespec *end) {
- time_t tdif=difftime(end->tv_sec,start->tv_sec);
- long ndif=end->tv_nsec-start->tv_nsec;
- if (ndif<0) {tdif-=1; ndif+=1000000000;}
- return (double)tdif+ndif*0.000000001;
- }
- int main(int argc,char* argv[]) {
- double o;
- int i;
- int iterations=16;
- struct timespec start,end,prev;
- int file=open("test",O_WRONLY);
- if (argc>1) {
- int q=atoi(argv[1]);
- if (q) iterations=q;
- }
- printf("Doing %d iterations.\n",iterations);
- clock_gettime(CLOCK_MONOTONIC,&end);
- memcpy(&start,&end,sizeof(start));
- memcpy(&prev,&end,sizeof(start));
- for (i=0; i<iterations; i++) {
- write(file,"abcd",4);
- fsync(file);
- clock_gettime(CLOCK_MONOTONIC,&end);
- o=timespecdif(&prev,&end);
- memcpy(&prev,&end,sizeof(end));
- printf("%.03f\n",o);
- fflush(stdout);
- }
- clock_gettime(CLOCK_MONOTONIC,&end);
- o=timespecdif(&start,&end);
- printf("Total time taken %.03f seconds.\n",o);
- close(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement