source: trunk/markup_stats.cxx @ 92

Last change on this file since 92 was 92, checked in by cameron, 11 years ago

Passing buffer pointers/lgths to markup actions.

File size: 11.5 KB
Line 
1/*  markup_stats.c - parabix demo program
2    Copyright (c) 2007, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters, Inc., under the Academic
5    Free License 3.0.
6
7*/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <string>
16#include <iostream>
17using namespace std;
18
19#ifdef CODE_CLOCKING
20#include "codeclocker/clocker/code_clocker_session.h"
21#include "codeclocker/clocker/code_clocker_session.c"
22Code_Clocker * total_clocker;
23#endif
24
25
26
27//#include "src/ilax.h"
28#include "src/engine.h"
29
30#ifndef REPEAT_RUNS
31#define REPEAT_RUNS 1
32#endif
33
34/* Internals */
35#include "src/xmlmodel.h"
36#include "src/bitplex.h"
37#include "src/byteplex.h"
38#include "src/xmldecl.h"
39#include "src/bitlex.h"
40
41
42#include "src/xmlmodel.c"
43#include "src/bitplex.c"
44#include "src/byteplex.c"
45#include "src/xmldecl.c"
46#include "src/bitlex.c"
47#include "src/engine.c"
48
49/* Global declarations of parsing engine. */
50Parser_Interface * parser;
51
52/* Global declarations for statistics. */
53
54int comment_count = 0;
55int comment_length = 0;
56int CDATA_start_count = 0;
57int CDATA_start_pos = 0;
58int CDATA_length = 0;
59int CDATA_end_count = 0;
60int PI_count = 0;
61int PI_length = 0;
62int empty_elem_count = 0;
63int empty_elem_length = 0;
64int start_tag_count = 0;
65int start_tag_length = 0;
66int attribute_count = 0;
67int end_tag_count = 0;
68int end_tag_length = 0;
69int reference_count = 0;
70int reference_length = 0;
71int text_item_count = 0;
72int text_item_length = 0;
73int error_item_count = 0;
74int error_item_length = 0;
75int nesting_depth = 0;
76int max_nesting_depth = 0;
77int total_attribute_count = 0;
78int total_att_name_length = 0;
79int total_att_value_length = 0;
80int namespace_count = 0;
81int total_namespace_name_length = 0;
82int total_namespace_URI_length = 0;
83
84int last_item_start = 0;
85int last_item_stop = 0;
86int last_buffer_rel_pos = 0;
87
88
89/* Action routine for an XML comment in "<!--"  "-->" brackets. */
90template <CodeUnit_Base C>
91inline void ParsingEngine<C>::Comment_action(unsigned char * item, int lgth) {
92        comment_count +=1;
93        comment_length += lgth;
94}
95
96/* Action routine called upon recognizing "<![CDATA[" to start a CDATA section. */
97template <CodeUnit_Base C>
98inline void ParsingEngine<C>::CDATA_start_action(unsigned char * CDATA_ptr){
99        CDATA_start_pos = AbsPos() - 9;
100        CDATA_start_count +=1;
101}
102
103/* Action routine called upon recognizing "]]>" to end a CDATA section. */
104template <CodeUnit_Base C>
105inline void ParsingEngine<C>::CDATA_end_action(unsigned char * CDATA_end_ptr) {
106        CDATA_end_count +=1;
107        CDATA_length += AbsPos() - CDATA_start_pos;
108}
109
110/* Action routine for an XML processing instruction enclosed in "<?" and "?>" brackets. */
111template <CodeUnit_Base C>
112inline void ParsingEngine<C>::PI_action(unsigned char * item, int lgth) {
113        PI_count +=1;
114        PI_length += lgth;
115}
116
117/* Action routine for an empty element enclosed in "<" and "/>" brackets. */
118template <CodeUnit_Base C>
119inline void ParsingEngine<C>::EmptyElement_action(unsigned char * item, int lgth) {
120        empty_elem_count +=1;
121        empty_elem_length += lgth;
122}
123
124/* Action routine for a start tag enclosed in "<" and ">" brackets. */
125template <CodeUnit_Base C>
126inline void ParsingEngine<C>::StartTag_action(unsigned char * item, int lgth) {
127        start_tag_count +=1;
128        start_tag_length += lgth;
129        nesting_depth += 1;
130        if (nesting_depth > max_nesting_depth) max_nesting_depth = nesting_depth;
131}
132
133/* Action routine for an end tag enclosed in "</" and ">" brackets. */
134template <CodeUnit_Base C>
135inline void ParsingEngine<C>::EndTag_action(unsigned char * item, int lgth) {
136        end_tag_count +=1;
137        end_tag_length += lgth;
138        nesting_depth -= 1;
139}
140
141/* Action routine for an error item */
142template <CodeUnit_Base C>
143inline void ParsingEngine<C>::Error_action(unsigned char * item, int lgth) {
144        error_item_count +=1;
145        error_item_length += lgth;
146        printf("Error: illegal markup at positions %i of length %i.\n", AbsPos()-lgth, lgth);
147        cout << string((char *) item, lgth) << endl;
148}
149
150/* Action routine for a text item */
151template <CodeUnit_Base C>
152inline void ParsingEngine<C>::Text_action(unsigned char * item, int lgth) {
153        text_item_count +=1;
154        text_item_length += lgth;
155}
156
157template <CodeUnit_Base C>
158inline void ParsingEngine<C>::Reference_action(unsigned char * item, int lgth) {
159        reference_count +=1;
160        reference_length += lgth;
161}
162
163
164
165
166/* Three action routines for markup components are defined as follows.
167|ElementName_action| is the action routine called upon recognition of
168an element name immediately after the opening angle bracket of a start
169tag or empty element tag.  {\bf OR MAYBE THIS SHOULD BE DEFERRED UNTIL
170AFTER ATTRIBUTE PROCESSING SO THAT NAMESPACES ARE SET?}
171It is called with two parameters identifying the
172first and last character positions of the expected XML_name.
173Similarly, |PI_Target_action| is the action routine called upon recognition
174of the XML Name that occurs immediately after the opening "<?"
175delimiter of a processing instruction.
176
177 The third action routine for markup components is Attribute_Value_action,
178which takes three parameters rather than two.  {\bf OR POSSIBLY JUST
179THE QUOTE MARK ITEMS, RELYING ON THE END OF THE LAST COMPONENT PROCESSED
180TO MARK THE SPACE BEFORE THE ATT NAME.- REQUIRES ELEMENT_NAME_ACTION}
181*/
182
183/* Semantic action routines for markup components. */
184/* Action routine for an element name occurring immediately after the
185   opening "<" of a start tag or empty element tag. */
186template <CodeUnit_Base C>
187inline void ParsingEngine<C>::ElementName_action(unsigned char * item, int lgth) {
188}
189
190/* Action routine for a processing instruction target name occurring immediately
191   after the opening "<?" of a processing instruction. */
192template <CodeUnit_Base C>
193inline void ParsingEngine<C>::PI_Target_action(unsigned char * item, int lgth) {
194}
195
196/* Action routine for an individual attribute/value pair occurring in
197   a element start tag or an empty element tag. */
198template <CodeUnit_Base C>
199inline void ParsingEngine<C>::AttributeValue_action(unsigned char * name, int name_lgth, 
200                                 unsigned char * val, int val_lgth) {
201        total_attribute_count+=1;
202        total_att_name_length += name_lgth;
203        total_att_value_length += val_lgth;
204}
205
206/* Action routine for an individual attribute/value pair occurring in
207   a element start tag or an empty element tag. */
208template <CodeUnit_Base C>
209inline void ParsingEngine<C>::Namespace_action(unsigned char * name, int name_lgth,
210                             unsigned char * URI, int URI_lgth) {
211        namespace_count+=1;
212        total_namespace_name_length += name_lgth;
213        total_namespace_URI_length += URI_lgth;
214}
215
216
217template <CodeUnit_Base C>
218inline void ParsingEngine<C>::FinalizeBuffer_action(int& preserve_pos) {
219#ifdef DEBUG
220        printf ("FinalizeBuffer; last 16 bytes + lookahead 16 =\n");
221        cout << string((char *) GetCodeUnitPtr(AbsPos()-16), 16) << "::" << string((char *) GetCodeUnitPtr(AbsPos()), 16) << endl;
222#endif
223
224        preserve_pos = AbsPos();
225#ifdef CODE_CLOCKING
226end_Interval (total_clocker, buffer_rel_pos);
227start_Interval (total_clocker);
228#endif
229}
230
231
232template <CodeUnit_Base C>
233inline void ParsingEngine<C>::DocumentStart_action() {
234#ifdef CODE_CLOCKING
235start_Interval (total_clocker);
236#endif
237}
238
239template <CodeUnit_Base C>
240inline void ParsingEngine<C>::DocumentEnd_action() {
241#ifdef CODE_CLOCKING
242end_Interval (total_clocker, buffer_rel_pos);
243#endif
244}
245
246template <CodeUnit_Base C>
247inline void ParsingEngine<C>::Doctype_action(unsigned char * item, int lgth) {
248#ifdef SHOW_DTD_ACTIONS
249        printf("Document Type:\n");
250        cout << string((char *) item, lgth) <<endl;
251#endif
252}
253
254template <CodeUnit_Base C>
255inline void ParsingEngine<C>::PEReference_action(unsigned char * item, int lgth) {
256}
257
258template <CodeUnit_Base C>
259inline void ParsingEngine<C>::Elementdecl_action(unsigned char * item, int lgth) {
260#ifdef SHOW_DTD_ACTIONS
261        printf("Elementdecl:\n");
262        cout << string((char *) item, lgth) <<endl;
263#endif
264}
265
266template <CodeUnit_Base C>
267inline void ParsingEngine<C>::AttlistDecl_action(unsigned char * item, int lgth) {
268#ifdef SHOW_DTD_ACTIONS
269        printf("AttlistDecl:\n");
270        cout << string((char *) item, lgth) <<endl;
271#endif
272}
273
274template <CodeUnit_Base C>
275inline void ParsingEngine<C>::Entitydecl_action(unsigned char * entity_name, int entity_name_lgth, unsigned char * item, int lgth) {
276#ifdef SHOW_DTD_ACTIONS
277        printf("Entitydecl:\n");
278        cout << string((char *) item, lgth) <<endl;
279#endif
280}
281
282template <CodeUnit_Base C>
283inline void ParsingEngine<C>::Notationdecl_action(unsigned char * item, int lgth) {
284#ifdef SHOW_DTD_ACTIONS
285        printf("Notationdecl:\n");
286        cout << string((char *) item, lgth) <<endl;
287#endif
288}
289
290template <CodeUnit_Base C>
291inline void ParsingEngine<C>::Prolog_action(unsigned char * item, int lgth) {
292#ifdef SHOW_DTD_ACTIONS
293        printf("Prolog:\n");
294        cout << string((char *) item, lgth) <<endl;
295#endif
296}
297
298
299#define print_stats(stat_string, count, total_lgth) \
300        printf("%i %s", count, stat_string);\
301        if (count == 0) printf("s.\n");\
302        else if (count == 1) printf(" of length %i.\n", total_lgth);\
303        else printf("s of avg. lgth %i.\n", total_lgth/count);
304
305int
306main(int argc, char * argv[]) {
307        if (argc != 2) {
308        printf("Usage: %s <filename>\n", argv[0]);
309                exit(-1);
310        }
311        char * filename = argv[1];
312#ifdef CODE_CLOCKING
313
314init_Code_Clocker_Session(8, // num code clockers
315                         64096, // max individual timestamp measurements
316                         4ul, // num session parameters
317                         65536, // num timestamp calibration measurements,
318                            65534,// num constant time loop executions
319                         8 // num constant time loop iterations
320                         );
321set_Session_Cmd_Line_Args(argv[0]);
322total_clocker = register_Code_Clocker("Markup_stats_total", "Total markup_stats time\n");
323#endif
324
325        for (int run = 0; run < REPEAT_RUNS; run++) {
326       
327        parser = Parser_Interface::ParserFactory(filename);
328       
329       
330        if (!parser->has_ByteOrderMark()) printf("No ");
331        printf("Byte Order Mark found.\n");
332
333        if (parser->get_version() == XML_1_0) printf("XML version 1.0 declared.\n");
334        else if (parser->get_version() == XML_1_1) printf("XML version 1.1 declared.\n");
335        else printf ("XML version 1.0 implied by default.\n");
336        if (parser->has_EncodingDecl()) {
337                printf("XML encoding declared:  %s\n", parser->get_Encoding());
338        }
339        if (parser->standalone_status() == Standalone_yes) 
340                printf("XML standalone = yes declared.\n");
341        else if (parser->standalone_status() == Standalone_no) 
342                printf("XML standalone = no declared.\n");
343        else printf ("XML standalone = no by default.\n");
344       
345        parser->Parse_Prolog();
346        parser->ParseContent();
347        parser->~Parser_Interface();
348        printf("Run %i complete.\n", run);
349        }
350       
351        print_stats("comment", comment_count, comment_length);
352        print_stats("CDATA section", CDATA_end_count, CDATA_length);
353        print_stats("processing instruction", PI_count, PI_length);
354        print_stats("empty element", empty_elem_count, empty_elem_length);
355        print_stats("start tag", start_tag_count, start_tag_length);
356        printf("%i total attributes\n", attribute_count);
357        print_stats("attribute name", total_attribute_count, total_att_name_length);
358        print_stats("attribute value", total_attribute_count, total_att_value_length);
359        print_stats("namespace name", namespace_count, total_namespace_name_length);
360        print_stats("namespace URI", namespace_count, total_namespace_URI_length);
361        print_stats("end tag", end_tag_count, end_tag_length);
362        print_stats("text item", text_item_count, text_item_length);
363        print_stats("reference", reference_count, reference_length);
364        print_stats("error item", error_item_count, error_item_length);
365        printf("Maximum nesting depth = %i\n", max_nesting_depth);
366#ifdef CODE_CLOCKING
367        write_XML_File();
368#endif
369       
370        return(0);
371}
Note: See TracBrowser for help on using the repository browser.