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