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_NO
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
42 #define DEBUG_MEASUREMENT GNUNET_NO
43 #define DEBUG_CONNECTIONS GNUNET_NO
46 * Note that this value must not significantly exceed
47 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48 * messages may be dropped even for a reliable transport.
50 #define TOTAL_MSGS (10000 * 2)
52 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
53 #define MEASUREMENT_MSG_SIZE 1024
54 #define MEASUREMENT_MSG_SIZE_BIG 32768
55 #define MEASUREMENT_MAX_QUOTA 1024*1024*1024
56 #define MEASUREMENT_MIN_QUOTA 1024
61 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
68 struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_TRANSPORT_Handle *th;
70 struct GNUNET_PeerIdentity id;
76 static struct PeerContext p1;
78 static struct PeerContext p2;
80 static struct GNUNET_SCHEDULER_Handle *sched;
86 static unsigned long long total_bytes;
87 static unsigned long long current_quota_p1;
88 static unsigned long long current_quota_p2;
90 static struct GNUNET_TIME_Absolute start_time;
92 static GNUNET_SCHEDULER_TaskIdentifier die_task;
93 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
94 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
96 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
99 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
101 #define OKPP do { ok++; } while (0)
108 GNUNET_SCHEDULER_cancel (sched, die_task);
109 die_task = GNUNET_SCHEDULER_NO_TASK;
111 if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
113 GNUNET_SCHEDULER_cancel (sched, measurement_task);
114 measurement_task = GNUNET_SCHEDULER_NO_TASK;
116 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
118 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
119 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
121 GNUNET_SCHEDULER_shutdown (sched);
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
125 GNUNET_TRANSPORT_disconnect (p1.th);
126 GNUNET_TRANSPORT_disconnect (p2.th);
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Transports disconnected, returning success!\n");
131 GNUNET_SCHEDULER_shutdown (sched);
137 stop_arm (struct PeerContext *p)
140 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
141 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
142 GNUNET_OS_process_wait (p->arm_pid);
144 GNUNET_CONFIGURATION_destroy (p->cfg);
149 end_badly (void *cls,
150 const struct GNUNET_SCHEDULER_TaskContext *tc)
152 if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
154 GNUNET_SCHEDULER_cancel (sched, measurement_task);
155 measurement_task = GNUNET_SCHEDULER_NO_TASK;
157 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
159 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
160 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
164 GNUNET_TRANSPORT_disconnect (p1.th);
166 GNUNET_TRANSPORT_disconnect (p2.th);
172 struct GNUNET_MessageHeader header;
177 get_size_new (unsigned int iter)
179 return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
183 notify_receive_new (void *cls,
184 const struct GNUNET_PeerIdentity *peer,
185 const struct GNUNET_MessageHeader *message,
186 struct GNUNET_TIME_Relative latency,
191 const struct TestMessage *hdr;
193 hdr = (const struct TestMessage*) message;
194 s = get_size_new (n);
195 if (MTYPE != ntohs (message->type))
197 #if DEBUG_MEASUREMENT
198 if (ntohl(hdr->num) % 5000 == 0)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Got message %u of size %u\n",
203 ntohs (message->size));
210 notify_ready_new (void *cls, size_t size, void *buf)
214 struct TestMessage hdr;
218 transmit_handle = NULL;
220 if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
230 s = get_size_new (n);
231 GNUNET_assert (size >= s);
232 GNUNET_assert (buf != NULL);
236 hdr.header.size = htons (s);
237 hdr.header.type = htons (MTYPE);
239 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
240 ret += sizeof (struct TestMessage);
241 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
242 ret += s - sizeof (struct TestMessage);
243 #if DEBUG_MEASUREMENT
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Sending message %u\n",n);
251 s = get_size_new (n);
252 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
253 break; /* sometimes pack buffer full, sometimes not */
255 while (size - ret >= s);
256 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
265 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
267 static void measurement_counter
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
273 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
279 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
280 GNUNET_TIME_UNIT_SECONDS,
281 &measurement_counter,
286 measurement_end (void *cls,
287 const struct GNUNET_SCHEDULER_TaskContext *tc)
289 measurement_task = GNUNET_SCHEDULER_NO_TASK;
290 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
293 struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
295 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
297 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
298 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
301 if (transmit_handle != NULL)
303 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
304 transmit_handle = NULL;
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "\n Measurement finished: \n Quota allowed: %llu kb/s\n Throughput: %llu kb/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.value / 1000)/1024));
309 if (current_quota_p1 < total_bytes/(duration.value / 1000))
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 "\nQuota compliance failed: \n"\
313 "Quota allowed: %llu kb/s\n"\
314 "Throughput : %llu kb/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.value / 1000)/1024));
323 if (current_quota_p1 < (MEASUREMENT_MIN_QUOTA))
326 measure (current_quota_p1/100, current_quota_p2/100);
329 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
331 current_quota_p1 = quota_p1;
332 current_quota_p2 = quota_p2;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Starting measurement: Duration: %u Quota: %u\n", MEASUREMENT_INTERVALL, current_quota_p1);
337 GNUNET_TRANSPORT_set_quota (p1.th,
339 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
340 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
341 GNUNET_TIME_UNIT_FOREVER_REL,
343 GNUNET_TRANSPORT_set_quota (p2.th,
345 GNUNET_BANDWIDTH_value_init (current_quota_p2),
346 GNUNET_BANDWIDTH_value_init (current_quota_p2),
347 GNUNET_TIME_UNIT_FOREVER_REL,
350 if (transmit_handle != NULL)
351 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
353 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
355 get_size_new (0), 0, TIMEOUT,
359 GNUNET_SCHEDULER_cancel (sched, die_task);
360 die_task = GNUNET_SCHEDULER_add_delayed (sched,
364 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
365 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
366 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
367 GNUNET_TIME_UNIT_SECONDS,
368 &measurement_counter,
370 measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
371 MEASUREMENT_INTERVALL,
375 start_time = GNUNET_TIME_absolute_get ();
379 notify_connect (void *cls,
380 const struct GNUNET_PeerIdentity *peer,
381 struct GNUNET_TIME_Relative latency,
386 #if DEBUG_CONNECTIONS
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
394 #if DEBUG_CONNECTIONS
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
402 measure(MEASUREMENT_MAX_QUOTA,MEASUREMENT_MAX_QUOTA);
408 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
410 #if DEBUG_CONNECTIONS
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Peer `%4s' disconnected (%p)!\n",
413 GNUNET_i2s (peer), cls);
419 setup_peer (struct PeerContext *p, const char *cfgname)
421 p->cfg = GNUNET_CONFIGURATION_create ();
423 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
424 "gnunet-service-arm",
425 "gnunet-service-arm",
429 "-c", cfgname, NULL);
432 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
433 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
438 GNUNET_assert (p->th != NULL);
443 exchange_hello_last (void *cls,
444 const struct GNUNET_MessageHeader *message)
446 struct PeerContext *me = cls;
448 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
450 GNUNET_assert (ok >= 3);
452 GNUNET_assert (message != NULL);
453 GNUNET_assert (GNUNET_OK ==
454 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
456 /* both HELLOs exchanged, get ready to test transmission! */
461 exchange_hello (void *cls,
462 const struct GNUNET_MessageHeader *message)
464 struct PeerContext *me = cls;
466 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
467 GNUNET_assert (ok >= 2);
469 GNUNET_assert (message != NULL);
470 GNUNET_assert (GNUNET_OK ==
471 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
473 GNUNET_TRANSPORT_offer_hello (p2.th, message);
474 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
479 struct GNUNET_SCHEDULER_Handle *s,
481 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
483 GNUNET_assert (ok == 1);
487 die_task = GNUNET_SCHEDULER_add_delayed (sched,
492 /* Setting initial quota for both peers */
493 // current_quota_p1 = 1024 * 1024 * 1024;
494 // current_quota_p2 = 1024 * 1024 * 1024;
496 setup_peer (&p1, "test_quota_compliance_peer1.conf");
497 setup_peer (&p2, "test_quota_compliance_peer2.conf");
499 GNUNET_assert(p1.th != NULL);
500 GNUNET_assert(p2.th != NULL);
501 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
505 main (int argc, char *argv[])
509 return GNUNET_SYSERR;
511 GNUNET_log_setup ("test-quota-compliance",
518 char *const argv1[] = { "test-quota-compliance",
520 "test_quota_compliance_data.conf",
526 struct GNUNET_GETOPT_CommandLineOption options[] = {
527 GNUNET_GETOPT_OPTION_END
531 GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
532 argv1, "test-quota-compliance", "nohelp",
537 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
538 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
542 /* end of test_quota_compliance.c */