Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

writetest.cs

00001 /*
00002  * writetest --
00003  *
00004  * $Id: writetest.cs,v 10.8 2005/09/13 18:15:36 bostic Exp $
00005  */
00006 #include <sys/types.h>
00007 #include <sys/time.h>
00008 
00009 #include <errno.h>
00010 #include <fcntl.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <unistd.h>
00015 
00016 void usage(void);
00017 
00018 int
00019 main(argc, argv)
00020         int argc;
00021         char *argv[];
00022 {
00023         struct timeval start_time, end_time;
00024         double usecs;
00025         long val;
00026         int bytes, ch, cnt, fd, ops;
00027         char *fname, buf[100 * 1024];
00028 
00029         bytes = 256;
00030         fname = "testfile";
00031         ops = 1000;
00032         while ((ch = getopt(argc, argv, "b:f:o:")) != EOF)
00033                 switch (ch) {
00034                 case 'b':
00035                         if ((bytes = atoi(optarg)) > sizeof(buf)) {
00036                                 fprintf(stderr,
00037                                     "max -b option %d\n", sizeof(buf));
00038                                 exit (1);
00039                         }
00040                         break;
00041                 case 'f':
00042                         fname = optarg;
00043                         break;
00044                 case 'o':
00045                         if ((ops = atoi(optarg)) <= 0) {
00046                                 fprintf(stderr, "illegal -o option value\n");
00047                                 exit (1);
00048                         }
00049                         break;
00050                 case '?':
00051                 default:
00052                         usage();
00053                 }
00054         argc -= optind;
00055         argv += optind;
00056 
00057         (void)unlink(fname);
00058         if ((fd = open(fname, O_RDWR | O_CREAT, 0666)) == -1) {
00059                 perror(fname);
00060                 exit (1);
00061         }
00062 
00063         memset(buf, 0, bytes);
00064 
00065         printf("Running: %d ops\n", ops);
00066 
00067         (void)gettimeofday(&start_time, NULL);
00068         for (cnt = 0; cnt < ops; ++cnt) {
00069                 if (write(fd, buf, bytes) != bytes) {
00070                         fprintf(stderr, "write: %s\n", strerror(errno));
00071                         exit (1);
00072                 }
00073                 if (lseek(fd, (off_t)0, SEEK_SET) == -1) {
00074                         fprintf(stderr, "lseek: %s\n", strerror(errno));
00075                         exit (1);
00076                 }
00077                 if (fsync(fd) != 0) {
00078                         fprintf(stderr, "fsync: %s\n", strerror(errno));
00079                         exit (1);
00080                 }
00081         }
00082         (void)gettimeofday(&end_time, NULL);
00083 
00084         /*
00085          * Guarantee end_time fields are greater than or equal to start_time
00086          * fields.
00087          */
00088         if (end_time.tv_sec > start_time.tv_sec) {
00089                 --end_time.tv_sec;
00090                 end_time.tv_usec += 1000000;
00091         }
00092 
00093         /* Display elapsed time. */
00094         val = end_time.tv_sec - start_time.tv_sec;
00095         printf("Elapsed time: %ld:", val / (60 * 60));
00096         val %= 60 * 60;
00097         printf("%ld:", val / 60);
00098         val %= 60;
00099         printf("%ld.%ld\n", val, end_time.tv_usec - start_time.tv_usec);
00100 
00101         /* Display operations per second. */
00102         usecs =
00103             (end_time.tv_sec - start_time.tv_sec) * 1000000 +
00104             (end_time.tv_usec - start_time.tv_usec);
00105         printf("%d operations: %7.2f operations per second\n",
00106             ops, (ops / usecs) * 1000000);
00107 
00108         (void)unlink(fname);
00109         exit (0);
00110 }
00111 
00112 void
00113 usage()
00114 {
00115         (void)fprintf(stderr,
00116             "usage: testfile [-b bytes] [-f file] [-o ops]\n");
00117         exit(1);
00118 }

Generated on Sun Dec 25 12:14:24 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2