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