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