iksperf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. char *xml;
  178. int err;
  179. prs = iks_dom_new (&x);
  180. err = iks_parse (prs, buf, len, 1);
  181. switch (err) {
  182. case IKS_OK:
  183. break;
  184. case IKS_NOMEM:
  185. exit (2);
  186. case IKS_BADXML:
  187. fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
  188. iks_nr_bytes (prs), iks_nr_lines (prs));
  189. exit (1);
  190. case IKS_HOOK:
  191. exit (1);
  192. }
  193. iks_parser_delete (prs);
  194. t_reset ();
  195. xml = iks_string (iks_stack (x), x);
  196. time = t_elapsed ();
  197. printf ("Serialize: serializing the tree took %ld milliseconds.\n", time);
  198. iks_delete (x);
  199. }
  200. void
  201. sha_test (char *buf, int len)
  202. {
  203. unsigned long time;
  204. iksha *s;
  205. char out[41];
  206. t_reset ();
  207. s = iks_sha_new ();
  208. iks_sha_hash (s, buf, len, 1);
  209. iks_sha_print (s, out);
  210. out[40] = '\0';
  211. iks_sha_delete (s);
  212. time = t_elapsed ();
  213. printf ("SHA: hashing took %ld milliseconds.\n", time);
  214. printf ("SHA: hash [%s]\n", out);
  215. }
  216. int
  217. main (int argc, char *argv[])
  218. {
  219. int test_type = 0;
  220. int c;
  221. #ifdef HAVE_GETOPT_LONG
  222. int i;
  223. while ((c = getopt_long (argc, argv, shortopts, longopts, &i)) != -1) {
  224. #else
  225. while ((c = getopt (argc, argv, shortopts)) != -1) {
  226. #endif
  227. switch (c) {
  228. case 'a':
  229. test_type = 0xffff;
  230. break;
  231. case 's':
  232. test_type |= 1;
  233. break;
  234. case 'd':
  235. test_type |= 2;
  236. break;
  237. case 'e':
  238. test_type |= 4;
  239. break;
  240. case '1':
  241. test_type |= 8;
  242. break;
  243. case 'b':
  244. block_size = atoi (optarg);
  245. break;
  246. case 'm':
  247. m_trace ();
  248. break;
  249. case 'h':
  250. print_usage ();
  251. exit (0);
  252. case 'V':
  253. puts ("iksperf (iksemel) "VERSION);
  254. exit (0);
  255. }
  256. }
  257. for (; optind < argc; optind++) {
  258. char *buf;
  259. int len;
  260. buf = load_file (argv[optind], &len);
  261. if (test_type & 1) sax_test (buf, len);
  262. if (test_type == 0 || test_type & 2) dom_test (buf, len);
  263. if (test_type & 4) serialize_test (buf, len);
  264. if (test_type & 8) sha_test (buf, len);
  265. free (buf);
  266. }
  267. return 0;
  268. }