glitch in the license text detected by hyazinthe, thank you!
[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
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
28 /**
29  * Print information about the given node and its edges
30  * to stdout.
31  *
32  * @param cls closure, unused.
33  * @param key hash for current state.
34  * @param proof proof for current state.
35  * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
36  * @param num_edges number of edges leaving current state.
37  * @param edges edges leaving current state.
38  */
39 static void
40 print_edge (void *cls,
41             const struct GNUNET_HashCode *key,
42             const char *proof,
43             int accepting,
44             unsigned int num_edges,
45             const struct REGEX_BLOCK_Edge *edges)
46 {
47   unsigned int i;
48
49   printf ("%s: %s, proof: `%s'\n",
50           GNUNET_h2s (key),
51           accepting ? "ACCEPTING" : "",
52           proof);
53   for (i = 0; i < num_edges; i++)
54     printf ("    `%s': %s\n",
55             edges[i].label,
56             GNUNET_h2s (&edges[i].destination));
57 }
58
59
60 /**
61  * The main function of the regex performace test.
62  *
63  * Read a set of regex from a file, combine them and create a DFA from the
64  * resulting combined regex.
65  *
66  * @param argc number of arguments from the command line
67  * @param argv command line arguments
68  * @return 0 ok, 1 on error
69  */
70 int
71 main (int argc, char *const *argv)
72 {
73   struct REGEX_INTERNAL_Automaton* dfa;
74   char **regexes;
75   char *buffer;
76   char *regex;
77   int compression;
78   unsigned int alphabet_size;
79   long size;
80
81   GNUNET_log_setup ("perf-regex", "DEBUG", NULL);
82   if (4 != argc)
83   {
84     fprintf (stderr,
85              "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n",
86              argv[0]);
87     return 1;
88   }
89   regexes = REGEX_TEST_read_from_file (argv[1]);
90   if (NULL == regexes)
91   {
92     fprintf (stderr,
93              "Failed to read regexes from `%s'\n",
94              argv[1]);
95     return 2;
96   }
97   alphabet_size = atoi (argv[2]);
98   compression = atoi (argv[3]);
99   printf ("********* PERF-REGEX *********'\n");
100   printf ("Using:\n file '%s'\n Alphabet size %u\n compression %d\n",
101           argv[1], alphabet_size, compression);
102   fflush(stdout);
103   buffer = REGEX_TEST_combine (regexes, alphabet_size);
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 */