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
usage(const char * prog,int rc)52 usage(const char *prog, int rc) {
53 fprintf(stderr, "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
54 exit(rc);
55 }
56
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[]) {
59 XML_Parser parser;
60 char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
61 FILE *fd;
62 struct stat fileAttr;
63 int nrOfLoops, bufferSize, fileSize, i, isFinal;
64 int j = 0, ns = 0;
65 clock_t tstart, tend;
66 double cpuTime = 0.0;
67
68 if (argc > 1) {
69 if (argv[1][0] == '-') {
70 if (argv[1][1] == 'n' && argv[1][2] == '\0') {
71 ns = 1;
72 j = 1;
73 } else
74 usage(argv[0], 1);
75 }
76 }
77
78 if (argc != j + 4)
79 usage(argv[0], 1);
80
81 if (stat(argv[j + 1], &fileAttr) != 0) {
82 fprintf(stderr, "could not access file '%s'\n", argv[j + 1]);
83 return 2;
84 }
85
86 fd = fopen(argv[j + 1], "r");
87 if (! fd) {
88 fprintf(stderr, "could not open file '%s'\n", argv[j + 1]);
89 exit(2);
90 }
91
92 bufferSize = atoi(argv[j + 2]);
93 nrOfLoops = atoi(argv[j + 3]);
94 if (bufferSize <= 0 || nrOfLoops <= 0) {
95 fprintf(stderr, "buffer size and nr of loops must be greater than zero.\n");
96 exit(3);
97 }
98
99 XMLBuf = malloc(fileAttr.st_size);
100 fileSize = fread(XMLBuf, sizeof(char), fileAttr.st_size, fd);
101 fclose(fd);
102
103 if (ns)
104 parser = XML_ParserCreateNS(NULL, '!');
105 else
106 parser = XML_ParserCreate(NULL);
107
108 i = 0;
109 XMLBufEnd = XMLBuf + fileSize;
110 while (i < nrOfLoops) {
111 XMLBufPtr = XMLBuf;
112 isFinal = 0;
113 tstart = clock();
114 do {
115 int parseBufferSize = XMLBufEnd - XMLBufPtr;
116 if (parseBufferSize <= bufferSize)
117 isFinal = 1;
118 else
119 parseBufferSize = bufferSize;
120 if (! XML_Parse(parser, XMLBufPtr, parseBufferSize, isFinal)) {
121 fprintf(stderr,
122 "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD
123 "u character %" XML_FMT_INT_MOD "u\n",
124 XML_ErrorString(XML_GetErrorCode(parser)),
125 XML_GetCurrentLineNumber(parser),
126 XML_GetCurrentColumnNumber(parser));
127 free(XMLBuf);
128 XML_ParserFree(parser);
129 exit(4);
130 }
131 XMLBufPtr += bufferSize;
132 } while (! isFinal);
133 tend = clock();
134 cpuTime += ((double)(tend - tstart)) / CLOCKS_PER_SEC;
135 XML_ParserReset(parser, NULL);
136 i++;
137 }
138
139 XML_ParserFree(parser);
140 free(XMLBuf);
141
142 printf("%d loops, with buffer size %d. Average time per loop: %f\n",
143 nrOfLoops, bufferSize, cpuTime / (double)nrOfLoops);
144 return 0;
145 }
146