.
[SXSI/xpathcomp.git] / timeXMLTree.cpp
1 #include "XMLDocShredder.h"
2 #include "XMLTree.h"
3 #include "Utils.h"
4 #include <sys/time.h>
5 #include <time.h>
6
7 using std::cout;
8 using std::string;
9 using std::left;
10 using std::right;
11
12 static clock_t tFirstChild = 0;
13 static clock_t tNextSibling = 0;
14 static clock_t tParent = 0;
15 static clock_t tTaggedAncestor = 0;
16 static clock_t tTaggedChild = 0;
17 static clock_t tTaggedDesc = 0;
18 static clock_t tTaggedFoll = 0;
19 static clock_t tParentNode = 0;
20 static clock_t tPrevNode = 0;
21 static clock_t tTag = 0;
22 static clock_t tMyText = 0;
23 static clock_t tPrevText = 0;
24 static clock_t tNextText = 0;
25 static clock_t tDocIds = 0;
26
27 static clock_t tFullTraversal = 0;
28 static clock_t tJumpTraversal = 0;
29
30 static unsigned int cFirstChild = 0;
31 static unsigned int cNextSibling = 0;
32 static unsigned int cParent = 0;
33 static unsigned int cTaggedAncestor = 0;
34 static unsigned int cTaggedChild = 0;
35 static unsigned int cTaggedDesc = 0;
36 static unsigned int cTaggedFoll = 0;
37 static unsigned int cParentNode = 0;
38 static unsigned int cPrevNode = 0;
39 static unsigned int cTag = 0;
40 static unsigned int cMyText = 0;
41 static unsigned int cPrevText = 0;
42 static unsigned int cNextText = 0;
43 static unsigned int cDocIds = 0;
44
45 static unsigned int cFullTraversal = 0;
46 static unsigned int cJumpTraversal = 0;
47
48
49
50 static clock_t tmp;
51
52 static TagType target_tag = -1;
53
54 #define STARTTIMER()   (tmp= clock())
55 #define STOPTIMER(x)   do {  (t##x) = (t##x) + (clock() - tmp); (c##x)= (c##x)+1;  } while (0)
56 #define PRINTSTATS(x)  do {                                             \
57     std::cout.width(15);                                                \
58     std::cout << std::left << #x;                                       \
59     std::cout << " : ";                                                 \
60     std::cout.width(8);                                                 \
61     std::cout << std::right << c##x << " calls,";                       \
62     std::cout.width(8);                                                 \
63     std::cout << std::right << t##x << " cycles, total:";               \
64     std::cout.width(5);                                                 \
65     std::cout << std::right << ((t##x) *1000.00)  /CLOCKS_PER_SEC       \
66               << " ms, mean: ";                                         \
67     std::cout.width(5);                                                 \
68     std::cout << std::right                                             \
69               << (((t##x)* 1000.00)  /CLOCKS_PER_SEC) / c##x            \
70               << "\n";                                                  \
71   } while (0)
72
73
74 void traversal(XMLTree * tree, treeNode node,unsigned char* targettagname){
75   treeNode res1,res2;
76   TagType tag;
77   DocID id1,id2,id3;
78   range rg;
79   const unsigned char * tagname;
80   if (node != NULLT){
81
82     STARTTIMER();
83     tag = tree->Tag(node);
84     STOPTIMER(Tag);
85     if (target_tag == -1){
86       tagname = tree->GetTagNameByRef(tag);
87       if (strcmp( (char*) tagname, (char*) targettagname) == 0)
88         target_tag = tag;
89     };
90     STARTTIMER();
91     res1 = tree->Parent(node);
92     STOPTIMER(Parent);
93     /*
94     STARTTIMER();
95     res1 = tree->TaggedChild(node,0,tag);
96     STOPTIMER(TaggedChild);
97
98     STARTTIMER();
99     res1 = tree->TaggedAncestor(node,tag);
100     STOPTIMER(TaggedAncestor);
101     */
102     STARTTIMER();
103     res1 = tree->TaggedDesc(node,tag);
104     STOPTIMER(TaggedDesc);
105
106     STARTTIMER();
107     res1 = tree->TaggedFoll(node,tag);
108     STOPTIMER(TaggedFoll);
109     
110     STARTTIMER();
111     rg = tree->DocIds(node);
112     STOPTIMER(DocIds);
113
114     STARTTIMER();
115     id1 = tree->MyText(node);
116     STOPTIMER(MyText);
117
118     STARTTIMER();
119     id2 = tree->PrevText(node);
120     STOPTIMER(PrevText);
121
122     STARTTIMER();
123     id3 = tree->NextText(node);
124     STOPTIMER(NextText);
125     
126     id1 = max(id1, max(id2,id3));
127
128     STARTTIMER();
129     res1 = tree->ParentNode(id1);
130     STOPTIMER(ParentNode);
131
132     STARTTIMER();
133     res1 = tree->PrevNode(id1);
134     STOPTIMER(PrevNode);
135     
136     STARTTIMER();
137     res1 = tree->FirstChild(node);
138     STOPTIMER(FirstChild);
139
140     STARTTIMER();
141     res2 = tree->NextSibling(node);
142     STOPTIMER(NextSibling);
143
144     traversal(tree,res1,targettagname);
145     traversal(tree,res2,targettagname);
146     
147   };
148   
149 }
150
151 /* This simulates the run function of the automata */
152
153 unsigned int time_traversal(XMLTree *tree,treeNode node){
154   TagType tag;
155   if (node != NULLT) {
156     cFullTraversal++;
157     tag = tree->Tag(node);
158     if (tag == target_tag)      
159       return 1 + 
160         time_traversal(tree,tree->FirstChild(node)) +
161         time_traversal(tree,tree->NextSibling(node)); 
162     else
163       return time_traversal(tree,tree->FirstChild(node)) +
164         time_traversal(tree,tree->NextSibling(node));
165
166   }
167   else 
168     return 0;
169 }
170
171 /* This simulates the run function of the jumping automata*/
172 unsigned int time_jump(XMLTree* tree, treeNode node,treeNode root){
173   TagType tag;
174   if (node != NULLT) {
175     cJumpTraversal++;
176     tag = tree->Tag(node);
177     if (tag == target_tag)
178
179       return 1 + 
180         time_jump(tree, tree->TaggedDesc(node,target_tag),node) +
181         time_jump(tree, tree->TaggedFollBelow(node,target_tag,root),  root);
182
183     else
184       return time_jump(tree, tree->TaggedDesc(node,target_tag),node) +
185         time_jump(tree, tree->TaggedFollBelow(node,target_tag,root),  root);
186   }
187   else 
188     return 0;
189 }
190
191
192
193
194
195 int main(int argc, char ** argv){
196   unsigned int count1,count2;
197   unsigned char * tagname = (unsigned char *) "keyword";
198
199   if (argc != 2){
200     std::cout << "Usage : " << argv[0] << " filename (without .srx)\n";
201     return 1;
202   };
203
204   // The samplerate is not taken into account for loading anymore
205   XMLTree * tree = XMLTree::Load((unsigned char*) argv[1],64);
206   
207   traversal(tree,tree->Root(),tagname);
208
209   STARTTIMER();
210   count1 = time_traversal(tree,tree->Root());
211   STOPTIMER(FullTraversal);
212
213   count2 = time_jump(tree,tree->Root(),tree->Root());
214   STOPTIMER(JumpTraversal);
215
216   PRINTSTATS(Tag);
217   PRINTSTATS(FirstChild);
218   PRINTSTATS(NextSibling);
219   PRINTSTATS(Parent);
220   PRINTSTATS(TaggedAncestor);
221   PRINTSTATS(TaggedChild);
222   PRINTSTATS(DocIds);
223   PRINTSTATS(TaggedDesc);
224   PRINTSTATS(TaggedFoll);
225   PRINTSTATS(PrevText);
226   PRINTSTATS(MyText);
227   PRINTSTATS(NextText);
228   PRINTSTATS(ParentNode);
229   PRINTSTATS(PrevNode);
230   std::cout << "\n";
231   std::cout << "Full traversal found " << count1 << " " << tagname << "  nodes\n";
232   PRINTSTATS(FullTraversal);
233   std::cout << "\n";
234   std::cout << "Jump traversal found " << count2 << " " << tagname << "  nodes\n";
235   PRINTSTATS(JumpTraversal);
236   
237   
238   return 0;
239 }