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