1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000-2017 Expat development team
11    Licensed under the MIT license:
12 
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20 
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23 
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32 
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <time.h>
37 #include "expat.h"
38 
39 #ifdef XML_LARGE_SIZE
40 # define XML_FMT_INT_MOD "ll"
41 #else
42 # define XML_FMT_INT_MOD "l"
43 #endif
44 
45 #ifdef XML_UNICODE_WCHAR_T
46 # define XML_FMT_STR "ls"
47 #else
48 # define XML_FMT_STR "s"
49 #endif
50 
51 static void
52 usage(const char *prog, int rc)
53 {
54   fprintf(stderr,
55           "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
56   exit(rc);
57 }
58 
59 int main (int argc, char *argv[])
60 {
61   XML_Parser  parser;
62   char        *XMLBuf, *XMLBufEnd, *XMLBufPtr;
63   FILE        *fd;
64   struct stat fileAttr;
65   int         nrOfLoops, bufferSize, fileSize, i, isFinal;
66   int         j = 0, ns = 0;
67   clock_t     tstart, tend;
68   double      cpuTime = 0.0;
69 
70   if (argc > 1) {
71     if (argv[1][0] == '-') {
72       if (argv[1][1] == 'n' && argv[1][2] == '\0') {
73         ns = 1;
74         j = 1;
75       }
76       else
77         usage(argv[0], 1);
78     }
79   }
80 
81   if (argc != j + 4)
82     usage(argv[0], 1);
83 
84   if (stat (argv[j + 1], &fileAttr) != 0) {
85     fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
86     return 2;
87   }
88 
89   fd = fopen (argv[j + 1], "r");
90   if (!fd) {
91     fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
92     exit(2);
93   }
94 
95   bufferSize = atoi (argv[j + 2]);
96   nrOfLoops = atoi (argv[j + 3]);
97   if (bufferSize <= 0 || nrOfLoops <= 0) {
98     fprintf (stderr,
99              "buffer size and nr of loops must be greater than zero.\n");
100     exit(3);
101   }
102 
103   XMLBuf = malloc (fileAttr.st_size);
104   fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
105   fclose (fd);
106 
107   if (ns)
108     parser = XML_ParserCreateNS(NULL, '!');
109   else
110     parser = XML_ParserCreate(NULL);
111 
112   i = 0;
113   XMLBufEnd = XMLBuf + fileSize;
114   while (i < nrOfLoops) {
115     XMLBufPtr = XMLBuf;
116     isFinal = 0;
117     tstart = clock();
118     do {
119       int parseBufferSize = XMLBufEnd - XMLBufPtr;
120       if (parseBufferSize <= bufferSize)
121         isFinal = 1;
122       else
123         parseBufferSize = bufferSize;
124       if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
125         fprintf (stderr,
126                  "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD   \
127                      "u character %" XML_FMT_INT_MOD "u\n",
128                  XML_ErrorString (XML_GetErrorCode (parser)),
129                  XML_GetCurrentLineNumber (parser),
130                  XML_GetCurrentColumnNumber (parser));
131         free (XMLBuf);
132         XML_ParserFree (parser);
133         exit (4);
134       }
135       XMLBufPtr += bufferSize;
136     } while (!isFinal);
137     tend = clock();
138     cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
139     XML_ParserReset(parser, NULL);
140     i++;
141   }
142 
143   XML_ParserFree (parser);
144   free (XMLBuf);
145 
146   printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
147           nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
148   return 0;
149 }
150