benchmark.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000-2017 Expat development team
  10. Licensed under the MIT license:
  11. Permission is hereby granted, free of charge, to any person obtaining
  12. a copy of this software and associated documentation files (the
  13. "Software"), to deal in the Software without restriction, including
  14. without limitation the rights to use, copy, modify, merge, publish,
  15. distribute, sublicense, and/or sell copies of the Software, and to permit
  16. persons to whom the Software is furnished to do so, subject to the
  17. following conditions:
  18. The above copyright notice and this permission notice shall be included
  19. in all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  23. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  26. USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <time.h>
  32. #include "expat.h"
  33. #ifdef XML_LARGE_SIZE
  34. # define XML_FMT_INT_MOD "ll"
  35. #else
  36. # define XML_FMT_INT_MOD "l"
  37. #endif
  38. #ifdef XML_UNICODE_WCHAR_T
  39. # define XML_FMT_STR "ls"
  40. #else
  41. # define XML_FMT_STR "s"
  42. #endif
  43. static void
  44. usage(const char *prog, int rc)
  45. {
  46. fprintf(stderr,
  47. "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
  48. exit(rc);
  49. }
  50. int main (int argc, char *argv[])
  51. {
  52. XML_Parser parser;
  53. char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
  54. FILE *fd;
  55. struct stat fileAttr;
  56. int nrOfLoops, bufferSize, fileSize, i, isFinal;
  57. int j = 0, ns = 0;
  58. clock_t tstart, tend;
  59. double cpuTime = 0.0;
  60. if (argc > 1) {
  61. if (argv[1][0] == '-') {
  62. if (argv[1][1] == 'n' && argv[1][2] == '\0') {
  63. ns = 1;
  64. j = 1;
  65. }
  66. else
  67. usage(argv[0], 1);
  68. }
  69. }
  70. if (argc != j + 4)
  71. usage(argv[0], 1);
  72. if (stat (argv[j + 1], &fileAttr) != 0) {
  73. fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
  74. return 2;
  75. }
  76. fd = fopen (argv[j + 1], "r");
  77. if (!fd) {
  78. fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
  79. exit(2);
  80. }
  81. bufferSize = atoi (argv[j + 2]);
  82. nrOfLoops = atoi (argv[j + 3]);
  83. if (bufferSize <= 0 || nrOfLoops <= 0) {
  84. fprintf (stderr,
  85. "buffer size and nr of loops must be greater than zero.\n");
  86. exit(3);
  87. }
  88. XMLBuf = malloc (fileAttr.st_size);
  89. fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
  90. fclose (fd);
  91. if (ns)
  92. parser = XML_ParserCreateNS(NULL, '!');
  93. else
  94. parser = XML_ParserCreate(NULL);
  95. i = 0;
  96. XMLBufEnd = XMLBuf + fileSize;
  97. while (i < nrOfLoops) {
  98. XMLBufPtr = XMLBuf;
  99. isFinal = 0;
  100. tstart = clock();
  101. do {
  102. int parseBufferSize = XMLBufEnd - XMLBufPtr;
  103. if (parseBufferSize <= bufferSize)
  104. isFinal = 1;
  105. else
  106. parseBufferSize = bufferSize;
  107. if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
  108. fprintf (stderr,
  109. "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD \
  110. "u character %" XML_FMT_INT_MOD "u\n",
  111. XML_ErrorString (XML_GetErrorCode (parser)),
  112. XML_GetCurrentLineNumber (parser),
  113. XML_GetCurrentColumnNumber (parser));
  114. free (XMLBuf);
  115. XML_ParserFree (parser);
  116. exit (4);
  117. }
  118. XMLBufPtr += bufferSize;
  119. } while (!isFinal);
  120. tend = clock();
  121. cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
  122. XML_ParserReset(parser, NULL);
  123. i++;
  124. }
  125. XML_ParserFree (parser);
  126. free (XMLBuf);
  127. printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
  128. nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
  129. return 0;
  130. }