2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_quota_compliance.c
22 * @brief base test case for transport implementations
24 * This test case tests quota compliance both on core and transport level
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
37 #define VERBOSE GNUNET_YES
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
44 * Note that this value must not significantly exceed
45 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
46 * messages may be dropped even for a reliable transport.
48 #define TOTAL_MSGS (10000 * 2)
50 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
51 #define MEASUREMENT_MSG_SIZE 1024
52 #define MEASUREMENT_MSG_SIZE_BIG 32768
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
64 struct GNUNET_CONFIGURATION_Handle *cfg;
65 struct GNUNET_TRANSPORT_Handle *th;
66 struct GNUNET_PeerIdentity id;
72 static struct PeerContext p1;
74 static struct PeerContext p2;
76 static struct GNUNET_SCHEDULER_Handle *sched;
82 static unsigned long long total_bytes;
83 static unsigned long long current_quota_p1;
84 static unsigned long long current_quota_p2;
86 static struct GNUNET_TIME_Absolute start_time;
88 static GNUNET_SCHEDULER_TaskIdentifier die_task;
90 static int msg_scheduled;
92 static int msg_recv_expected;
97 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
99 #define OKPP do { ok++; } while (0)
106 unsigned long long delta;
108 GNUNET_SCHEDULER_cancel (sched, die_task);
109 die_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
113 GNUNET_TRANSPORT_disconnect (p1.th);
114 GNUNET_TRANSPORT_disconnect (p2.th);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Transports disconnected, returning success!\n");
119 delta = GNUNET_TIME_absolute_get_duration (start_time).value;
121 "\nThroughput was %llu kb/s\n",
122 total_bytes * 1000 / 1024 / delta);
130 stop_arm (struct PeerContext *p)
133 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
134 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
135 GNUNET_OS_process_wait (p->arm_pid);
137 GNUNET_CONFIGURATION_destroy (p->cfg);
142 end_badly (void *cls,
143 const struct GNUNET_SCHEDULER_TaskContext *tc)
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 GNUNET_TRANSPORT_disconnect (p1.th);
152 GNUNET_TRANSPORT_disconnect (p2.th);
159 struct GNUNET_MessageHeader header;
165 get_size (unsigned int iter)
170 return iter + sizeof (struct TestMessage);
171 ret = (iter * iter * iter);
172 return sizeof (struct TestMessage) + (ret % 60000);
176 get_size_new (unsigned int iter)
178 unsigned int ret = 1024;
183 notify_receive (void *cls,
184 const struct GNUNET_PeerIdentity *peer,
185 const struct GNUNET_MessageHeader *message,
186 struct GNUNET_TIME_Relative latency,
191 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
192 const struct TestMessage *hdr;
194 hdr = (const struct TestMessage*) message;
196 if (MTYPE != ntohs (message->type))
198 msg_recv_expected = n;
199 msg_recv = ntohl(hdr->num);
200 if (ntohs (message->size) != s)
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 "Expected message %u of size %u, got %u bytes of message %u\n",
205 ntohs (message->size),
207 GNUNET_SCHEDULER_cancel (sched, die_task);
208 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
211 if (ntohl (hdr->num) != n)
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 "Expected message %u of size %u, got %u bytes of message %u\n",
216 ntohs (message->size),
218 GNUNET_SCHEDULER_cancel (sched, die_task);
219 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
222 memset (cbuf, n, s - sizeof (struct TestMessage));
223 if (0 != memcmp (cbuf,
225 s - sizeof (struct TestMessage)))
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 "Expected message %u with bits %u, but body did not match\n",
229 n, (unsigned char) n);
230 GNUNET_SCHEDULER_cancel (sched, die_task);
231 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
235 if (ntohl(hdr->num) % 5000 == 0)
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Got message %u of size %u\n",
240 ntohs (message->size));
244 if (0 == (n % (TOTAL_MSGS/100)))
246 fprintf (stderr, ".");
247 GNUNET_SCHEDULER_cancel (sched, die_task);
248 die_task = GNUNET_SCHEDULER_add_delayed (sched,
259 notify_ready (void *cls, size_t size, void *buf)
263 struct TestMessage hdr;
275 GNUNET_assert (size >= s);
276 GNUNET_assert (buf != NULL);
280 hdr.header.size = htons (s);
281 hdr.header.type = htons (MTYPE);
284 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
285 ret += sizeof (struct TestMessage);
286 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
287 ret += s - sizeof (struct TestMessage);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Sending message %u of size %u\n",
299 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
300 break; /* sometimes pack buffer full, sometimes not */
302 while (size - ret >= s);
305 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Returning total message block of size %u\n",
322 static void measure ()
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Both peers are connected, starting measurement...\n");
328 GNUNET_TRANSPORT_set_quota (p1.th,
330 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
331 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
332 GNUNET_TIME_UNIT_FOREVER_REL,
334 GNUNET_TRANSPORT_set_quota (p2.th,
336 GNUNET_BANDWIDTH_value_init (current_quota_p2),
337 GNUNET_BANDWIDTH_value_init (current_quota_p2),
338 GNUNET_TIME_UNIT_FOREVER_REL,
340 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
342 get_size (0), 0, TIMEOUT,
345 start_time = GNUNET_TIME_absolute_get ();
350 notify_connect (void *cls,
351 const struct GNUNET_PeerIdentity *peer,
352 struct GNUNET_TIME_Relative latency,
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
375 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Peer `%4s' disconnected (%p)!\n",
380 GNUNET_i2s (peer), cls);
386 setup_peer (struct PeerContext *p, const char *cfgname)
388 p->cfg = GNUNET_CONFIGURATION_create ();
390 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
391 "gnunet-service-arm",
392 "gnunet-service-arm",
396 "-c", cfgname, NULL);
399 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
400 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
405 GNUNET_assert (p->th != NULL);
410 exchange_hello_last (void *cls,
411 const struct GNUNET_MessageHeader *message)
413 struct PeerContext *me = cls;
415 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
417 GNUNET_assert (ok >= 3);
419 GNUNET_assert (message != NULL);
420 GNUNET_assert (GNUNET_OK ==
421 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
423 /* both HELLOs exchanged, get ready to test transmission! */
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Finished exchanging HELLOs, now waiting for peers to connect!\n");
430 exchange_hello (void *cls,
431 const struct GNUNET_MessageHeader *message)
433 struct PeerContext *me = cls;
435 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
436 GNUNET_assert (ok >= 2);
438 GNUNET_assert (message != NULL);
439 GNUNET_assert (GNUNET_OK ==
440 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
442 GNUNET_TRANSPORT_offer_hello (p2.th, message);
443 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
448 struct GNUNET_SCHEDULER_Handle *s,
450 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
452 GNUNET_assert (ok == 1);
456 die_task = GNUNET_SCHEDULER_add_delayed (sched,
461 /* Setting initial quota for both peers */
462 current_quota_p1 = 1024 * 1024 * 1024;
463 current_quota_p2 = 1024 * 1024 * 1024;
465 setup_peer (&p1, "test_quota_compliance_peer1.conf");
466 setup_peer (&p2, "test_quota_compliance_peer2.conf");
468 GNUNET_assert(p1.th != NULL);
469 GNUNET_assert(p2.th != NULL);
470 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
474 main (int argc, char *argv[])
478 return GNUNET_SYSERR;
480 GNUNET_log_setup ("test-quota-compliance",
487 char *const argv1[] = { "test-quota-compliance",
489 "test_quota_compliance_data.conf",
495 struct GNUNET_GETOPT_CommandLineOption options[] = {
496 GNUNET_GETOPT_OPTION_END
500 GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
501 argv1, "test-quota-compliance", "nohelp",
507 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
508 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
512 /* end of test_quota_compliance.c */