source: trunk/markup_stats.cxx @ 154

Last change on this file since 154 was 154, checked in by lindanl, 11 years ago

Performance test definitions.

File size: 14.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#define ON 1
20#define OFF 2
21
22#define VALIDATION_MODE OFF
23
24#ifdef PAPI
25#include "../code_clocker/clocker/cc.h"
26#include "../code_clocker/clocker/cc.cxx"
27
28CC * code_clocker;
29
30#define NONE 0
31#define END_TAG_MATCHING 1
32#define ATTRIBUTE_UNIQUENESS 2
33#define NAME_VALIDATION 3
34#define NAME_LOOKUP 4
35
36#define OMISSION END_TAG_MATCHING
37
38
39#define CHARSET_VALIDATION 1
40#define WS_CONTROL 2
41#define MARKUP_STREAMS 3
42#define BITLEX_ALL 4
43#define BITPLEX 5
44#define BYTEPLEX 6
45#define ADVANCE_BUFFERS 7
46#define BUFFER_TOTAL 8
47#define FILE_READING 9
48
49#define CODE_CLOCKING BUFFER_TOTAL
50
51#endif
52
53//#include "src/ilax.h"
54#include "src/engine.h"
55
56#ifndef REPEAT_RUNS
57#define REPEAT_RUNS 1
58#endif
59
60/* Internals */
61#include "src/xmlmodel.h"
62#include "src/xml_error.h"
63#include "src/bitplex.h"
64#include "src/byteplex.h"
65#include "src/xmldecl.h"
66#include "src/bitlex.h"
67
68
69#include "src/xmlmodel.c"
70#include "src/xml_error.c"
71#include "src/bitplex.c"
72#include "src/byteplex.c"
73#include "src/xmldecl.c"
74#include "src/bitlex.c"
75#include "src/engine.c"
76#include "src/symtab.c"
77
78/* Global declarations of parsing engine. */
79Parser_Interface * parser;
80
81/* Global declarations for statistics. */
82
83int comment_count = 0;
84int comment_length = 0;
85int CDATA_start_count = 0;
86int CDATA_start_pos = 0;
87int CDATA_length = 0;
88int CDATA_end_count = 0;
89int PI_count = 0;
90int PI_length = 0;
91int empty_elem_count = 0;
92int empty_elem_length = 0;
93int start_tag_count = 0;
94int start_tag_length = 0;
95int attribute_count = 0;
96int end_tag_count = 0;
97int end_tag_length = 0;
98int reference_count = 0;
99int reference_length = 0;
100int text_item_count = 0;
101int text_item_length = 0;
102int error_item_count = 0;
103int error_item_length = 0;
104int nesting_depth = 0;
105int max_nesting_depth = 0;
106int total_attribute_count = 0;
107int total_att_name_length = 0;
108int total_att_value_length = 0;
109int namespace_count = 0;
110int total_namespace_name_length = 0;
111int total_namespace_URI_length = 0;
112
113int last_item_start = 0;
114int last_item_stop = 0;
115int last_buffer_rel_pos = 0;
116
117
118/* Action routine for an XML comment in "<!--"  "-->" brackets. */
119template <CodeUnit_Base C>
120inline void ParsingEngine<C>::Comment_action(unsigned char * item, int lgth) {
121        comment_count +=1;
122       
123        #if defined(CALC_AVG)
124                comment_length += lgth;
125        #endif
126}
127
128/* Action routine called upon recognizing "<![CDATA[" to start a CDATA section. */
129template <CodeUnit_Base C>
130inline void ParsingEngine<C>::CDATA_start_action(unsigned char * CDATA_ptr){
131        CDATA_start_pos = AbsPos() - 9;
132        CDATA_start_count +=1;
133}
134
135/* Action routine called upon recognizing "]]>" to end a CDATA section. */
136template <CodeUnit_Base C>
137inline void ParsingEngine<C>::CDATA_end_action(unsigned char * CDATA_end_ptr) {
138        CDATA_end_count +=1;
139       
140        #if defined(CALC_AVG)
141                CDATA_length += AbsPos() - CDATA_start_pos;
142        #endif
143}
144
145/* Action routine for an XML processing instruction enclosed in "<?" and "?>" brackets. */
146template <CodeUnit_Base C>
147inline void ParsingEngine<C>::PI_action(unsigned char * item, int lgth) {
148        PI_count +=1;
149       
150        #if defined(CALC_AVG)
151                PI_length += lgth;
152        #endif
153}
154
155/* Action routine for an empty element enclosed in "<" and "/>" brackets. */
156template <CodeUnit_Base C>
157inline void ParsingEngine<C>::EmptyElement_action(unsigned char * item, int lgth) {
158        empty_elem_count +=1;
159       
160        #if defined(CALC_AVG)
161                empty_elem_length += lgth;
162        #endif
163}
164
165/* Action routine for a start tag enclosed in "<" and ">" brackets. */
166template <CodeUnit_Base C>
167inline void ParsingEngine<C>::StartTag_action(unsigned char * item, int lgth) {
168        start_tag_count +=1;
169       
170        #if defined(CALC_AVG)
171                start_tag_length += lgth;
172        #endif
173       
174        nesting_depth += 1;
175        if (nesting_depth > max_nesting_depth) max_nesting_depth = nesting_depth;
176//      cout << string((char *) item, lgth) << endl;
177}
178
179/* Action routine for an end tag enclosed in "</" and ">" brackets. */
180template <CodeUnit_Base C>
181inline void ParsingEngine<C>::EndTag_action(unsigned char * item, int lgth) {
182        end_tag_count +=1;
183       
184        #if defined(CALC_AVG)
185                end_tag_length += lgth;
186        #endif
187       
188        nesting_depth -= 1;
189}
190
191/* Action routine for an error item */
192template <CodeUnit_Base C>
193inline void ParsingEngine<C>::Error_action(unsigned char * item, int lgth) {
194        error_item_count +=1;
195       
196        #if defined(CALC_AVG)
197                error_item_length += lgth;
198        #endif
199       
200        fprintf(stderr, "Error: illegal markup at positions %i of length %i.\n", AbsPos()-lgth, lgth);
201        cerr << string((char *) item, lgth) << endl;
202}
203
204/* Action routine for a text item */
205template <CodeUnit_Base C>
206inline void ParsingEngine<C>::Text_action(unsigned char * item, int lgth) {
207        text_item_count +=1;
208       
209        #if defined(CALC_AVG)
210                text_item_length += lgth;
211        #endif
212}
213
214template <CodeUnit_Base C>
215inline void ParsingEngine<C>::Reference_action(unsigned char * item, int lgth) {
216        reference_count +=1;
217       
218        #if defined(CALC_AVG)
219                reference_length += lgth;
220        #endif
221}
222
223
224
225
226/* Three action routines for markup components are defined as follows.
227|ElementName_action| is the action routine called upon recognition of
228an element name immediately after the opening angle bracket of a start
229tag or empty element tag.  {\bf OR MAYBE THIS SHOULD BE DEFERRED UNTIL
230AFTER ATTRIBUTE PROCESSING SO THAT NAMESPACES ARE SET?}
231It is called with two parameters identifying the
232first and last character positions of the expected XML_name.
233Similarly, |PI_Target_action| is the action routine called upon recognition
234of the XML Name that occurs immediately after the opening "<?"
235delimiter of a processing instruction.
236
237 The third action routine for markup components is Attribute_Value_action,
238which takes three parameters rather than two.  {\bf OR POSSIBLY JUST
239THE QUOTE MARK ITEMS, RELYING ON THE END OF THE LAST COMPONENT PROCESSED
240TO MARK THE SPACE BEFORE THE ATT NAME.- REQUIRES ELEMENT_NAME_ACTION}
241*/
242
243/* Semantic action routines for markup components. */
244/* Action routine for an element name occurring immediately after the
245   opening "<" of a start tag or empty element tag. */
246template <CodeUnit_Base C>
247inline void ParsingEngine<C>::ElementName_action(unsigned char * item, int lgth) {
248}
249
250/* Action routine for a processing instruction target name occurring immediately
251   after the opening "<?" of a processing instruction. */
252template <CodeUnit_Base C>
253inline void ParsingEngine<C>::PI_Target_action(unsigned char * item, int lgth) {
254}
255
256/* Action routine for an individual attribute/value pair occurring in
257   a element start tag or an empty element tag. */
258template <CodeUnit_Base C>
259inline void ParsingEngine<C>::AttributeValue_action(unsigned char * name, int name_lgth, 
260                                 unsigned char * val, int val_lgth) {
261        total_attribute_count+=1;
262       
263        #if defined(CALC_AVG)
264                total_att_name_length += name_lgth;
265                total_att_value_length += val_lgth;
266        #endif
267}
268
269/* Action routine for an individual attribute/value pair occurring in
270   a element start tag or an empty element tag. */
271template <CodeUnit_Base C>
272inline void ParsingEngine<C>::Namespace_action(unsigned char * name, int name_lgth,
273                             unsigned char * URI, int URI_lgth) {
274        namespace_count+=1;
275       
276        #if defined(CALC_AVG)
277                total_namespace_name_length += name_lgth;
278                total_namespace_URI_length += URI_lgth;
279        #endif
280}
281
282
283template <CodeUnit_Base C>
284inline void ParsingEngine<C>::FinalizeBuffer_action() {
285       
286       
287#ifdef DEBUG
288        printf ("FinalizeBuffer; last 16 bytes + lookahead 16 =\n");
289        cout << string((char *) GetCodeUnitPtr(AbsPos()-16), 16) << "::" << string((char *) GetCodeUnitPtr(AbsPos()), 16) << endl;
290#endif
291        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
292                code_clocker->cc_end_interval(BUFFER_SIZE);
293        #endif
294        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
295                code_clocker->cc_start_interval();
296        #endif
297       
298}
299
300
301template <CodeUnit_Base C>
302inline void ParsingEngine<C>::DocumentStart_action() {
303        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
304                code_clocker->cc_start_interval();
305        #endif
306}
307
308template <CodeUnit_Base C>
309inline void ParsingEngine<C>::DocumentEnd_action() {
310        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
311                code_clocker->cc_end_interval(buffer_rel_pos);
312        #endif
313}
314
315template <CodeUnit_Base C>
316inline void ParsingEngine<C>::Doctype_action(unsigned char * item, int lgth) {
317#ifdef SHOW_DTD_ACTIONS
318        printf("Document Type:\n");
319        cout << string((char *) item, lgth) <<endl;
320#endif
321}
322
323template <CodeUnit_Base C>
324inline void ParsingEngine<C>::PEReference_action(unsigned char * item, int lgth) {
325}
326
327
328template <CodeUnit_Base C>
329inline void ParsingEngine<C>::ExtSubsetDecl_action(unsigned char * item, int lgth) {
330#ifdef SHOW_DTD_ACTIONS
331        printf("ExtSubsetDecl:\n");
332        cout << string((char *) item, lgth) <<endl;
333#endif
334}
335
336template <CodeUnit_Base C>
337inline void ParsingEngine<C>::Prolog_action(unsigned char * item, int lgth) {
338#ifdef SHOW_DTD_ACTIONS
339        printf("Prolog:\n");
340        cout << string((char *) item, lgth) <<endl;
341#endif
342}
343
344#define print_stats(stat_string, count, total_lgth) \
345        printf("%i %s", count, stat_string);\
346        if (count == 0) printf("s.\n");\
347        else if (count == 1) printf(" of length %i.\n", total_lgth);\
348        else printf("s of avg. lgth %i.\n", total_lgth/count);
349
350
351
352
353int
354main(int argc, char * argv[]) {
355        if (argc != 2) {
356        printf("Usage: %s <filename>\n", argv[0]);
357                exit(-1);
358        }
359       
360        char * src_filename = argv[1];
361        char * cmdline = new char[strlen(argv[0]) + strlen(argv[1]) +1 +1]; 
362        strcat(cmdline, argv[0]);
363        strcat(cmdline," ");
364        strcat(cmdline,argv[1]);
365
366        #ifdef PAPI
367                #define NUM_EVENTS 2
368                int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
369                int cal_size = 1000;
370                code_clocker = new CC(Events,NUM_EVENTS,cal_size);
371                code_clocker->cc_set_cmd(cmdline);
372        #endif
373
374        // Read the entire file into a memory buffer   
375        FILE * src_file;
376        struct stat fileinfo;
377        int src_filesize;
378
379        // open file and fstat 
380        src_file = fopen ( src_filename , "rb" );
381        if(fstat(fileno(src_file), &fileinfo)!=0) {
382                fprintf(stderr, "Cannot fstat '%s'. Terminating the process ...\n", src_filename); 
383                exit(-1);
384        }
385       
386        src_filesize = fileinfo.st_size;
387       
388        // close file
389        fclose (src_file);
390       
391        for (int run = 0; run < REPEAT_RUNS; run++) {
392
393                #ifdef PAPI
394                        code_clocker->cc_start_interval();
395                #endif
396
397                parser = Parser_Interface::ParserFactory(src_filename);
398       
399                /*             
400                if (!parser->has_ByteOrderMark()) printf("No ");
401                printf("Byte Order Mark found.\n");
402       
403                if (parser->get_version() == XML_1_0) printf("XML version 1.0 declared.\n");
404                else if (parser->get_version() == XML_1_1) printf("XML version 1.1 declared.\n");
405                else printf ("XML version 1.0 implied by default.\n");
406                if (parser->has_EncodingDecl()) {
407                        printf("XML encoding declared:  %s\n", parser->get_Encoding());
408                }
409                if (parser->standalone_status() == Standalone_yes)
410                        printf("XML standalone = yes declared.\n");
411                else if (parser->standalone_status() == Standalone_no)
412                        printf("XML standalone = no declared.\n");
413                else printf ("XML standalone = no by default.\n");
414                */
415
416//              #ifdef PAPI
417//                      code_clocker->cc_start_interval();
418//              #endif
419               
420                parser->Parse_Prolog();
421                parser->Parse_DocumentContent();
422
423//              #ifdef PAPI
424//                      int elems = src_filesize;
425//                      code_clocker->cc_end_interval(elems);
426//              #endif
427
428                parser->~Parser_Interface();
429                printf("Run %i complete.\n", run);
430
431        }
432
433        #ifdef PAPI
434       
435        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == CHARSET_VALIDATION)
436                code_clocker->cc_set_param("CODE_CLOCKING", "CHARSET_VALIDATION");
437        #endif 
438        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == WS_CONTROL)
439                code_clocker->cc_set_param("CODE_CLOCKING", "WS_CONTROL");
440        #endif
441        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == MARKUP_STREAMS)
442                code_clocker->cc_set_param("CODE_CLOCKING", "MARKUP_STREAMS");
443        #endif
444        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BITLEX_ALL)
445                code_clocker->cc_set_param("CODE_CLOCKING", "BITLEX_ALL");
446        #endif
447        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BITPLEX)
448                code_clocker->cc_set_param("CODE_CLOCKING", "BITPLEX");
449        #endif
450        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BYTEPLEX)
451                code_clocker->cc_set_param("CODE_CLOCKING", "BYTEPLEX");
452        #endif
453        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == ADVANCE_BUFFERS)
454                code_clocker->cc_set_param("CODE_CLOCKING", "ADVANCE_BUFFERS");
455        #endif
456        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
457                code_clocker->cc_set_param("CODE_CLOCKING", "BUFFER_TOTAL");
458        #endif
459        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == FILE_READING)
460                code_clocker->cc_set_param("CODE_CLOCKING", "FILE_READING");
461        #endif
462        #if defined(OMISSION) and (OMISSION == NONE)
463                code_clocker->cc_set_param("OMISSION", "NONE");
464        #endif
465        #if defined(OMISSION) and (OMISSION == END_TAG_MATCHING)
466                code_clocker->cc_set_param("OMISSION", "END_TAG_MATCHING");
467        #endif
468        #if defined(OMISSION) and (OMISSION == ATTRIBUTE_UNIQUENESS)
469                code_clocker->cc_set_param("OMISSION", "ATTRIBUTE_UNIQUENESS");
470        #endif
471        #if defined(OMISSION) and (OMISSION == NAME_VALIDATION)
472                code_clocker->cc_set_param("OMISSION", "NAME_VALIDATION");
473        #endif
474        #if defined(OMISSION) and (OMISSION == NAME_LOOKUP)
475                code_clocker->cc_set_param("OMISSION", "NAME_LOOKUP");
476        #endif
477        #if defined(VALIDATION_MODE) and (VALIDATION_MODE == ON)
478                code_clocker->cc_set_param("VALIDATION_MODE", "ON");
479        #endif
480        #if defined(VALIDATION_MODE) and (VALIDATION_MODE == OFF)
481                code_clocker->cc_set_param("VALIDATION_MODE", "OFF");
482        #endif
483       
484                code_clocker->cc_write_xml_file();
485                code_clocker->cc_display();
486                delete code_clocker;
487        #endif 
488       
489        print_stats("comment", comment_count, comment_length);
490        print_stats("CDATA section", CDATA_end_count, CDATA_length);
491        print_stats("processing instruction", PI_count, PI_length);
492        print_stats("empty element", empty_elem_count, empty_elem_length);
493        print_stats("start tag", start_tag_count, start_tag_length);
494        printf("%i total attributes\n", total_attribute_count);
495        print_stats("attribute name", total_attribute_count, total_att_name_length);
496        print_stats("attribute value", total_attribute_count, total_att_value_length);
497        print_stats("namespace name", namespace_count, total_namespace_name_length);
498        print_stats("namespace URI", namespace_count, total_namespace_URI_length);
499        print_stats("end tag", end_tag_count, end_tag_length);
500        print_stats("text item", text_item_count, text_item_length);
501        print_stats("reference", reference_count, reference_length);
502        print_stats("error item", error_item_count, error_item_length);
503        printf("Maximum nesting depth = %i\n", max_nesting_depth);
504       
505        return(0);
506}
Note: See TracBrowser for help on using the repository browser.