events: fix typos in code comment
[platform/upstream/nodejs.git] / benchmark / io.c
1 /**
2  * gcc -o iotest io.c
3  */
4
5 #include <assert.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <sys/time.h>
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <stdio.h>
14  
15 static int c = 0;
16 static int tsize = 1000 * 1048576;
17 static const char path[] = "/tmp/wt.dat";
18 static char buf[65536];
19
20 static uint64_t now(void) {
21   struct timeval tv;
22
23   if (gettimeofday(&tv, NULL))
24     abort();
25
26   return tv.tv_sec * 1000000ULL + tv.tv_usec;
27 }
28
29 static void writetest(int size, size_t bsize)
30 {
31   int i;
32   uint64_t start, end;
33   double elapsed;
34   double mbps;
35
36   assert(bsize <= sizeof buf);
37
38   int fd = open(path, O_CREAT|O_WRONLY, 0644);
39   if (fd < 0) {
40     perror("open failed");
41     exit(254);
42   }
43
44   start = now();
45
46   for (i = 0; i < size; i += bsize) {
47     int rv = write(fd, buf, bsize);
48     if (c++ % 2000 == 0) fprintf(stderr, ".");
49     if (rv < 0) {
50       perror("write failed");
51       exit(254);
52     }
53   }
54
55 #ifndef NSYNC
56 # ifdef __linux__
57   fdatasync(fd);
58 # else
59   fsync(fd);
60 # endif
61 #endif /* SYNC */
62
63   close(fd);
64
65   end = now();
66   elapsed = (end - start) / 1e6;
67   mbps = ((tsize/elapsed)) / 1048576;
68
69   fprintf(stderr, "\nWrote %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
70 }
71
72 void readtest(int size, size_t bsize)
73 {
74   int i;
75   uint64_t start, end;
76   double elapsed;
77   double mbps;
78
79   assert(bsize <= sizeof buf);
80
81   int fd = open(path, O_RDONLY, 0644);
82   if (fd < 0) {
83     perror("open failed");
84     exit(254);
85   }
86
87   start = now();
88
89   for (i = 0; i < size; i += bsize) {
90     int rv = read(fd, buf, bsize);
91     if (rv < 0) {
92       perror("write failed");
93       exit(254);
94     }
95   }
96   close(fd);
97
98   end = now();
99   elapsed = (end - start) / 1e6;
100   mbps = ((tsize/elapsed)) / 1048576;
101
102   fprintf(stderr, "Read %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
103 }
104
105 void cleanup() {
106   unlink(path);
107 }
108
109 int main()
110 {
111   int i;
112   int bsizes[] = {1024, 4096, 8192, 16384, 32768, 65536, 0};
113
114   for (i = 0; bsizes[i] != 0; i++) {
115     writetest(tsize, bsizes[i]);
116   }
117   for (i = 0; bsizes[i] != 0; i++) {
118     readtest(tsize, bsizes[i]);
119   }
120   atexit(cleanup);
121   return 0;
122 }