perf-regex.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file src/regex/perf-regex.c
  18. * @brief Test how long it takes to create a automaton from a string regex.
  19. * @author Bartlomiej Polot
  20. */
  21. #include <regex.h>
  22. #include <time.h>
  23. #include "platform.h"
  24. #include "regex_internal_lib.h"
  25. #include "regex_test_lib.h"
  26. /**
  27. * Print information about the given node and its edges
  28. * to stdout.
  29. *
  30. * @param cls closure, unused.
  31. * @param key hash for current state.
  32. * @param proof proof for current state.
  33. * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
  34. * @param num_edges number of edges leaving current state.
  35. * @param edges edges leaving current state.
  36. */
  37. static void
  38. print_edge (void *cls,
  39. const struct GNUNET_HashCode *key,
  40. const char *proof,
  41. int accepting,
  42. unsigned int num_edges,
  43. const struct REGEX_BLOCK_Edge *edges)
  44. {
  45. unsigned int i;
  46. printf ("%s: %s, proof: `%s'\n",
  47. GNUNET_h2s (key),
  48. accepting ? "ACCEPTING" : "",
  49. proof);
  50. for (i = 0; i < num_edges; i++)
  51. printf (" `%s': %s\n",
  52. edges[i].label,
  53. GNUNET_h2s (&edges[i].destination));
  54. }
  55. /**
  56. * The main function of the regex performace test.
  57. *
  58. * Read a set of regex from a file, combine them and create a DFA from the
  59. * resulting combined regex.
  60. *
  61. * @param argc number of arguments from the command line
  62. * @param argv command line arguments
  63. * @return 0 ok, 1 on error
  64. */
  65. int
  66. main (int argc, char *const *argv)
  67. {
  68. struct REGEX_INTERNAL_Automaton*dfa;
  69. char **regexes;
  70. char *buffer;
  71. char *regex;
  72. int compression;
  73. unsigned int alphabet_size;
  74. long size;
  75. GNUNET_log_setup ("perf-regex", "DEBUG", NULL);
  76. if (4 != argc)
  77. {
  78. fprintf (stderr,
  79. "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n",
  80. argv[0]);
  81. return 1;
  82. }
  83. regexes = REGEX_TEST_read_from_file (argv[1]);
  84. if (NULL == regexes)
  85. {
  86. fprintf (stderr,
  87. "Failed to read regexes from `%s'\n",
  88. argv[1]);
  89. return 2;
  90. }
  91. alphabet_size = atoi (argv[2]);
  92. compression = atoi (argv[3]);
  93. printf ("********* PERF-REGEX *********'\n");
  94. printf ("Using:\n file '%s'\n Alphabet size %u\n compression %d\n",
  95. argv[1], alphabet_size, compression);
  96. fflush (stdout);
  97. buffer = REGEX_TEST_combine (regexes, alphabet_size);
  98. GNUNET_asprintf (&regex, "GNUNET_REGEX_PROFILER_(%s)(0|1)*", buffer);
  99. size = strlen (regex);
  100. fprintf (stderr,
  101. "Combined regex (%ld bytes):\n%s\n",
  102. size,
  103. regex);
  104. dfa = REGEX_INTERNAL_construct_dfa (regex, size, compression);
  105. printf ("********* ALL EDGES *********'\n");
  106. REGEX_INTERNAL_iterate_all_edges (dfa, &print_edge, NULL);
  107. printf ("\n\n********* REACHABLE EDGES *********'\n");
  108. REGEX_INTERNAL_iterate_reachable_edges (dfa, &print_edge, NULL);
  109. REGEX_INTERNAL_automaton_destroy (dfa);
  110. GNUNET_free (buffer);
  111. REGEX_TEST_free_from_file (regexes);
  112. GNUNET_free (regex);
  113. return 0;
  114. }
  115. /* end of prof-regex.c */