Added support for XML_LARGE_SIZE.
[mirror-ossqm-expat.git] / tests / benchmark / benchmark.c
blob86266a00f33a7a2e921a6f9c8508c36c5ee45ef5
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 i = 0;
76 XMLBufEnd = XMLBuf + fileSize;
77 while (i < nrOfLoops) {
78 XMLBufPtr = XMLBuf;
79 isFinal = 0;
80 if (ns)
81 parser = XML_ParserCreateNS(NULL, '!');
82 else
83 parser = XML_ParserCreate(NULL);
84 tstart = clock();
85 do {
86 int parseBufferSize = XMLBufEnd - XMLBufPtr;
87 if (parseBufferSize <= bufferSize)
88 isFinal = 1;
89 else
90 parseBufferSize = bufferSize;
91 if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
92 fprintf (stderr, "error '%s' at line %" XML_FMT_INT_MOD \
93 "u character %" XML_FMT_INT_MOD "u\n",
94 XML_ErrorString (XML_GetErrorCode (parser)),
95 XML_GetCurrentLineNumber (parser),
96 XML_GetCurrentColumnNumber (parser));
97 free (XMLBuf);
98 XML_ParserFree (parser);
99 exit (4);
101 XMLBufPtr += bufferSize;
102 } while (!isFinal);
103 tend = clock();
104 cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
105 XML_ParserFree (parser);
106 i++;
109 free (XMLBuf);
111 printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
112 nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
113 return 0;