Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (98 sloc) 2.177 kb
41ad339 ry Add Paul Querna's io benchmarks
ry authored
1 /**
2 * gcc -o iotest io.c
3 */
4
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
5 #include <assert.h>
41ad339 ry Add Paul Querna's io benchmarks
ry authored
6 #include <unistd.h>
7 #include <string.h>
8 #include <fcntl.h>
353e256 ry Fix timing on I/O benchmark
ry authored
9 #include <sys/time.h>
10 #include <assert.h>
41ad339 ry Add Paul Querna's io benchmarks
ry authored
11 #include <stdlib.h>
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
12 #include <stdint.h>
41ad339 ry Add Paul Querna's io benchmarks
ry authored
13 #include <stdio.h>
14
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
15 static int c = 0;
16 static int tsize = 1000 * 1048576;
17 static const char path[] = "/tmp/wt.dat";
18 static char buf[65536];
41ad339 ry Add Paul Querna's io benchmarks
ry authored
19
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
20 static uint64_t now(void) {
21 struct timeval tv;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
22
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
23 if (gettimeofday(&tv, NULL))
24 abort();
25
26 return tv.tv_sec * 1000000ULL + tv.tv_usec;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
27 }
28
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
29 static void writetest(int size, size_t bsize)
41ad339 ry Add Paul Querna's io benchmarks
ry authored
30 {
31 int i;
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
32 uint64_t start, end;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
33 double elapsed;
34 double mbps;
35
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
36 assert(bsize <= sizeof buf);
37
41ad339 ry Add Paul Querna's io benchmarks
ry authored
38 int fd = open(path, O_CREAT|O_WRONLY, 0644);
39 if (fd < 0) {
40 perror("open failed");
41 exit(254);
42 }
43
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
44 start = now();
45
41ad339 ry Add Paul Querna's io benchmarks
ry authored
46 for (i = 0; i < size; i += bsize) {
47 int rv = write(fd, buf, bsize);
353e256 ry Fix timing on I/O benchmark
ry authored
48 if (c++ % 2000 == 0) fprintf(stderr, ".");
41ad339 ry Add Paul Querna's io benchmarks
ry authored
49 if (rv < 0) {
50 perror("write failed");
51 exit(254);
52 }
53 }
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
54
55 #ifndef NSYNC
56 # ifdef __linux__
41ad339 ry Add Paul Querna's io benchmarks
ry authored
57 fdatasync(fd);
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
58 # else
41ad339 ry Add Paul Querna's io benchmarks
ry authored
59 fsync(fd);
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
60 # endif
61 #endif /* SYNC */
62
ed5bad7 Ben Noordhuis bench: fix use of fd after close
bnoordhuis authored
63 close(fd);
64
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
65 end = now();
66 elapsed = (end - start) / 1e6;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
67 mbps = ((tsize/elapsed)) / 1048576;
68
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
69 fprintf(stderr, "\nWrote %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
41ad339 ry Add Paul Querna's io benchmarks
ry authored
70 }
71
72 void readtest(int size, size_t bsize)
73 {
74 int i;
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
75 uint64_t start, end;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
76 double elapsed;
77 double mbps;
78
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
79 assert(bsize <= sizeof buf);
80
41ad339 ry Add Paul Querna's io benchmarks
ry authored
81 int fd = open(path, O_RDONLY, 0644);
82 if (fd < 0) {
83 perror("open failed");
84 exit(254);
85 }
86
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
87 start = now();
88
41ad339 ry Add Paul Querna's io benchmarks
ry authored
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);
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
97
98 end = now();
99 elapsed = (end - start) / 1e6;
41ad339 ry Add Paul Querna's io benchmarks
ry authored
100 mbps = ((tsize/elapsed)) / 1048576;
101
78ca555 Ben Noordhuis bench: optimize io.c benchmark
bnoordhuis authored
102 fprintf(stderr, "Read %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
41ad339 ry Add Paul Querna's io benchmarks
ry authored
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 }
Something went wrong with that request. Please try again.