Consistent GAUGER naming.
[oweals/gnunet.git] / src / peerinfo / perf_peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2009, 2010 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 peerinfo/test_peerinfo_hammer.c
23  * @brief testcase for peerinfo_api.c, hopefully hammer the peerinfo service
24  * @author Nathan Evans
25  */
26
27 #include "platform.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_time_lib.h"
34 #include "peerinfo.h"
35 #include <gauger.h>
36
37 #define START_SERVICE 1
38
39 #define NUM_REQUESTS 5000
40
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 static struct GNUNET_PEERINFO_IteratorContext *ic[NUM_REQUESTS];
44
45 static struct GNUNET_PEERINFO_Handle *h;
46
47 static unsigned int numpeers;
48
49 static int
50 check_it (void *cls,
51           const char *tname,
52           struct GNUNET_TIME_Absolute expiration,
53           const void *addr, uint16_t addrlen)
54 {
55 #if DEBUG
56   if (addrlen > 0)
57     {
58       fprintf (stderr,
59                "name: %s, addr: %s\n", 
60                tname, 
61                (const char*) addr);
62     }
63 #endif
64   return GNUNET_OK;
65 }
66
67
68 static size_t
69 address_generator (void *cls, size_t max, void *buf)
70 {
71   size_t *agc = cls;
72   size_t ret;
73   char *address;
74
75   if (*agc == 0)
76     return 0;
77
78   GNUNET_asprintf(&address, "Address%d", *agc);
79
80   ret = GNUNET_HELLO_add_address ("peerinfotest",
81                                   GNUNET_TIME_relative_to_absolute
82                                   (GNUNET_TIME_UNIT_HOURS), 
83                                   address, strlen(address) + 1,
84                                   buf, max);
85   GNUNET_free (address);
86   *agc = 0;
87   return ret;
88 }
89
90
91 static void
92 add_peer (size_t i)
93 {
94   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
95   struct GNUNET_PeerIdentity pid;
96   struct GNUNET_HELLO_Message *h2;
97   size_t agc;
98
99   agc = 2;
100   memset (&pkey, i, sizeof (pkey));
101   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
102   h2 = GNUNET_HELLO_create (&pkey, &address_generator, &i);
103   GNUNET_PEERINFO_add_peer (h, h2);
104   GNUNET_free (h2);
105 }
106
107
108 static void
109 process (void *cls,
110          const struct GNUNET_PeerIdentity *peer,
111          const struct GNUNET_HELLO_Message *hello,
112          const char * err_msg)
113 {
114   if (peer == NULL)
115     {
116 #if DEBUG
117       fprintf(stderr, "Process received NULL response\n");
118 #endif
119     }
120   else
121     {
122 #if DEBUG
123       fprintf(stderr, "Processed a peer\n");
124 #endif
125       numpeers++;
126       if (0 && (hello != NULL))
127         GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, NULL);
128
129     }
130 }
131
132
133 static void
134 run (void *cls,
135      char *const *args,
136      const char *cfgfile, 
137      const struct GNUNET_CONFIGURATION_Handle *c)
138 {
139   size_t i;
140   cfg = c;
141   h = GNUNET_PEERINFO_connect (cfg);
142   GNUNET_assert (h != NULL);
143   for (i = 0; i < NUM_REQUESTS; i++)
144     {
145       add_peer (i);
146       ic[i] = GNUNET_PEERINFO_iterate (h,
147                                        NULL,
148                                        GNUNET_TIME_relative_multiply
149                                        (GNUNET_TIME_UNIT_SECONDS, 30),
150                                        &process, cls);
151     }
152 }
153
154 static int
155 check ()
156 {
157   int ok = 0;
158   char *const argv[] = { "perf-peerinfo-api",
159     "-c",
160     "test_peerinfo_api_data.conf",
161 #if DEBUG_PEERINFO
162     "-L", "DEBUG",
163 #else
164     "-L", "ERROR",
165 #endif
166     NULL
167   };
168 #if START_SERVICE
169   struct GNUNET_OS_Process *proc;
170   struct GNUNET_GETOPT_CommandLineOption options[] = {
171     GNUNET_GETOPT_OPTION_END
172   };
173   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-peerinfo",
174                                  "gnunet-service-peerinfo",
175 #if DEBUG_PEERINFO
176                                  "-L", "DEBUG",
177 #else
178                                 "-L", "ERROR",
179 #endif
180                                  "-c", "test_peerinfo_api_data.conf", NULL);
181 #endif
182   GNUNET_assert (NULL != proc);
183   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
184                       argv, "perf-peerinfo-api", "nohelp",
185                       options, &run, &ok);
186   fprintf (stderr,
187            "Received %u/%u calls before timeout\n",
188            numpeers,
189            NUM_REQUESTS * NUM_REQUESTS / 2);
190   GAUGER ("PEERINFO:Peerinfo lookups", numpeers / 30, "peers/s");
191 #if START_SERVICE
192   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
193     {
194       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
195       ok = 1;
196     }
197   GNUNET_OS_process_wait (proc);
198   GNUNET_OS_process_close (proc);
199   proc = NULL;
200
201 #endif
202   return ok;
203 }
204
205
206 int
207 main (int argc, char *argv[])
208 {
209   int ret = 0;
210
211   GNUNET_log_setup ("perf_peerinfo_api",
212 #if DEBUG_PEERINFO
213                     "DEBUG",
214 #else
215                     "ERROR",
216 #endif
217                     NULL);
218   ret = check ();
219   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-peerinfo");
220   return ret;
221 }
222
223 /* end of perf_peerinfo_api.c */