More API function tests...
[oweals/gnunet.git] / src / peerinfo / perf_peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file peerinfo/perf_peerinfo_api.c
23  * @brief testcase for peerinfo_api.c, hopefully hammer the peerinfo service,
24  * this performance test adds up to 5000 peers with one address each and checks
25  * over how many peers it can iterate before receiving a timeout after 30 seconds
26  * @author Nathan Evans
27  */
28
29 #include "platform.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "peerinfo.h"
35 #include <gauger.h>
36
37 #define START_SERVICE 1
38
39 #define NUM_REQUESTS 5000
40
41 static struct GNUNET_PEERINFO_IteratorContext *ic[NUM_REQUESTS];
42
43 static struct GNUNET_PEERINFO_Handle *h;
44
45 static unsigned int numpeers;
46
47 static struct GNUNET_PeerIdentity pid;
48
49
50 static int
51 check_it (void *cls, const struct GNUNET_HELLO_Address *address,
52           struct GNUNET_TIME_Absolute expiration)
53 {
54   return GNUNET_OK;
55 }
56
57
58 static ssize_t
59 address_generator (void *cls, size_t max, void *buf)
60 {
61   size_t *agc = cls;
62   ssize_t ret;
63   char *caddress;
64   struct GNUNET_HELLO_Address address;
65
66   if (*agc == 0)
67     return GNUNET_SYSERR; /* Done */
68
69   GNUNET_asprintf (&caddress, "Address%d", *agc);
70   address.peer = pid;
71   address.address_length = strlen (caddress) + 1;
72   address.address = caddress;
73   address.transport_name = "peerinfotest";
74   ret =
75       GNUNET_HELLO_add_address (&address,
76                                 GNUNET_TIME_relative_to_absolute
77                                 (GNUNET_TIME_UNIT_HOURS), buf, max);
78   GNUNET_free (caddress);
79   *agc = 0;
80   return ret;
81 }
82
83
84 static void
85 add_peer (size_t i)
86 {
87   struct GNUNET_HELLO_Message *h2;
88
89   memset (&pid, i, sizeof (pid));
90   h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &i, GNUNET_NO);
91   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
92   GNUNET_free (h2);
93 }
94
95
96 static void
97 process (void *cls, const struct GNUNET_PeerIdentity *peer,
98          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
99 {
100   if (NULL != peer)
101   {
102     numpeers++;
103     if (0 && (hello != NULL))
104       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, NULL);
105
106   }
107 }
108
109
110 static void
111 run (void *cls,
112      const struct GNUNET_CONFIGURATION_Handle *cfg,
113      struct GNUNET_TESTING_Peer *peer)
114 {
115   size_t i;
116
117   h = GNUNET_PEERINFO_connect (cfg);
118   GNUNET_assert (h != NULL);
119   for (i = 0; i < NUM_REQUESTS; i++)
120   {
121     add_peer (i);
122     ic[i] =
123         GNUNET_PEERINFO_iterate (h, GNUNET_YES, NULL,
124                                  GNUNET_TIME_relative_multiply
125                                  (GNUNET_TIME_UNIT_SECONDS, 30), &process, cls);
126   }
127 }
128
129
130 int
131 main (int argc, char *argv[])
132 {
133   if (0 != GNUNET_TESTING_service_run ("perf-gnunet-peerinfo",
134                                        "peerinfo",
135                                        "test_peerinfo_api_data.conf",
136                                        &run, NULL))
137     return 1;
138   FPRINTF (stderr, "Received %u/%u calls before timeout\n", numpeers,
139            NUM_REQUESTS * NUM_REQUESTS / 2);
140   GAUGER ("PEERINFO", "Peerinfo lookups", numpeers / 30, "peers/s");
141   return 0;
142 }
143
144 /* end of perf_peerinfo_api.c */