documentation for new parameters
[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 struct GNUNET_SCHEDULER_Handle *sched;
41
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 static struct GNUNET_PEERINFO_IteratorContext *ic[NUM_REQUESTS];
45
46 static struct GNUNET_PEERINFO_Handle *h;
47
48 static unsigned int numpeers;
49
50 static int
51 check_it (void *cls,
52           const char *tname,
53           struct GNUNET_TIME_Absolute expiration,
54           const void *addr, uint16_t addrlen)
55 {
56 #if DEBUG
57   if (addrlen > 0)
58     {
59       fprintf (stderr,
60                "name: %s, addr: %s\n", 
61                tname, 
62                (const char*) addr);
63     }
64 #endif
65   return GNUNET_OK;
66 }
67
68
69 static size_t
70 address_generator (void *cls, size_t max, void *buf)
71 {
72   size_t *agc = cls;
73   size_t ret;
74   char *address;
75
76   if (*agc == 0)
77     return 0;
78
79   GNUNET_asprintf(&address, "Address%d", *agc);
80
81   ret = GNUNET_HELLO_add_address ("peerinfotest",
82                                   GNUNET_TIME_relative_to_absolute
83                                   (GNUNET_TIME_UNIT_HOURS), 
84                                   address, strlen(address) + 1,
85                                   buf, max);
86   GNUNET_free (address);
87   *agc = 0;
88   return ret;
89 }
90
91
92 static void
93 add_peer (size_t i)
94 {
95   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
96   struct GNUNET_PeerIdentity pid;
97   struct GNUNET_HELLO_Message *h2;
98   size_t agc;
99
100   agc = 2;
101   memset (&pkey, i, sizeof (pkey));
102   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
103   h2 = GNUNET_HELLO_create (&pkey, &address_generator, &i);
104   GNUNET_PEERINFO_add_peer (h, h2);
105   GNUNET_free (h2);
106 }
107
108
109 static void
110 process (void *cls,
111          const struct GNUNET_PeerIdentity *peer,
112          const struct GNUNET_HELLO_Message *hello)
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      struct GNUNET_SCHEDULER_Handle *s,
136      char *const *args,
137      const char *cfgfile, 
138      const struct GNUNET_CONFIGURATION_Handle *c)
139 {
140   size_t i;
141   sched = s;
142   cfg = c;
143   h = GNUNET_PEERINFO_connect (sched, cfg);
144   GNUNET_assert (h != NULL);
145   for (i = 0; i < NUM_REQUESTS; i++)
146     {
147       add_peer (i);
148       ic[i] = GNUNET_PEERINFO_iterate (h,
149                                        NULL,
150                                        GNUNET_TIME_relative_multiply
151                                        (GNUNET_TIME_UNIT_SECONDS, 30),
152                                        &process, cls);
153     }
154 }
155
156 static int
157 check ()
158 {
159   int ok = 0;
160   char *const argv[] = { "test-peerinfo-hammer",
161     "-c",
162     "test_peerinfo_api_data.conf",
163 #if DEBUG_PEERINFO
164     "-L", "DEBUG",
165 #endif
166     NULL
167   };
168 #if START_SERVICE
169   pid_t pid;
170   struct GNUNET_GETOPT_CommandLineOption options[] = {
171     GNUNET_GETOPT_OPTION_END
172   };
173   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-peerinfo",
174                                  "gnunet-service-peerinfo",
175 #if DEBUG_PEERINFO
176                                  "-L", "DEBUG",
177 #endif
178                                  "-c", "test_peerinfo_api_data.conf", NULL);
179 #endif
180   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
181                       argv, "test-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 != PLIBC_KILL (pid, SIGTERM))
189     {
190       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
191       ok = 1;
192     }
193   GNUNET_OS_process_wait(pid);
194 #endif
195   return ok;
196 }
197
198
199 int
200 main (int argc, char *argv[])
201 {
202   int ret = 0;
203
204   GNUNET_log_setup ("test_peerinfo_api",
205 #if DEBUG_PEERINFO
206                     "DEBUG",
207 #else
208                     "ERROR",
209 #endif
210                     NULL);
211   ret = check ();
212   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-peerinfo");
213   return ret;
214 }
215
216 /* end of test_peerinfo_hammer.c */