more like samba offline test
[tridge/junkcode.git] / readframes.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
13
14 #define READ_SIZE 61440
15
16 static double total_time, min_time, max_time, total_bytes;
17 static int num_files;
18
19 static struct timeval tp1,tp2;
20
21 static void start_timer()
22 {
23         gettimeofday(&tp1,NULL);
24 }
25
26 static double end_timer()
27 {
28         gettimeofday(&tp2,NULL);
29         return((tp2.tv_sec - tp1.tv_sec) + 
30                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
31 }
32
33 static void test_file(const char *fname)
34 {
35         int fd;
36         unsigned char buf[READ_SIZE];
37         
38         double t;
39         int n;
40
41         start_timer();
42         fd = open(fname, O_RDONLY);
43         if (fd == -1) {
44                 printf("Failed to open %s\n", fname);
45                 return;
46         }
47         while ((n=read(fd, buf, READ_SIZE)) > 0) {
48                 total_bytes += n;
49         }
50         close(fd);
51         t = end_timer();
52
53         if (t > max_time) max_time = t;
54         if (min_time == 0 || t < min_time) min_time = t;
55         total_time += t;
56         num_files++;
57
58         printf("%6.2fms %s\n", t*1000.0, fname);
59 }
60
61 static int name_cmp(char **n1, char **n2)
62 {
63         const char *s1=*n1, *s2=*n2;
64         /* try to do numerical sorting */
65         while (*s1 && *s1 == *s2) {
66                 s1++; s2++;
67         }
68         if (isdigit(*s1) || isdigit(*s2)) {
69                 return atoi(s1) - atoi(s2);
70         }
71         return strcmp(s1, s2);
72 }
73
74 int main(int argc, char* argv[])
75 {
76         int i;  
77
78         printf("readframes tester - tridge@samba.org\n");
79
80         if (argc < 2) {
81                 printf("Usage: readframes <files>\n");
82                 exit(1);
83         }
84
85         qsort(&argv[1], argc-1, sizeof(char *), name_cmp);
86
87         for (i=1;i<argc;i++) {
88                 test_file(argv[i]);
89         }
90
91         printf("\nProcessed %d files totalling %.2f MBytes\n", 
92                num_files, total_bytes/(1024*1024));
93         printf("Speed was %.2f files/sec\n", num_files/total_time);
94         printf("Average speed was %.2fms per file\n", 1000.0*total_time/num_files);
95         printf("Worst: %.2fms  Best: %.2fms\n", max_time*1000.0, min_time*1000.0);
96
97         return 0;
98 }