Debug swcsa
[SXSI/TextCollection.git] / testTextCollection.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
10 #include "TextCollectionBuilder.h"
11 #include "HeapProfiler.h"
12 using SXSI::TextCollectionBuilder;
13 using SXSI::TextCollection;
14
15 void printDocumentResult(TextCollection::document_result dr)
16 {
17     TextCollection::document_result::iterator it;
18     printf("document result:");
19     for (it = dr.begin(); it != dr.end(); ++it)
20         printf(" %i", *it);
21     printf("\n");
22     fflush(stdout);
23 }
24
25 void printFullResult(TextCollection::full_result fr)
26 {
27     TextCollection::full_result::iterator it;
28     printf("full result:");
29     for (it = fr.begin(); it != fr.end(); ++it)
30         printf(" (%i, %lu)", (*it).first, (*it).second);
31     printf("\n");
32     fflush(stdout);
33 }
34
35 int main()
36 {
37   std::string kbd;
38   string str;
39   uchar* data;
40   int i = 0 ,j = 0;
41   int heap_base = HeapProfiler::GetHeapConsumption();
42   std::cerr << "Initial heap usage : " << heap_base << "\n";
43   TextCollectionBuilder *tcb = TextCollectionBuilder::create(5);
44   heap_base = HeapProfiler::GetHeapConsumption ();
45   std::cerr << "Heap usage after InitTextCollection : " << heap_base << "\n";
46   Tools::StartTimer();
47
48   while (not(cin.eof())){
49    getline(cin,str); // Read line by line.
50 //   cin >> str; // Read word by word.
51     data = (uchar *) str.c_str();
52     if (str.size() == 0)
53         continue;
54        
55     tcb->InsertText(data);    
56     i++;
57     j+= str.size();
58     str.clear();
59     if ( i % 100000 == 0)  {
60       std::cerr << "Inserted : " << i << " strings\n";
61       std::cerr << "Number of bytes inserted : " << j << "b \n";
62       std::cerr << "Heap usage used for strings: " << HeapProfiler::GetHeapConsumption() - heap_base
63                 << "bytes\n";
64       std::cerr << "Ratio is : " << (float) (HeapProfiler::GetHeapConsumption() - heap_base) / ((float) j) <<"\n";
65       
66     };
67     
68     };
69 /**/
70   //the whole file as 20 strings:
71   /* uchar *temp = Tools::GetFileContents("data/english.100MB", 0);
72   ulong n = strlen((char *)temp);
73   std::cout << "n = " << n << std::endl;
74   ulong offset = n/40;
75   uchar *it = temp;
76   for (i = 0; i < 5; ++i)
77   {
78       it[offset] = '\0';
79       tcb->InsertText(it);
80       std::cout << "inserted " << strlen((char *)it) << " bytes." << std::endl;
81       it += offset +1;
82   }
83   it -= offset+1;
84
85   if (it > temp + n)
86       std::cout << "over bounds" << std::endl;
87   delete [] temp;*/
88   
89
90   std::cerr << "Creating new text collection with " << i << " strings (total " << j/1024 << " kb)\n"; 
91   std::cerr << "max heap usage: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
92   HeapProfiler::ResetMaxHeapConsumption();
93   std::cerr << "Before MakeStatic() [press enter]\n";
94   //std::cin >> kbd;
95   // This will print the maximum mem usage during construction time:
96   TextCollection* tc = tcb->InitTextCollection();//SXSI::TextStorage::TYPE_LZ_INDEX);
97   delete tcb; tcb = 0;
98   std::cerr << "After MakeStatic() [press enter]\n";
99   // This will print the maximum mem usage during MakeStatic():
100   std::cerr << "max heap usage: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
101   //std::cin >> kbd;
102   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
103   delete tc;
104   std::cerr << "After Delete [press enter]\n";
105   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption() << " bytes" << std::endl;
106   //std::cin >> kbd;
107   return 0;
108 }