Debug swcsa
[SXSI/TextCollection.git] / swcsa / buildStats.c
1 /*
2  * Run Queries
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "buildFacade.h"
10
11 /* only for getTime() */
12 #include <sys/time.h>
13 #include <sys/resource.h>
14
15 /* macro to detect and to notify errors */
16 #define IFERROR(error) {{if (error) { fprintf(stderr, "%s\n", error_index(error)); exit(1); }}}
17
18
19 /* local headers */
20
21 double getTime (void);
22 void usage(char * progname);
23                 
24 static void *Index;      /* opauque data type */
25 static ulong Index_size, Text_length;
26 static double Load_time;
27
28
29
30 /*
31  * Temporary usage: run_queries <index file> <type> [length] [V]
32  */
33 int main (int argc, char *argv[])
34 {
35         int error = 0;
36         char *filename;
37         char querytype;
38         
39         if (argc != 2)  {
40                 usage(argv[0]); 
41                 exit (1);
42         }
43         
44         filename = argv[1];
45
46         printf("\n Stats of index: %s\n",argv[1]);
47         
48         Load_time = getTime ();
49         error = load_index (filename, &Index);
50         IFERROR (error);
51         Load_time = getTime () - Load_time;
52         fprintf (stderr, "\tLoad index time = %.2f secs\n", Load_time);
53
54         error = index_size(Index, &Index_size);
55         IFERROR (error);
56
57         error = get_length(Index, &Text_length);
58         IFERROR (error);
59         
60         
61         ulong text_len;
62         error = get_length(Index, &text_len);
63         IFERROR (error);
64         
65         error = printInfo(Index);
66         IFERROR(error);         
67         
68         error = free_index(Index);
69         IFERROR(error);
70         
71
72
73         fprintf(stdout,"\t===============================================\n");
74         fprintf(stdout,"\tInput: %lu bytes (text) --> Output %lu bytes (wcsa).\n", text_len, Index_size);
75         fprintf(stderr,"\tIndex size = %lu Kb\n", Index_size/1024);
76         fprintf(stdout,"\tOverall compression --> %.2f%% (%.2f bits per char).\n",
77                         (100.0*Index_size)/text_len, (Index_size*8.0)/text_len);
78         fprintf(stdout,"\t===============================================\n");
79
80
81         return 0;
82 }
83
84 double
85 getTime (void)
86 {
87         double usertime, systime;
88         struct rusage usage;
89
90         getrusage (RUSAGE_SELF, &usage);
91
92         usertime = (double) usage.ru_utime.tv_sec +
93                 (double) usage.ru_utime.tv_usec / 1000000.0;
94
95         systime = (double) usage.ru_stime.tv_sec +
96                 (double) usage.ru_stime.tv_usec / 1000000.0;
97
98         //return (usertime + systime);
99         return (usertime );
100 }
101
102
103 void usage(char * progname) {   
104         fprintf(stderr, "\nThe program loads <index> and shows some stats on it\n");
105         fprintf(stderr, "Usage:  %s <index> \n", progname);
106 }