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