benchmark.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <sys/stat.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include "expat.h"
  6. #ifdef XML_LARGE_SIZE
  7. #define XML_FMT_INT_MOD "ll"
  8. #else
  9. #define XML_FMT_INT_MOD "l"
  10. #endif
  11. static void
  12. usage(const char *prog, int rc)
  13. {
  14. fprintf(stderr,
  15. "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
  16. exit(rc);
  17. }
  18. int main (int argc, char *argv[])
  19. {
  20. XML_Parser parser;
  21. char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
  22. FILE *fd;
  23. struct stat fileAttr;
  24. int nrOfLoops, bufferSize, fileSize, i, isFinal;
  25. int j = 0, ns = 0;
  26. clock_t tstart, tend;
  27. double cpuTime = 0.0;
  28. if (argc > 1) {
  29. if (argv[1][0] == '-') {
  30. if (argv[1][1] == 'n' && argv[1][2] == '\0') {
  31. ns = 1;
  32. j = 1;
  33. }
  34. else
  35. usage(argv[0], 1);
  36. }
  37. }
  38. if (argc != j + 4)
  39. usage(argv[0], 1);
  40. if (stat (argv[j + 1], &fileAttr) != 0) {
  41. fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
  42. return 2;
  43. }
  44. fd = fopen (argv[j + 1], "r");
  45. if (!fd) {
  46. fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
  47. exit(2);
  48. }
  49. bufferSize = atoi (argv[j + 2]);
  50. nrOfLoops = atoi (argv[j + 3]);
  51. if (bufferSize <= 0 || nrOfLoops <= 0) {
  52. fprintf (stderr,
  53. "buffer size and nr of loops must be greater than zero.\n");
  54. exit(3);
  55. }
  56. XMLBuf = malloc (fileAttr.st_size);
  57. fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
  58. fclose (fd);
  59. if (ns)
  60. parser = XML_ParserCreateNS(NULL, '!');
  61. else
  62. parser = XML_ParserCreate(NULL);
  63. i = 0;
  64. XMLBufEnd = XMLBuf + fileSize;
  65. while (i < nrOfLoops) {
  66. XMLBufPtr = XMLBuf;
  67. isFinal = 0;
  68. tstart = clock();
  69. do {
  70. int parseBufferSize = XMLBufEnd - XMLBufPtr;
  71. if (parseBufferSize <= bufferSize)
  72. isFinal = 1;
  73. else
  74. parseBufferSize = bufferSize;
  75. if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
  76. fprintf (stderr, "error '%s' at line %" XML_FMT_INT_MOD \
  77. "u character %" XML_FMT_INT_MOD "u\n",
  78. XML_ErrorString (XML_GetErrorCode (parser)),
  79. XML_GetCurrentLineNumber (parser),
  80. XML_GetCurrentColumnNumber (parser));
  81. free (XMLBuf);
  82. XML_ParserFree (parser);
  83. exit (4);
  84. }
  85. XMLBufPtr += bufferSize;
  86. } while (!isFinal);
  87. tend = clock();
  88. cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
  89. XML_ParserReset(parser, NULL);
  90. i++;
  91. }
  92. XML_ParserFree (parser);
  93. free (XMLBuf);
  94. printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
  95. nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
  96. return 0;
  97. }