Fix position of content outline (#2108).
[dcpomatic.git] / hacks / direct_disk_speed_test.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 int test_posix(int block_size, int blocks, int gap, int rdisk, int nocache)
9 {
10         struct timeval start;
11         gettimeofday(&start, NULL);
12
13         int f = -1;
14         if (rdisk) {
15                 f = open("/dev/rdisk3", O_RDWR);
16         } else {
17                 f = open("/dev/disk3", O_RDWR);
18         }
19         if (f == -1) {
20                 printf("open failed.\n");
21                 return -1;
22         }
23
24         fcntl(f, F_NOCACHE, nocache);
25
26         void* buffer = malloc(block_size);
27         if (!buffer) {
28                 printf("malloc failed.\n");
29                 return -1;
30         }
31
32         for (int i = 0; i < blocks; ++i) {
33                 write(f, buffer, block_size);
34                 if (gap > 0) {
35                         lseek(f, gap, SEEK_CUR);
36                 }
37         }
38
39         close(f);
40
41         struct timeval end;
42         gettimeofday(&end, NULL);
43         float duration = ((end.tv_sec + end.tv_usec / 1e6) - (start.tv_sec + start.tv_usec / 1e6));
44         printf("POSIX: block_size=%d blocks=%d gap=%d rdisk=%d nocache=%d time=%f\n", block_size, blocks, gap, rdisk, nocache, duration);
45         return 0;
46 }
47
48
49 int test_stdio(int block_size, int blocks, int gap, int rdisk)
50 {
51         struct timeval start;
52         gettimeofday(&start, NULL);
53
54         FILE* f = NULL;
55         if (rdisk) {
56                 f = fopen("/dev/rdisk3", "r+b");
57         } else {
58                 f = fopen("/dev/disk3", "r+b");
59         }
60         if (!f) {
61                 printf("fopen failed.\n");
62                 return -1;
63         }
64
65         setbuf(f, 0);
66
67         void* buffer = malloc(block_size);
68         if (!buffer) {
69                 printf("malloc failed.\n");
70                 return -1;
71         }
72
73         for (int i = 0; i < blocks; ++i) {
74                 fwrite(buffer, block_size, 1, f);
75                 if (gap > 0) {
76                         fseek(f, gap, SEEK_CUR);
77                 }
78         }
79
80         fclose(f);
81
82         struct timeval end;
83         gettimeofday(&end, NULL);
84         float duration = ((end.tv_sec + end.tv_usec / 1e6) - (start.tv_sec + start.tv_usec / 1e6));
85         printf("STDIO: block_size=%d blocks=%d gap=%d rdisk=%d time=%f\n", block_size, blocks, gap, rdisk, duration);
86         return 0;
87 }
88
89
90 int main()
91 {
92         for (int i = 0; i < 2; i++) {
93 /*
94                 test_posix(4096, 4096, 0, i);
95                 test_posix(4096, 4096, 0, i);
96                 test_posix(8192, 2048, 0, i);
97                 test_posix(4096, 4096, 4096, i);
98                 test_posix(4096, 4096, 65536, i);
99                 test_posix(8192, 2048, 65536, i);
100                 test_posix(16384, 1024, 65536, i);
101 */
102                 for (int j = 0; j < 2; j++) {
103                         // test_posix(4096, 262144, 0, i, j);
104                         // test_posix(8192, 131072, 0, i, j);
105                         // test_posix(16384, 65536, 0, i, j);
106                         test_posix(32768, 32768, 0, i, j);
107                         test_posix(65536, 16384, 0, i, j);
108                         test_posix(131072, 8192, 0, i, j);
109                         test_posix(262144, 4096, 0, i, j);
110                         test_posix(524288, 2048, 0, i, j);
111                 }
112         }
113 }
114