Test case for memory consumption with the TextCollection.
[SXSI/TextCollection.git] / testTextCollection.cpp
1 // Test driver for text collection
2 #include <iostream>
3 #include <cstdlib>
4 #include <string>
5 #include "HeapProfiler.h"
6 using std::cout;
7 using std::endl;
8 using std::cin;
9 using std::string;
10
11 #include "TextCollection.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);
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);
49     data = (uchar *) str.c_str();
50     csa->InsertText(data);    
51     i++;
52     j+= str.size();
53     str.clear();
54     if ( i % 1000 == 0)  {
55       std::cerr << "Inserted : " << i << " strings\n";
56       std::cerr << "Number of bytes inserted : " << j << "b \n";
57       std::cerr << "Heap usage used for strings: " << HeapProfiler::GetHeapConsumption() - heap_base
58                 << "bytes\n";
59       std::cerr << "Ratio is : " << (float) (HeapProfiler::GetHeapConsumption() - heap_base) / ((float) j) <<"\n";
60       
61     };
62     
63   };
64
65   std::cerr << "Creating new text collection with " << i << " strings (total " << j/1024 << " kb)\n"; 
66   std::cerr << "Before MakeStatic() [press enter]\n";
67   std::cin >> kbd;
68   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
69   csa->MakeStatic();
70   std::cerr << "After MakeStatic() [press enter]\n";
71   std::cin >> kbd;
72   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
73   delete csa;
74   std::cerr << "After Delete [press enter]\n";
75   std::cerr << "heap usage: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " Mbytes" << std::endl;
76   std::cin >> kbd;
77   return 0;
78 }