You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

content_type_bench.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "printf.h"
  18. #include "message.h"
  19. #include "util.h"
  20. #include "content_type.h"
  21. static gdouble total_time = 0;
  22. static gint total_parsed = 0;
  23. static gint total_valid = 0;
  24. static gint total_type = 0;
  25. static gint total_subtype = 0;
  26. static gint total_charset = 0;
  27. static gint total_attrs = 0;
  28. static gint total_boundaries = 0;
  29. static gboolean verbose = 1;
  30. #define MODE_NORMAL 0
  31. #define MODE_GMIME 1
  32. #define MODE_COMPARE 2
  33. static void
  34. rspamd_process_file (const gchar *fname, gint mode)
  35. {
  36. rspamd_mempool_t *pool;
  37. GIOChannel *f;
  38. GError *err = NULL;
  39. GString *buf;
  40. struct rspamd_content_type *ct;
  41. gdouble t1, t2;
  42. rspamd_ftok_t t;
  43. f = g_io_channel_new_file (fname, "r", &err);
  44. if (!f) {
  45. rspamd_fprintf (stderr, "cannot open %s: %e\n", fname, err);
  46. g_error_free (err);
  47. return;
  48. }
  49. g_io_channel_set_encoding (f, NULL, NULL);
  50. buf = g_string_sized_new (8192);
  51. pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "test");
  52. while (g_io_channel_read_line_string (f, buf, NULL, &err)
  53. == G_IO_STATUS_NORMAL) {
  54. while (buf->len > 0 && g_ascii_isspace (buf->str[buf->len - 1])) {
  55. buf->len --;
  56. }
  57. if (mode == MODE_NORMAL) {
  58. t1 = rspamd_get_virtual_ticks ();
  59. ct = rspamd_content_type_parse (buf->str, buf->len, pool);
  60. t2 = rspamd_get_virtual_ticks ();
  61. }
  62. else {
  63. rspamd_fprintf (stderr, "gmime is no longer supported\n");
  64. exit (EXIT_FAILURE);
  65. }
  66. total_time += t2 - t1;
  67. total_parsed ++;
  68. if (mode == MODE_NORMAL) {
  69. if (ct) {
  70. total_valid ++;
  71. if (ct->type.len > 0) {
  72. total_type ++;
  73. }
  74. if (ct->subtype.len > 0) {
  75. total_subtype ++;
  76. }
  77. if (ct->charset.len > 0) {
  78. total_charset ++;
  79. }
  80. if (ct->boundary.len > 0) {
  81. total_boundaries ++;
  82. }
  83. if (ct->attrs) {
  84. total_attrs ++;
  85. }
  86. }
  87. }
  88. }
  89. if (err) {
  90. rspamd_fprintf (stderr, "cannot read %s: %e\n", fname, err);
  91. g_error_free (err);
  92. }
  93. g_io_channel_unref (f);
  94. g_string_free (buf, TRUE);
  95. rspamd_mempool_delete (pool);
  96. }
  97. int
  98. main (int argc, char **argv)
  99. {
  100. gint i, start = 1, mode = MODE_NORMAL;
  101. if (argc > 2 && *argv[1] == '-') {
  102. start = 2;
  103. if (argv[1][1] == 'g') {
  104. mode = MODE_GMIME;
  105. }
  106. else if (argv[1][1] == 'c') {
  107. mode = MODE_COMPARE;
  108. }
  109. }
  110. for (i = start; i < argc; i ++) {
  111. if (argv[i]) {
  112. rspamd_process_file (argv[i], mode);
  113. }
  114. }
  115. if (mode != MODE_COMPARE) {
  116. rspamd_printf ("Parsed %d received headers in %.3f seconds\n"
  117. "Total valid (has type): %d\n"
  118. "Total known type: %d\n"
  119. "Total known subtype: %d\n"
  120. "Total known charset: %d\n"
  121. "Total has attrs: %d\n"
  122. "Total has boundaries: %d\n",
  123. total_parsed, total_time,
  124. total_valid, total_type,
  125. total_subtype, total_charset,
  126. total_attrs,
  127. total_boundaries);
  128. }
  129. else {
  130. rspamd_printf ("Parsed %d received headers in %.3f seconds\n"
  131. "Total valid (parsed by both): %d\n"
  132. "Total same type: %d\n"
  133. "Total same subtype: %d\n"
  134. "Total same charset: %d\n"
  135. "Total same boundaries: %d\n",
  136. total_parsed, total_time,
  137. total_valid, total_type,
  138. total_subtype, total_charset,
  139. total_boundaries);
  140. }
  141. return 0;
  142. }