benchmark.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-2023 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. #include <sys/stat.h>
  31. #include <assert.h>
  32. #include <stddef.h> // ptrdiff_t
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <time.h>
  36. #include "expat.h"
  37. #ifdef XML_LARGE_SIZE
  38. # define XML_FMT_INT_MOD "ll"
  39. #else
  40. # define XML_FMT_INT_MOD "l"
  41. #endif
  42. #ifdef XML_UNICODE_WCHAR_T
  43. # define XML_FMT_STR "ls"
  44. #else
  45. # define XML_FMT_STR "s"
  46. #endif
  47. static void
  48. usage(const char *prog, int rc) {
  49. fprintf(stderr, "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
  50. exit(rc);
  51. }
  52. int
  53. main(int argc, char *argv[]) {
  54. XML_Parser parser;
  55. char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
  56. FILE *fd;
  57. struct stat fileAttr;
  58. int nrOfLoops, bufferSize, i, isFinal;
  59. size_t fileSize;
  60. int j = 0, ns = 0;
  61. clock_t tstart, tend;
  62. double cpuTime = 0.0;
  63. if (argc > 1) {
  64. if (argv[1][0] == '-') {
  65. if (argv[1][1] == 'n' && argv[1][2] == '\0') {
  66. ns = 1;
  67. j = 1;
  68. } else
  69. usage(argv[0], 1);
  70. }
  71. }
  72. if (argc != j + 4)
  73. usage(argv[0], 1);
  74. if (stat(argv[j + 1], &fileAttr) != 0) {
  75. fprintf(stderr, "could not access file '%s'\n", argv[j + 1]);
  76. return 2;
  77. }
  78. fd = fopen(argv[j + 1], "r");
  79. if (! fd) {
  80. fprintf(stderr, "could not open file '%s'\n", argv[j + 1]);
  81. exit(2);
  82. }
  83. bufferSize = atoi(argv[j + 2]);
  84. nrOfLoops = atoi(argv[j + 3]);
  85. if (bufferSize <= 0 || nrOfLoops <= 0) {
  86. fprintf(stderr, "buffer size and nr of loops must be greater than zero.\n");
  87. exit(3);
  88. }
  89. XMLBuf = malloc(fileAttr.st_size);
  90. fileSize = fread(XMLBuf, sizeof(char), fileAttr.st_size, fd);
  91. fclose(fd);
  92. if (ns)
  93. parser = XML_ParserCreateNS(NULL, '!');
  94. else
  95. parser = XML_ParserCreate(NULL);
  96. i = 0;
  97. XMLBufEnd = XMLBuf + fileSize;
  98. while (i < nrOfLoops) {
  99. XMLBufPtr = XMLBuf;
  100. isFinal = 0;
  101. tstart = clock();
  102. do {
  103. ptrdiff_t parseBufferSize = XMLBufEnd - XMLBufPtr;
  104. if (parseBufferSize <= (ptrdiff_t)bufferSize)
  105. isFinal = 1;
  106. else
  107. parseBufferSize = bufferSize;
  108. assert(parseBufferSize <= (ptrdiff_t)bufferSize);
  109. if (! XML_Parse(parser, XMLBufPtr, (int)parseBufferSize, isFinal)) {
  110. fprintf(stderr,
  111. "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD
  112. "u character %" XML_FMT_INT_MOD "u\n",
  113. XML_ErrorString(XML_GetErrorCode(parser)),
  114. XML_GetCurrentLineNumber(parser),
  115. XML_GetCurrentColumnNumber(parser));
  116. free(XMLBuf);
  117. XML_ParserFree(parser);
  118. exit(4);
  119. }
  120. XMLBufPtr += bufferSize;
  121. } while (! isFinal);
  122. tend = clock();
  123. cpuTime += ((double)(tend - tstart)) / CLOCKS_PER_SEC;
  124. XML_ParserReset(parser, NULL);
  125. i++;
  126. }
  127. XML_ParserFree(parser);
  128. free(XMLBuf);
  129. printf("%d loops, with buffer size %d. Average time per loop: %f\n",
  130. nrOfLoops, bufferSize, cpuTime / (double)nrOfLoops);
  131. return 0;
  132. }