added demo of AIO/setresuid race
[tridge/junkcode.git] / readframes_seek.c
1 /* 
2    test reading image frames from a directory
3    tridge@samba.org, March 2006
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <sys/time.h>
11 #include <time.h>
12 #include <ctype.h>
13
14
15 static double total_time, min_time, max_time, total_bytes;
16 static int num_files;
17
18 static struct timeval tp1,tp2;
19
20 static void start_timer()
21 {
22         gettimeofday(&tp1,NULL);
23 }
24
25 static double end_timer()
26 {
27         gettimeofday(&tp2,NULL);
28         return((tp2.tv_sec - tp1.tv_sec) + 
29                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
30 }
31
32 static void test_file(const char *fname, int seeksize)
33 {
34         int fd;
35         unsigned char buf[64000];
36         double t;
37         double lastt = total_time;
38
39         fd = open(fname, O_RDONLY);
40         if (fd == -1) {
41                 printf("Failed to open %s\n", fname);
42                 return;
43         }
44
45         while (1) {
46                 start_timer();
47                 /* for the app, reading a frame involves 5 reads and a
48                    seek */
49                 read(fd, buf, 61440);
50                 read(fd, buf, 61440);
51                 read(fd, buf, 61440);
52                 read(fd, buf, 61440);
53                 if (read(fd, buf, 42240) != 42240) break;
54                 lseek(fd, seeksize, SEEK_CUR);
55                 total_bytes += 42240 + 4*61440;
56                 t = end_timer();
57                 total_time += t;
58                 if (t > max_time) {
59                         max_time = t;
60                 }
61                 if (min_time == 0 || t < min_time) {
62                         min_time = t;
63                 }
64                 if (total_time > lastt + 1.0) {
65                         lastt = total_time;
66                         printf("Worst: %.2fms  Best: %.2fms  throughput=%.2f MByte/sec\n", 
67                                max_time*1000.0, min_time*1000.0, 
68                                1.0e-6*total_bytes/total_time);
69                 }
70         }
71
72         num_files++;
73         close(fd);
74 }
75
76 int main(int argc, char* argv[])
77 {
78         int i;  
79         int seeksize;
80
81         printf("readframes_seek tester - tridge@samba.org\n");
82
83         setlinebuf(stdout);
84
85         if (argc < 3) {
86                 printf("Usage: readframes_seek <seeksize> <files>\n");
87                 exit(1);
88         }
89
90         seeksize = strtoul(argv[1], NULL, 0);
91
92         for (i=2;i<argc;i++) {
93                 test_file(argv[i], seeksize);
94         }
95
96         printf("\nProcessed %d files totalling %.2f MBytes with seeksize=%d\n", 
97                num_files, total_bytes/(1024*1024), seeksize);
98         printf("Worst: %.2fms  Best: %.2fms\n", max_time*1000.0, min_time*1000.0);
99         printf("Throughput %.2f MByte/sec\n", 1.0e-6*total_bytes/total_time);
100
101         return 0;
102 }