Debug swcsa
[SXSI/TextCollection.git] / timeTextCollection.cpp
1 // Test driver for text collection
2 #include <iostream>
3 #include <cstdlib>
4 #include <string>
5 using std::cout;
6 using std::endl;
7 using std::cin;
8 using std::string;
9 #include <sys/time.h>
10 #include <time.h>
11 #include <stdio.h>
12 #include <unistd.h>
13
14 static struct timeval t1;
15 static struct timeval t2;
16
17
18 #define STARTTIMER() (gettimeofday(&t1,NULL))
19 #define STOPTIMER()  (gettimeofday(&t2,NULL))
20 #define GETTIME()    (((t2.tv_sec  - t1.tv_sec) * 1000000.0 + (t2.tv_usec  - t1.tv_usec))/1000.0)
21
22 void *  last_brk = NULL;
23
24 long int get_mem(){
25   void * current_brk = sbrk(0);
26   long int mem = ((long int) current_brk ) - ((long int) last_brk);
27   //last_brk = current_brk;
28   return (mem/1024/1024);
29 }
30
31 #include "TextCollectionBuilder.h"
32 using SXSI::TextCollection;
33 using SXSI::TextCollectionBuilder;
34 string words[] =  { "Bakst", 
35                     "ruminants", "morphine", "AUSTRALIA",
36                     "molecule" ,"brain", "human", "blood","from",
37                     "with", " in", "the", "of",
38                       "a",
39                       "\n" };
40
41
42 unsigned int NWORDS = 15;
43
44
45 void time_tc(TextCollection *tc){
46   double time;
47   int count;
48   bool is;
49   TextCollection::document_result res;
50   for (unsigned int i = 0; i < NWORDS; i++){
51   std::cerr << "\"" << words[i] << "\": "; 
52   STARTTIMER();
53   is = tc->IsContains((unsigned char*) words[i].c_str());
54   STOPTIMER();
55   time = GETTIME();
56   
57   std::cerr << is << ", " << time << ", ";
58   
59   
60   STARTTIMER();
61   count = tc->Count((unsigned char*) words[i].c_str());
62   STOPTIMER();
63   time = GETTIME();
64   
65   std::cerr << count << ", " << time << ", ";
66   
67   
68   STARTTIMER();
69   count = tc->CountContains((unsigned char*) words[i].c_str());
70   STOPTIMER();
71   time = GETTIME();
72   
73   std::cerr << count << ", " << time << ", ";
74   
75   
76   STARTTIMER();
77   res = tc->Contains((unsigned char*) words[i].c_str());
78   STOPTIMER();
79   time = GETTIME();
80   
81   std::cerr << time << ", max_mem = " << get_mem() << "\n" ;
82   };
83 }
84
85 int main(int argc, char**argv)
86 {
87   string * str = new string("Foo");
88   string * buffer = new string("Foo");
89   unsigned int text_size = 0;
90   unsigned int max_str = 0;
91   unsigned int num_str = 0;
92   double time;
93   FILE* file;
94   TextCollection * tc;
95  
96
97   TextCollectionBuilder *tcb64 = new TextCollectionBuilder(64);
98   TextCollectionBuilder *tcb2 = new TextCollectionBuilder(5);
99
100   STARTTIMER();
101   last_brk= sbrk(0);
102
103   std::cerr << "Filling collection\n";
104
105   while (not(cin.eof()) ){
106     std::getline(cin,  *str ); // Read line by line.    
107     str->append("\n");
108     
109     if (str->compare("----------\n") == 0 ){
110       tcb64->InsertText((unsigned char*) buffer->c_str());
111       tcb2->InsertText((unsigned char*) buffer->c_str());
112
113       if (num_str % 10000 == 0){
114         STOPTIMER();
115         time = GETTIME();
116         std::cerr << "Added " << num_str << " strings in "
117                   << time  << " ms, max_mem=" << get_mem() << "\n";
118         std::cerr.flush();
119         //STARTTIMER();
120       };
121       
122       num_str++;
123       if (max_str < buffer->size())
124         max_str = buffer->size();
125       text_size += buffer->size();
126       buffer->clear();
127     }     
128     else 
129       buffer->append(*str);
130   };
131   delete str;
132   delete buffer;
133   buffer = NULL;
134   str = NULL;
135
136   std::cerr << "Freeing text buffers : max_mem = " << get_mem() << "\n";
137
138   std::cerr << "Number of bytes inserted : " << text_size << "\n";
139
140   std::cerr << "Calling InitTextCollection() for sf=64: ";
141   STARTTIMER();
142   tc = tcb64->InitTextCollection();
143   STOPTIMER();
144   time = GETTIME();
145   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
146   delete tcb64;
147   tcb64 = NULL;
148
149   file = fopen("index_64.tc","w+");
150   std::cerr << "Saving to index_64.tc ";
151   STARTTIMER();
152   tc->Save(file);
153   STOPTIMER();   
154   time = GETTIME();
155   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
156   fclose(file);
157   delete tc;
158   tc = NULL;
159   std::cerr << "Freeing memory : max_mem = " << get_mem() << "\n";
160
161   std::cerr << "Calling InitTextCollection() for sf=5: ";
162   STARTTIMER();
163   tc = tcb2->InitTextCollection();
164   STOPTIMER();
165   time = GETTIME();
166   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
167   free(tcb2);
168   tcb2=NULL;
169
170
171   file = fopen("index_05.tc","w+");
172   std::cerr << "Saving to index_05.tc ";
173   STARTTIMER();
174   tc->Save(file);
175   STOPTIMER();   
176   time = GETTIME();
177   std::cerr << time << "ms, max_mem = " << get_mem() << "\n";
178   fclose(file);
179   delete tc;
180   tc = NULL;
181   std::cerr << "Freeing memory : max_mem = " << get_mem() << "\n";
182   std::cerr << "Statistics: " << num_str << " strings, " << max_str << " = max length\n";
183
184
185   std::cerr << "Loading sf=5 TextCollection ";
186   STARTTIMER();
187   file = fopen("index_05.tc","r");
188   tc = TextCollection::Load(file,5); // sample rate is not used.
189   STOPTIMER();
190   time = GETTIME();
191   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
192   fclose(file);
193   std::cerr << "-----------------\nSampling rate 5\n";
194   time_tc(tc);
195   delete tc;
196   tc = NULL;
197   std::cerr << "Freeing memory : max_mem = " << get_mem() << "\n";
198
199   std::cerr << "Loading sf=64 TextCollection ";
200   STARTTIMER();
201   file = fopen("index_64.tc","r");
202   tc = TextCollection::Load(file,64);
203   STOPTIMER();
204   time = GETTIME();
205   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
206   fclose(file);
207   std::cerr << "-----------------\nSampling rate 64\n";
208   time_tc(tc);
209   delete tc;
210   tc = NULL;
211   std::cerr << "Freeing memory : max_mem = " << get_mem() << "\n";
212   return 0;
213 }