Fixed MakeStatic()
[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 "TextCollection.h"
11 #include "HeapProfiler.h"
12 using SXSI::TextCollection;
13
14 void printDocumentResult(TextCollection::document_result dr)
15 {
16     TextCollection::document_result::iterator it;
17     printf("document result:");
18     for (it = dr.begin(); it != dr.end(); ++it)
19         printf(" %i", *it);
20     printf("\n");
21     fflush(stdout);
22 }
23
24 void printFullResult(TextCollection::full_result fr)
25 {
26     TextCollection::full_result::iterator it;
27     printf("full result:");
28     for (it = fr.begin(); it != fr.end(); ++it)
29         printf(" (%i, %lu)", (*it).first, (*it).second);
30     printf("\n");
31     fflush(stdout);
32 }
33
34 int main()
35 {
36   std::string kbd;
37   string str;
38   uchar* data;
39   int i = 0 ,j = 0;
40   int heap_base = HeapProfiler::GetHeapConsumption();
41   std::cerr << "Initial heap usage : " << heap_base << "\n";
42   TextCollection *csa = TextCollection::InitTextCollection(5);  // Avoid small samplerates ;)
43   heap_base = HeapProfiler::GetHeapConsumption ();
44   std::cerr << "Heap usage after InitTextCollection : " << heap_base << "\n";
45
46
47   while (not(cin.eof())){
48       getline(cin,str); // Read line by line.
49 //    cin >> str; // Read word by word.
50     data = (uchar *) str.c_str();
51     csa->InsertText(data);    
52     i++;
53     j+= str.size();
54     str.clear();
55     if ( i % 1000 == 0)  {
56       std::cerr << "Inserted : " << i << " strings\n";
57       std::cerr << "Number of bytes inserted : " << j << "b \n";
58       std::cerr << "Heap usage used for strings: " << HeapProfiler::GetHeapConsumption() - heap_base
59                 << "bytes\n";
60       std::cerr << "Ratio is : " << (float) (HeapProfiler::GetHeapConsumption() - heap_base) / ((float) j) <<"\n";
61       
62     };
63     
64     };
65
66 /* the whole file as one string:
67   uchar *temp = Tools::GetFileContents("data.txt", 0);
68   csa->InsertText(temp);
69   delete [] temp;*/
70
71   std::cerr << "Creating new text collection with " << i << " strings (total " << j/1024 << " kb)\n"; 
72   std::cerr << "Before MakeStatic() [press enter]\n";
73   std::cin >> kbd;
74   // This will print the maximum mem usage during construction time:
75   std::cerr << "max heap usage: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
76   csa->MakeStatic();
77   std::cerr << "After MakeStatic() [press enter]\n";
78   // This will print the maximum mem usage during MakeStatic():
79   std::cerr << "max heap usage: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
80   std::cin >> kbd;
81   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
82   delete csa;
83   std::cerr << "After Delete [press enter]\n";
84   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
85   std::cin >> kbd;
86   return 0;
87 }