first batch of license fixes (boring)
[oweals/gnunet.git] / src / peerinfo / perf_peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2009, 2010, 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file peerinfo/perf_peerinfo_api.c
18  * @brief testcase for peerinfo_api.c, hopefully hammer the peerinfo service,
19  * this performance test adds up to 5000 peers with one address each and checks
20  * over how many peers it can iterate before receiving a timeout after 5 seconds
21  * @author Nathan Evans
22  */
23
24 #include "platform.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "peerinfo.h"
30 #include <gauger.h>
31
32 #define NUM_REQUESTS 5000
33
34 static struct GNUNET_PEERINFO_IteratorContext *ic[NUM_REQUESTS];
35
36 static struct GNUNET_PEERINFO_Handle *h;
37
38 static unsigned int numpeers;
39
40 static struct GNUNET_PeerIdentity pid;
41
42 static struct GNUNET_SCHEDULER_Task *tt;
43
44
45 static void
46 do_shutdown (void *cls)
47 {
48   if (NULL != tt)
49   {
50     GNUNET_SCHEDULER_cancel (tt);
51     tt = NULL;
52   }
53   for (unsigned int i = 0; i < NUM_REQUESTS; i++)
54     if (NULL != ic[i])
55       GNUNET_PEERINFO_iterate_cancel (ic[i]);
56   GNUNET_PEERINFO_disconnect (h);
57   h = NULL;
58 }
59
60
61 static void
62 do_timeout (void *cls)
63 {
64   tt = NULL;
65   GNUNET_SCHEDULER_shutdown ();
66 }
67
68
69 static int
70 check_it (void *cls,
71           const struct GNUNET_HELLO_Address *address,
72           struct GNUNET_TIME_Absolute expiration)
73 {
74   return GNUNET_OK;
75 }
76
77
78 static ssize_t
79 address_generator (void *cls, size_t max, void *buf)
80 {
81   size_t *agc = cls;
82   ssize_t ret;
83   char *caddress;
84   struct GNUNET_HELLO_Address address;
85
86   if (*agc == 0)
87     return GNUNET_SYSERR; /* Done */
88
89   GNUNET_asprintf (&caddress, "Address%d", *agc);
90   address.peer = pid;
91   address.address_length = strlen (caddress) + 1;
92   address.address = caddress;
93   address.transport_name = "peerinfotest";
94   ret =
95       GNUNET_HELLO_add_address (&address,
96                                 GNUNET_TIME_relative_to_absolute
97                                 (GNUNET_TIME_UNIT_HOURS), buf, max);
98   GNUNET_free (caddress);
99   *agc = 0;
100   return ret;
101 }
102
103
104 static void
105 add_peer (size_t i)
106 {
107   struct GNUNET_HELLO_Message *h2;
108
109   memset (&pid, i, sizeof (pid));
110   h2 = GNUNET_HELLO_create (&pid.public_key,
111                             &address_generator,
112                             &i,
113                             GNUNET_NO);
114   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
115   GNUNET_free (h2);
116 }
117
118
119 static void
120 process (void *cls,
121          const struct GNUNET_PeerIdentity *peer,
122          const struct GNUNET_HELLO_Message *hello,
123          const char *err_msg)
124 {
125   struct GNUNET_PEERINFO_IteratorContext **icp = cls;
126
127   if (NULL == peer)
128   {
129     *icp = NULL;
130     return;
131   }
132   numpeers++;
133   if (0 && (NULL != hello) )
134     GNUNET_HELLO_iterate_addresses (hello,
135                                     GNUNET_NO,
136                                     &check_it,
137                                     NULL);
138 }
139
140
141 static void
142 run (void *cls,
143      const struct GNUNET_CONFIGURATION_Handle *cfg,
144      struct GNUNET_TESTING_Peer *peer)
145 {
146   h = GNUNET_PEERINFO_connect (cfg);
147   GNUNET_assert (h != NULL);
148   for (unsigned int i = 0; i < NUM_REQUESTS; i++)
149   {
150     add_peer (i);
151     ic[i] = GNUNET_PEERINFO_iterate (h,
152                                      GNUNET_YES,
153                                      NULL,
154                                      &process,
155                                      &ic[i]);
156   }
157   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
158                                                                     5),
159                                      &do_timeout,
160                                      NULL);
161   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
162                                  NULL);
163 }
164
165
166 int
167 main (int argc,
168       char *argv[])
169 {
170   if (0 != GNUNET_TESTING_service_run ("perf-gnunet-peerinfo",
171                                        "peerinfo",
172                                        "test_peerinfo_api_data.conf",
173                                        &run, NULL))
174     return 1;
175   FPRINTF (stderr,
176            "Received %u/%u calls before timeout\n",
177            numpeers,
178            NUM_REQUESTS * NUM_REQUESTS / 2);
179   GAUGER ("PEERINFO",
180           "Peerinfo lookups",
181           numpeers / 5,
182           "peers/s");
183   return 0;
184 }
185
186 /* end of perf_peerinfo_api.c */