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