iksperf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #include "common.h"
  7. #include "iksemel.h"
  8. #include "perf.h"
  9. #include <sys/stat.h>
  10. #ifdef HAVE_GETOPT_LONG
  11. #include <getopt.h>
  12. #endif
  13. #ifdef HAVE_GETOPT_LONG
  14. static struct option longopts[] = {
  15. { "all", 0, 0, 'a' },
  16. { "sax", 0, 0, 's' },
  17. { "dom", 0, 0, 'd' },
  18. { "serialize", 0, 0, 'e' },
  19. { "sha1", 0, 0, '1' },
  20. { "block", required_argument, 0, 'b' },
  21. { "memdbg", 0, 0, 'm' },
  22. { "help", 0, 0, 'h' },
  23. { "version", 0, 0, 'V' },
  24. { 0, 0, 0, 0 }
  25. };
  26. #endif
  27. static char *shortopts = "asde1b:mhV";
  28. static void
  29. print_usage (void)
  30. {
  31. puts ("Usage: iksperf [OPTIONS] FILE\n"
  32. "This tool measures the performance of the iksemel library.\n"
  33. " -a, --all Make all tests.\n"
  34. " -s, --sax Sax test.\n"
  35. " -d, --dom Tree generating test.\n"
  36. " -e, --serialize Tree serializing test.\n"
  37. " -1, --sha1 SHA1 hashing test.\n"
  38. " -b, --block SIZE Parse the file in SIZE byte blocks.\n"
  39. " -m, --memdbg Trace malloc and free calls.\n"
  40. " -h, --help Print this text and exit.\n"
  41. " -V, --version Print version and exit.\n"
  42. #ifndef HAVE_GETOPT_LONG
  43. "(long options are not supported on your system)\n"
  44. #endif
  45. "Report bugs to <iksemel-dev@jabberstudio.org>.");
  46. }
  47. /* if not 0, file is parsed in block_size byte blocks */
  48. int block_size;
  49. char *load_file (const char *fname, int *sizeptr)
  50. {
  51. FILE *f;
  52. char *buf;
  53. struct stat fs;
  54. size_t size, ret;
  55. if (stat (fname, &fs) != 0) {
  56. fprintf (stderr, "Cannot access file '%s'.\n", fname);
  57. exit (1);
  58. }
  59. size = fs.st_size;
  60. printf ("Test file '%s' (%d bytes):\n", fname, size);
  61. f = fopen (fname, "rb");
  62. if (!f) {
  63. fprintf (stderr, "Cannot open file.\n");
  64. exit (1);
  65. }
  66. buf = malloc (size);
  67. if (!buf) {
  68. fclose (f);
  69. fprintf (stderr, "Cannot allocate %d bytes for buffer.\n", size);
  70. exit (2);
  71. }
  72. ret = fread (buf, 1, size, f);
  73. if (ret < size) {
  74. fprintf (stderr, "Read error in file.\n");
  75. exit (1);
  76. }
  77. *sizeptr = size;
  78. fclose (f);
  79. return buf;
  80. }
  81. /* stats */
  82. int sax_tag;
  83. int sax_cdata;
  84. int
  85. tagHook (void *udata, char *name, char **atts, int type)
  86. {
  87. ++sax_tag;
  88. return IKS_OK;
  89. }
  90. int
  91. cdataHook (void *udata, char *data, size_t len)
  92. {
  93. ++sax_cdata;
  94. return IKS_OK;
  95. }
  96. void
  97. sax_test (char *buf, int len)
  98. {
  99. unsigned long time;
  100. iksparser *prs;
  101. int bs, i, err;
  102. bs = block_size;
  103. if (0 == bs) bs = len;
  104. sax_tag = 0;
  105. sax_cdata = 0;
  106. t_reset ();
  107. prs = iks_sax_new (NULL, tagHook, cdataHook);
  108. i = 0;
  109. while (i < len) {
  110. if (i + bs > len) bs = len - i;
  111. err = iks_parse (prs, buf + i, bs, 0);
  112. switch (err) {
  113. case IKS_OK:
  114. break;
  115. case IKS_NOMEM:
  116. exit (2);
  117. case IKS_BADXML:
  118. fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
  119. iks_nr_bytes (prs), iks_nr_lines (prs));
  120. exit (1);
  121. case IKS_HOOK:
  122. exit (1);
  123. }
  124. i += bs;
  125. }
  126. time = t_elapsed ();
  127. printf ("SAX: parsing took %ld milliseconds.\n", time);
  128. printf ("SAX: tag hook called %d, cdata hook called %d times.\n", sax_tag, sax_cdata);
  129. iks_parser_delete (prs);
  130. }
  131. void dom_test (char *buf, int len)
  132. {
  133. int bs, i, err;
  134. iksparser *prs;
  135. unsigned long time;
  136. iks *x;
  137. size_t allocated, used;
  138. bs = block_size;
  139. if (0 == bs) bs = len;
  140. t_reset ();
  141. prs = iks_dom_new (&x);
  142. iks_set_size_hint (prs, len);
  143. i = 0;
  144. while (i < len) {
  145. if (i + bs > len) bs = len - i;
  146. err = iks_parse (prs, buf + i, bs, 0);
  147. switch (err) {
  148. case IKS_OK:
  149. break;
  150. case IKS_NOMEM:
  151. exit (2);
  152. case IKS_BADXML:
  153. fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
  154. iks_nr_bytes (prs), iks_nr_lines (prs));
  155. exit (1);
  156. case IKS_HOOK:
  157. exit (1);
  158. }
  159. i += bs;
  160. }
  161. time = t_elapsed ();
  162. iks_stack_stat (iks_stack (x), &allocated, &used);
  163. printf ("DOM: parsing and building the tree took %ld milliseconds.\n", time);
  164. printf ("DOM: ikstack: %d bytes allocated, %d bytes used.\n", allocated, used);
  165. t_reset ();
  166. iks_delete (x);
  167. time = t_elapsed ();
  168. printf ("DOM: deleting the tree took %ld milliseconds.\n", time);
  169. iks_parser_delete (prs);
  170. }
  171. void
  172. serialize_test (char *buf, int len)
  173. {
  174. unsigned long time;
  175. iks *x;
  176. iksparser *prs;
  177. int err;
  178. prs = iks_dom_new (&x);
  179. err = iks_parse (prs, buf, len, 1);
  180. switch (err) {
  181. case IKS_OK:
  182. break;
  183. case IKS_NOMEM:
  184. exit (2);
  185. case IKS_BADXML:
  186. fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
  187. iks_nr_bytes (prs), iks_nr_lines (prs));
  188. exit (1);
  189. case IKS_HOOK:
  190. exit (1);
  191. }
  192. iks_parser_delete (prs);
  193. t_reset ();
  194. iks_string (iks_stack (x), x);
  195. time = t_elapsed ();
  196. printf ("Serialize: serializing the tree took %ld milliseconds.\n", time);
  197. iks_delete (x);
  198. }
  199. void
  200. sha_test (char *buf, int len)
  201. {
  202. unsigned long time;
  203. iksha *s;
  204. char out[41];
  205. t_reset ();
  206. s = iks_sha_new ();
  207. iks_sha_hash (s, buf, len, 1);
  208. iks_sha_print (s, out);
  209. out[40] = '\0';
  210. iks_sha_delete (s);
  211. time = t_elapsed ();
  212. printf ("SHA: hashing took %ld milliseconds.\n", time);
  213. printf ("SHA: hash [%s]\n", out);
  214. }
  215. int
  216. main (int argc, char *argv[])
  217. {
  218. int test_type = 0;
  219. int c;
  220. #ifdef HAVE_GETOPT_LONG
  221. int i;
  222. while ((c = getopt_long (argc, argv, shortopts, longopts, &i)) != -1) {
  223. #else
  224. while ((c = getopt (argc, argv, shortopts)) != -1) {
  225. #endif
  226. switch (c) {
  227. case 'a':
  228. test_type = 0xffff;
  229. break;
  230. case 's':
  231. test_type |= 1;
  232. break;
  233. case 'd':
  234. test_type |= 2;
  235. break;
  236. case 'e':
  237. test_type |= 4;
  238. break;
  239. case '1':
  240. test_type |= 8;
  241. break;
  242. case 'b':
  243. block_size = atoi (optarg);
  244. break;
  245. case 'm':
  246. m_trace ();
  247. break;
  248. case 'h':
  249. print_usage ();
  250. exit (0);
  251. case 'V':
  252. puts ("iksperf (iksemel) "VERSION);
  253. exit (0);
  254. }
  255. }
  256. for (; optind < argc; optind++) {
  257. char *buf;
  258. int len;
  259. buf = load_file (argv[optind], &len);
  260. if (test_type & 1) sax_test (buf, len);
  261. if (test_type == 0 || test_type & 2) dom_test (buf, len);
  262. if (test_type & 4) serialize_test (buf, len);
  263. if (test_type & 8) sha_test (buf, len);
  264. free (buf);
  265. }
  266. return 0;
  267. }