benchmark.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2003-2006 Karl Waclawek <[email protected]>
  9. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  10. Copyright (c) 2017-2025 Sebastian Pipping <[email protected]>
  11. Copyright (c) 2017 Rhodri James <[email protected]>
  12. Licensed under the MIT license:
  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. The above copyright notice and this permission notice shall be included
  21. in all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  25. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  26. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  27. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  28. USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #define _POSIX_C_SOURCE 1 // fdopen
  31. #if defined(_MSC_VER)
  32. # include <io.h> // _open, _close
  33. #else
  34. # include <unistd.h> // close
  35. #endif
  36. #include <fcntl.h> // open
  37. #include <sys/stat.h>
  38. #include <assert.h>
  39. #include <stddef.h> // ptrdiff_t
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include "expat.h"
  43. #ifdef XML_LARGE_SIZE
  44. # define XML_FMT_INT_MOD "ll"
  45. #else
  46. # define XML_FMT_INT_MOD "l"
  47. #endif
  48. #ifdef XML_UNICODE_WCHAR_T
  49. # define XML_FMT_STR "ls"
  50. #else
  51. # define XML_FMT_STR "s"
  52. #endif
  53. static int
  54. usage(const char *prog, int rc) {
  55. fprintf(stderr, "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
  56. return rc;
  57. }
  58. int
  59. main(int argc, char *argv[]) {
  60. XML_Parser parser;
  61. char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
  62. int fd;
  63. FILE *file;
  64. struct stat fileAttr;
  65. int nrOfLoops, bufferSize, i, isFinal;
  66. size_t fileSize;
  67. int j = 0, ns = 0;
  68. clock_t tstart, tend;
  69. double cpuTime = 0.0;
  70. if (argc > 1) {
  71. if (argv[1][0] == '-') {
  72. if (argv[1][1] == 'n' && argv[1][2] == '\0') {
  73. ns = 1;
  74. j = 1;
  75. } else
  76. return usage(argv[0], 1);
  77. }
  78. }
  79. if (argc != j + 4)
  80. return usage(argv[0], 1);
  81. fd = open(argv[j + 1], O_RDONLY);
  82. if (fd == -1) {
  83. fprintf(stderr, "could not open file '%s'\n", argv[j + 1]);
  84. return 2;
  85. }
  86. if (fstat(fd, &fileAttr) != 0) {
  87. close(fd);
  88. fprintf(stderr, "could not fstat file '%s'\n", argv[j + 1]);
  89. return 2;
  90. }
  91. file = fdopen(fd, "r");
  92. if (! file) {
  93. close(fd);
  94. fprintf(stderr, "could not fdopen file '%s'\n", argv[j + 1]);
  95. return 2;
  96. }
  97. bufferSize = atoi(argv[j + 2]);
  98. nrOfLoops = atoi(argv[j + 3]);
  99. if (bufferSize <= 0 || nrOfLoops <= 0) {
  100. fclose(file);
  101. close(fd);
  102. fprintf(stderr, "buffer size and nr of loops must be greater than zero.\n");
  103. return 3;
  104. }
  105. XMLBuf = malloc(fileAttr.st_size);
  106. if (XMLBuf == NULL) {
  107. fclose(file);
  108. close(fd);
  109. fprintf(stderr, "ouf of memory.\n");
  110. return 5;
  111. }
  112. fileSize = fread(XMLBuf, sizeof(char), fileAttr.st_size, file);
  113. fclose(file);
  114. close(fd);
  115. if (ns)
  116. parser = XML_ParserCreateNS(NULL, '!');
  117. else
  118. parser = XML_ParserCreate(NULL);
  119. i = 0;
  120. XMLBufEnd = XMLBuf + fileSize;
  121. while (i < nrOfLoops) {
  122. XMLBufPtr = XMLBuf;
  123. isFinal = 0;
  124. tstart = clock();
  125. do {
  126. ptrdiff_t parseBufferSize = XMLBufEnd - XMLBufPtr;
  127. if (parseBufferSize <= (ptrdiff_t)bufferSize)
  128. isFinal = 1;
  129. else
  130. parseBufferSize = bufferSize;
  131. assert(parseBufferSize <= (ptrdiff_t)bufferSize);
  132. if (! XML_Parse(parser, XMLBufPtr, (int)parseBufferSize, isFinal)) {
  133. fprintf(stderr,
  134. "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD
  135. "u character %" XML_FMT_INT_MOD "u\n",
  136. XML_ErrorString(XML_GetErrorCode(parser)),
  137. XML_GetCurrentLineNumber(parser),
  138. XML_GetCurrentColumnNumber(parser));
  139. free(XMLBuf);
  140. XML_ParserFree(parser);
  141. return 4;
  142. }
  143. XMLBufPtr += bufferSize;
  144. } while (! isFinal);
  145. tend = clock();
  146. cpuTime += ((double)(tend - tstart)) / CLOCKS_PER_SEC;
  147. XML_ParserReset(parser, NULL);
  148. i++;
  149. }
  150. XML_ParserFree(parser);
  151. free(XMLBuf);
  152. printf("%d loops, with buffer size %d. Average time per loop: %f\n",
  153. nrOfLoops, bufferSize, cpuTime / (double)nrOfLoops);
  154. return 0;
  155. }