1 #include <sys/stat.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <time.h>
5 #include "expat.h"
6 
7 #ifdef XML_LARGE_SIZE
8 #define XML_FMT_INT_MOD "ll"
9 #else
10 #define XML_FMT_INT_MOD "l"
11 #endif
12 
13 static void
14 usage(const char *prog, int rc)
15 {
16   fprintf(stderr,
17           "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
18   exit(rc);
19 }
20 
21 int main (int argc, char *argv[])
22 {
23   XML_Parser  parser;
24   char        *XMLBuf, *XMLBufEnd, *XMLBufPtr;
25   FILE        *fd;
26   struct stat fileAttr;
27   int         nrOfLoops, bufferSize, fileSize, i, isFinal;
28   int         j = 0, ns = 0;
29   clock_t     tstart, tend;
30   double      cpuTime = 0.0;
31 
32   if (argc > 1) {
33     if (argv[1][0] == '-') {
34       if (argv[1][1] == 'n' && argv[1][2] == '\0') {
35         ns = 1;
36         j = 1;
37       }
38       else
39         usage(argv[0], 1);
40     }
41   }
42 
43   if (argc != j + 4)
44     usage(argv[0], 1);
45 
46   if (stat (argv[j + 1], &fileAttr) != 0) {
47     fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
48     return 2;
49   }
50 
51   fd = fopen (argv[j + 1], "r");
52   if (!fd) {
53     fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
54     exit(2);
55   }
56 
57   bufferSize = atoi (argv[j + 2]);
58   nrOfLoops = atoi (argv[j + 3]);
59   if (bufferSize <= 0 || nrOfLoops <= 0) {
60     fprintf (stderr,
61              "buffer size and nr of loops must be greater than zero.\n");
62     exit(3);
63   }
64 
65   XMLBuf = malloc (fileAttr.st_size);
66   fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
67   fclose (fd);
68 
69   if (ns)
70     parser = XML_ParserCreateNS(NULL, '!');
71   else
72     parser = XML_ParserCreate(NULL);
73 
74   i = 0;
75   XMLBufEnd = XMLBuf + fileSize;
76   while (i < nrOfLoops) {
77     XMLBufPtr = XMLBuf;
78     isFinal = 0;
79     tstart = clock();
80     do {
81       int parseBufferSize = XMLBufEnd - XMLBufPtr;
82       if (parseBufferSize <= bufferSize)
83         isFinal = 1;
84       else
85         parseBufferSize = bufferSize;
86       if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
87         fprintf (stderr, "error '%s' at line %" XML_FMT_INT_MOD \
88                      "u character %" XML_FMT_INT_MOD "u\n",
89                  XML_ErrorString (XML_GetErrorCode (parser)),
90                  XML_GetCurrentLineNumber (parser),
91                  XML_GetCurrentColumnNumber (parser));
92         free (XMLBuf);
93         XML_ParserFree (parser);
94         exit (4);
95       }
96       XMLBufPtr += bufferSize;
97     } while (!isFinal);
98     tend = clock();
99     cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
100     XML_ParserReset(parser, NULL);
101     i++;
102   }
103 
104   XML_ParserFree (parser);
105   free (XMLBuf);
106 
107   printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
108           nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
109   return 0;
110 }
111