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