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