Skip to content
This repository
Newer
Older
100644 110 lines (95 sloc) 2.234 kb
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
1 /**
2 * gcc -o iotest io.c
3 */
4
5 #include <unistd.h>
6 #include <string.h>
7 #include <fcntl.h>
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
8 #include <sys/time.h>
9 #include <assert.h>
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 int tsize = 1000 * 1048576;
14 const char *path = "/tmp/wt.dat";
15
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
16 int c = 0;
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
17
18 char* bufit(size_t l)
19 {
20 char *p = malloc(l);
21 memset(p, '!', l);
22 return p;
23 }
24
25 void writetest(int size, size_t bsize)
26 {
27 int i;
28 char *buf = bufit(bsize);
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
29 struct timeval start, end;
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
30 double elapsed;
31 double mbps;
32
33 int fd = open(path, O_CREAT|O_WRONLY, 0644);
34 if (fd < 0) {
35 perror("open failed");
36 exit(254);
37 }
38
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
39 assert(0 == gettimeofday(&start, NULL));
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
40 for (i = 0; i < size; i += bsize) {
41 int rv = write(fd, buf, bsize);
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
42 if (c++ % 2000 == 0) fprintf(stderr, ".");
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
43 if (rv < 0) {
44 perror("write failed");
45 exit(254);
46 }
47 }
48 #ifdef __linux__
49 fdatasync(fd);
50 #else
51 fsync(fd);
52 #endif
53 close(fd);
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
54 assert(0 == gettimeofday(&end, NULL));
55 elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
56 mbps = ((tsize/elapsed)) / 1048576;
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
57 fprintf(stderr, "\nWrote %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
58
59 free(buf);
60 }
61
62 void readtest(int size, size_t bsize)
63 {
64 int i;
65 char *buf = bufit(bsize);
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
66 struct timeval start, end;
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
67 double elapsed;
68 double mbps;
69
70 int fd = open(path, O_RDONLY, 0644);
71 if (fd < 0) {
72 perror("open failed");
73 exit(254);
74 }
75
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
76 assert(0 == gettimeofday(&start, NULL));
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
77 for (i = 0; i < size; i += bsize) {
78 int rv = read(fd, buf, bsize);
79 if (rv < 0) {
80 perror("write failed");
81 exit(254);
82 }
83 }
84 close(fd);
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
85 assert(0 == gettimeofday(&end, NULL));
86 elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
87 mbps = ((tsize/elapsed)) / 1048576;
353e2565 » ry
2010-09-24 Fix timing on I/O benchmark
88 fprintf(stderr, "Read %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
41ad3391 » ry
2010-08-17 Add Paul Querna's io benchmarks
89
90 free(buf);
91 }
92
93 void cleanup() {
94 unlink(path);
95 }
96
97 int main()
98 {
99 int i;
100 int bsizes[] = {1024, 4096, 8192, 16384, 32768, 65536, 0};
101
102 for (i = 0; bsizes[i] != 0; i++) {
103 writetest(tsize, bsizes[i]);
104 }
105 for (i = 0; bsizes[i] != 0; i++) {
106 readtest(tsize, bsizes[i]);
107 }
108 atexit(cleanup);
109 return 0;
110 }
Something went wrong with that request. Please try again.