more fixes for #2570
[oweals/gnunet.git] / src / stream / perf_stream_api.c
1  /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 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 stream/perf_stream_api.c
23  * @brief performance benchmarks for stream api
24  * @author Sree Harsha Totakura
25  */
26
27 #define LOG(kind, ...)                         \
28   GNUNET_log (kind, __VA_ARGS__);
29
30 /****************************************************************************************/
31 /* Test is setup into the following major steps:                                        */
32 /*    1. Measurements over loopback (1 hop). i.e. we use only one peer and open         */
33 /*       stream connections over loopback. Messages will go through                     */
34 /*       STREAM_API->MESH_API->MESH_SERVICE->MESH_API->STREAM_API.                      */
35 /*    2. Measurements over 2 peers (2 hops). We use testbed to create 2 peers,          */
36 /*       connect them and then create stream connections. Messages will go through      */
37 /*       STREAM_API->MESH_API->MESH_SERVICE->CORE1.....CORE2->MESH_API->STREAM_API      */
38 /*    3. Measurements over 3 peers (3 hops). We use testbed to create 3 peers,          */
39 /*       connect them in a line topology: peer1->peer2->peer3. Messages will go         */
40 /*       through                                                                        */
41 /*       STREAM_API->MESH_API->MESH_SERVICE->CORE1..CORE2..CORE3->MESH_API->STREAM_API. */
42 /****************************************************************************************/
43
44 #include "platform.h"
45 #include "gnunet_common.h"
46 #include "gnunet_util_lib.h"
47 #include "gnunet_testing_lib.h"
48 #include "gnunet_testbed_service.h"
49 #include "gnunet_stream_lib.h"
50
51
52   
53 /**
54  * Simple struct to keep track of progress, and print a
55  * nice little percentage meter for long running tasks.
56  */
57 struct ProgressMeter
58 {
59   unsigned int total;
60
61   unsigned int modnum;
62
63   unsigned int dotnum;
64
65   unsigned int completed;
66
67   int print;
68
69   char *startup_string;
70 };
71
72
73 /**
74  * Steps in testing
75  */
76 enum TestStep
77 {
78   /**
79    * Single hop loopback testing
80    */
81   TEST_STEP_1_HOP,
82
83   /**
84    * Testing with 2 peers
85    */
86   TEST_STEP_2_HOP,
87
88   /**
89    * Testing with 3 peers
90    */
91   TEST_STEP_3_HOP
92 };
93
94
95 /**
96  * Structure for holding peer's sockets and IO Handles
97  */
98 struct PeerData
99 {
100   /**
101    * Peer's stream socket
102    */
103   struct GNUNET_STREAM_Socket *socket;
104
105   /**
106    * Peer's io write handle
107    */
108   struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
109
110   /**
111    * Peer's io read handle
112    */
113   struct GNUNET_STREAM_IOReadHandle *io_read_handle;
114
115   /**
116    * The peer handle when we use the testbed servie
117    */
118   struct GNUNET_TESTBED_Peer *peer;
119
120   /**
121    * Handle to peer specific opearations while using testbed service
122    */
123   struct GNUNET_TESTBED_Operation *op;
124
125   /**
126    * The identity of this peer
127    */
128   struct GNUNET_PeerIdentity id;
129
130   /**
131    * Bytes the peer has written
132    */
133   unsigned int bytes_wrote;
134
135   /**
136    * Byte the peer has read
137    */
138   unsigned int bytes_read;
139 };
140
141
142 /**
143  * Enumeration of stages in this testing
144  */
145 enum TestStage
146 {
147   /**
148    * The initial stage
149    */
150   INIT,
151   
152   /**
153    * Uplink testing stage
154    */
155   UPLINK_OK,
156
157   /**
158    * Downlink testing stage
159    */
160   DOWNLINK_OK
161 };
162
163
164 /**
165  * Maximum size of the data which we will transfer during tests
166  */
167 #define DATA_SIZE 5000000      /* 5mB */
168
169 /**
170  * Listen socket of peer2
171  */
172 struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
173
174 /**
175  * Handle to configuration during TEST_STEP_1_HOP
176  */
177 const struct GNUNET_CONFIGURATION_Handle *config;
178
179 /**
180  * Handle for the progress meter
181  */
182 static struct ProgressMeter *meter;
183
184 /**
185  * Placeholder for peer data
186  */
187 static struct PeerData peer_data[3];
188
189 /**
190  * Handle to common operations while using testbed
191  */
192 static struct GNUNET_TESTBED_Operation *common_op;
193
194 /**
195  * Task ID for abort task
196  */
197 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
198
199 /**
200  * Task ID for write task
201  */
202 static GNUNET_SCHEDULER_TaskIdentifier write_task;
203
204 /**
205  * Task ID for read task
206  */
207 static GNUNET_SCHEDULER_TaskIdentifier read_task;
208
209 /**
210  * Absolute time when profiling starts
211  */
212 static struct GNUNET_TIME_Absolute prof_start_time;
213
214 /**
215  * Test time taken for sending the data
216  */
217 static struct GNUNET_TIME_Relative prof_time;
218
219 /**
220  * Random data block. Should generate data first
221  */
222 static uint32_t data[DATA_SIZE / 4];
223
224 /**
225  * Payload sizes to test each major test with
226  */
227 static uint16_t payload_size[] = 
228 { 20, 500, 2000, 7000, 13000, 25000, 50000, 60000, 63000, 64000 };
229
230 /**
231  * Current step of testing
232  */
233 static enum TestStep test_step;
234
235 /**
236  * Index for choosing payload size
237  */
238 static unsigned int payload_size_index;
239
240 /**
241  * Number of peers we want to create while using the testbed service
242  */
243 static int num_peers;
244
245 /**
246  * Testing result of a major test
247  */
248 static enum TestStage result;
249
250 /**
251  * Create a meter to keep track of the progress of some task.
252  *
253  * @param total the total number of items to complete
254  * @param start_string a string to prefix the meter with (if printing)
255  * @param print GNUNET_YES to print the meter, GNUNET_NO to count
256  *              internally only
257  *
258  * @return the progress meter
259  */
260 static struct ProgressMeter *
261 create_meter (unsigned int total, char *start_string, int print)
262 {
263   struct ProgressMeter *ret;
264
265   ret = GNUNET_malloc (sizeof (struct ProgressMeter));
266   ret->print = print;
267   ret->total = total;
268   ret->modnum = total / 4;
269   if (ret->modnum == 0)         /* Divide by zero check */
270     ret->modnum = 1;
271   ret->dotnum = (total / 50) + 1;
272   if (start_string != NULL)
273     ret->startup_string = GNUNET_strdup (start_string);
274   else
275     ret->startup_string = GNUNET_strdup ("");
276
277   return ret;
278 }
279
280
281 /**
282  * Update progress meter (increment by one).
283  *
284  * @param meter the meter to update and print info for
285  *
286  * @return GNUNET_YES if called the total requested,
287  *         GNUNET_NO if more items expected
288  */
289 static int
290 update_meter (struct ProgressMeter *meter)
291 {
292   if (meter->print == GNUNET_YES)
293   {
294     if (meter->completed % meter->modnum == 0)
295     {
296       if (meter->completed == 0)
297       {
298         FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
299       }
300       else
301         FPRINTF (stdout, "%d%%",
302                  (int) (((float) meter->completed / meter->total) * 100));
303     }
304     else if (meter->completed % meter->dotnum == 0)
305       FPRINTF (stdout, "%s",  ".");
306
307     if (meter->completed + 1 == meter->total)
308       FPRINTF (stdout, "%d%%]\n", 100);
309     fflush (stdout);
310   }
311   meter->completed++;
312
313   if (meter->completed == meter->total)
314     return GNUNET_YES;
315   if (meter->completed > meter->total)
316     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
317   return GNUNET_NO;
318 }
319
320
321 /**
322  * Reset progress meter.
323  *
324  * @param meter the meter to reset
325  *
326  * @return GNUNET_YES if meter reset,
327  *         GNUNET_SYSERR on error
328  */
329 static int
330 reset_meter (struct ProgressMeter *meter)
331 {
332   if (meter == NULL)
333     return GNUNET_SYSERR;
334
335   meter->completed = 0;
336   return GNUNET_YES;
337 }
338
339
340 /**
341  * Release resources for meter
342  *
343  * @param meter the meter to free
344  */
345 static void
346 free_meter (struct ProgressMeter *meter)
347 {
348   GNUNET_free_non_null (meter->startup_string);
349   GNUNET_free (meter);
350 }
351
352
353 /**
354  * Shutdown nicely
355  */
356 static void
357 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
358 {
359   switch (test_step)
360   {
361   case TEST_STEP_1_HOP:
362     if (NULL != peer_data[0].socket)
363       GNUNET_STREAM_close (peer_data[0].socket);
364     if (NULL != peer_data[1].socket)
365       GNUNET_STREAM_close (peer_data[1].socket);
366     if (NULL != peer2_listen_socket)
367       GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
368     break;
369   case TEST_STEP_2_HOP:
370     if (NULL != peer_data[1].socket)
371       GNUNET_STREAM_close (peer_data[1].socket);
372     if (NULL != peer_data[0].op)
373       GNUNET_TESTBED_operation_done (peer_data[0].op);
374     if (NULL != peer_data[1].op)
375       GNUNET_TESTBED_operation_done (peer_data[1].op);
376     break;
377   case TEST_STEP_3_HOP:
378     GNUNET_break (0);    
379   }  
380   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
381     GNUNET_SCHEDULER_cancel (abort_task);
382   if (GNUNET_SCHEDULER_NO_TASK != write_task)
383     GNUNET_SCHEDULER_cancel (write_task);
384   GNUNET_SCHEDULER_shutdown (); /* Shutdown this testcase */
385   if (NULL != meter)
386   {
387     free_meter (meter);
388     meter = NULL;
389   }
390 }
391
392
393 /**
394  * Something went wrong and timed out. Kill everything and set error flag
395  */
396 static void
397 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
398 {
399   abort_task = GNUNET_SCHEDULER_NO_TASK;
400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
401   if (GNUNET_SCHEDULER_NO_TASK != read_task)
402     GNUNET_SCHEDULER_cancel (read_task);
403   result = GNUNET_SYSERR;
404   do_shutdown (cls, tc);
405 }
406
407   
408 /**
409  * Scheduler call back; to be executed when a new stream is connected
410  * Called from listen connect for peer2
411  */
412 static void
413 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
414
415
416 /**
417  * Task for calling STREAM_write with a chunk of random data
418  *
419  * @param cls the peer data entity
420  * @param tc the task context
421  */
422 static void
423 stream_write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
424
425
426 /**
427  * The write completion function; called upon writing some data to stream or
428  * upon error
429  *
430  * @param cls the closure from GNUNET_STREAM_write/read
431  * @param status the status of the stream at the time this function is called
432  * @param size the number of bytes read or written
433  */
434 static void 
435 write_completion (void *cls, enum GNUNET_STREAM_Status status, size_t size)
436 {
437   struct PeerData *pdata = cls;
438   double throughput;
439   double prof_time_sec;
440
441   GNUNET_assert (GNUNET_STREAM_OK == status);
442   GNUNET_assert (size <= DATA_SIZE);
443   pdata->bytes_wrote += size;
444   for (;size > 0; size--)
445     update_meter (meter);
446   if (pdata->bytes_wrote < DATA_SIZE) /* Have more data to send */
447   {
448     pdata->io_write_handle =
449         GNUNET_STREAM_write (pdata->socket,
450                              ((void *) data) + pdata->bytes_wrote,
451                              sizeof (data) - pdata->bytes_wrote,
452                              GNUNET_TIME_UNIT_FOREVER_REL, &write_completion,
453                              pdata);
454     GNUNET_assert (NULL != pdata->io_write_handle);
455   }
456   else
457   {
458     free_meter (meter);
459     meter = NULL;
460     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
461     prof_time_sec = (((double) prof_time.rel_value)/ ((double) 1000));
462     throughput = (((float) sizeof (data)) / prof_time_sec);
463     PRINTF ("Throughput %.2f kB/sec\n", throughput / 1000.00);
464     switch (result)
465     {
466     case INIT:
467       result = UPLINK_OK;
468       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
469       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == write_task);
470       pdata->bytes_read = 0;
471       meter = create_meter (sizeof (data), "Testing Downlink\n", GNUNET_YES);
472       read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer_data[0]);
473       write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, &peer_data[1]);
474       break;
475     case UPLINK_OK:
476       result = DOWNLINK_OK;
477       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
478       break;
479     case DOWNLINK_OK:
480       GNUNET_assert (0);
481     }
482   }
483 }
484
485
486 /**
487  * Task for calling STREAM_write with a chunk of random data
488  *
489  * @param cls the peer data entity
490  * @param tc the task context
491  */
492 static void
493 stream_write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 {
495   struct PeerData *pdata = cls;
496   
497   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
498   {
499     GNUNET_SCHEDULER_cancel (abort_task);
500     abort_task = 
501       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
502                                     (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
503                                     NULL);
504   }
505   write_task = GNUNET_SCHEDULER_NO_TASK;
506   prof_start_time = GNUNET_TIME_absolute_get ();
507   pdata->bytes_wrote = 0;
508   pdata->io_write_handle = GNUNET_STREAM_write (pdata->socket, data,
509                                                 sizeof (data),
510                                                 GNUNET_TIME_UNIT_FOREVER_REL,
511                                                 &write_completion, pdata);
512   GNUNET_assert (NULL != pdata->io_write_handle);
513 }
514
515
516 /**
517  * Scheduler call back; to be executed when a new stream is connected
518  * Called from listen connect for peer2
519  */
520 static void
521 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
522
523
524 /**
525  * Input processor
526  *
527  * @param cls peer2
528  * @param status the status of the stream at the time this function is called
529  * @param data traffic from the other side
530  * @param size the number of bytes available in data read 
531  * @return number of bytes of processed from 'data' (any data remaining should be
532  *         given to the next time the read processor is called).
533  */
534 static size_t
535 input_processor (void *cls, enum GNUNET_STREAM_Status status,
536                  const void *input_data, size_t size)
537 {
538   struct PeerData *pdata = cls;
539
540   GNUNET_assert (GNUNET_STREAM_OK == status);
541   GNUNET_assert (size < DATA_SIZE);
542   GNUNET_assert (0 == memcmp (((void *)data ) + pdata->bytes_read, 
543                               input_data, size));
544   pdata->bytes_read += size;  
545   if (pdata->bytes_read < DATA_SIZE)
546   {
547     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
548     read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, pdata);
549   }
550   else 
551   {
552     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
553   }
554   return size;
555 }
556
557   
558 /**
559  * Scheduler call back; to be executed when a new stream is connected
560  * Called from listen connect for peer2
561  */
562 static void
563 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
564 {
565   struct PeerData *pdata = cls;
566
567   read_task = GNUNET_SCHEDULER_NO_TASK;
568   pdata->io_read_handle =
569       GNUNET_STREAM_read (pdata->socket, GNUNET_TIME_UNIT_FOREVER_REL,
570                           &input_processor, pdata);
571   GNUNET_assert (NULL != pdata->io_read_handle);
572 }
573
574
575 /**
576  * Functions of this type are called upon new stream connection from other peers
577  *
578  * @param cls the closure from GNUNET_STREAM_listen
579  * @param socket the socket representing the stream
580  * @param initiator the identity of the peer who wants to establish a stream
581  *            with us
582  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
583  *             stream (the socket will be invalid after the call)
584  */
585 static int
586 stream_listen_cb (void *cls, struct GNUNET_STREAM_Socket *socket,
587                   const struct GNUNET_PeerIdentity *initiator)
588 {
589   struct PeerData *pdata = cls;
590
591   
592   if ((NULL == socket) || (NULL == initiator))
593   {
594     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
595     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
596       GNUNET_SCHEDULER_cancel (abort_task);
597     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
598     return GNUNET_OK;
599   }
600   GNUNET_assert (NULL != socket);
601   GNUNET_assert (pdata == &peer_data[1]);
602   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer connected: %s\n",
603               GNUNET_i2s(initiator));
604   pdata->socket = socket;
605   pdata->bytes_read = 0;
606   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, pdata);
607   return GNUNET_OK;
608 }
609
610
611 /**
612  * Function executed after stream has been established
613  *
614  * @param cls the closure from GNUNET_STREAM_open
615  * @param socket socket to use to communicate with the other side (read/write)
616  */
617 static void 
618 stream_open_cb (void *cls,
619                 struct GNUNET_STREAM_Socket *socket)
620 {
621   struct PeerData *pdata = cls;
622
623   GNUNET_assert (socket == pdata->socket);
624   meter = create_meter (sizeof (data), "Testing Uplink\n", GNUNET_YES);
625   write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, pdata);
626 }
627
628
629 /**
630  * Listen success callback; connects a peer to stream as client
631  */
632 static void
633 stream_connect (void)
634 {
635   peer_data[0].socket = 
636       GNUNET_STREAM_open (config, &peer_data[1].id, 10, &stream_open_cb,
637                           &peer_data[0],
638                           GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
639                           payload_size[payload_size_index],
640                           GNUNET_STREAM_OPTION_END);
641   GNUNET_assert (NULL != peer_data[0].socket);
642 }
643
644
645 /**
646  * Initialize framework and start test
647  *
648  * @param cls closure
649  * @param cfg configuration of the peer that was started
650  * @param peer identity of the peer that was created
651  */
652 static void
653 run (void *cls, 
654      const struct GNUNET_CONFIGURATION_Handle *cfg,
655      struct GNUNET_TESTING_Peer *peer)
656 {
657   struct GNUNET_PeerIdentity id;
658
659   GNUNET_TESTING_peer_get_identity (peer, &id);
660   config = cfg;
661   peer2_listen_socket = 
662       GNUNET_STREAM_listen (config, 10, &stream_listen_cb, &peer_data[1],
663                             GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
664                             &stream_connect,
665                             GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
666                             payload_size[payload_size_index],
667                             GNUNET_STREAM_OPTION_END);
668   GNUNET_assert (NULL != peer2_listen_socket);
669   peer_data[0].id = id;
670   peer_data[1].id = id;
671   abort_task =
672       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
673                                     (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
674                                     NULL);
675 }
676
677
678 /**
679  * Adapter function called to establish a connection to
680  * a service.
681  * 
682  * @param cls closure
683  * @param cfg configuration of the peer to connect to; will be available until
684  *          GNUNET_TESTBED_operation_done() is called on the operation returned
685  *          from GNUNET_TESTBED_service_connect()
686  * @return service handle to return in 'op_result', NULL on error
687  */
688 static void * 
689 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
690
691
692 /**
693  * Adapter function called to destroy a connection to
694  * a service.
695  * 
696  * @param cls closure
697  * @param op_result service handle returned from the connect adapter
698  */
699 static void
700 stream_da (void *cls, void *op_result)
701 {
702   if (&peer_data[1] == cls)
703   {
704     GNUNET_STREAM_listen_close (op_result);
705     return;
706   }
707   else if (&peer_data[0] == cls)
708   {
709     GNUNET_STREAM_close (op_result);
710     return;
711   }
712   GNUNET_assert (0);
713 }
714
715
716 /**
717  * Listen success callback; connects a peer to stream as client. Called from
718  * testbed stream_ca
719  */
720 static void
721 stream_connect2 (void)
722 {
723   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
724   peer_data[0].op =
725       GNUNET_TESTBED_service_connect (&peer_data[0], peer_data[0].peer,
726                                       "stream", NULL, NULL, stream_ca,
727                                       stream_da, &peer_data[0]);
728 }
729
730
731 /**
732  * Adapter function called to establish a connection to
733  * a service.
734  * 
735  * @param cls closure
736  * @param cfg configuration of the peer to connect to; will be available until
737  *          GNUNET_TESTBED_operation_done() is called on the operation returned
738  *          from GNUNET_TESTBED_service_connect()
739  * @return service handle to return in 'op_result', NULL on error
740  */
741 static void * 
742 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
743 {
744   struct PeerData *pdata = cls;
745
746   if (&peer_data[1] == pdata)
747   {
748     peer2_listen_socket = NULL;
749     peer2_listen_socket =
750         GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, &peer_data[1],
751                               GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
752                               &stream_connect2,
753                               GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
754                               payload_size[payload_size_index],
755                               GNUNET_STREAM_OPTION_END);
756     GNUNET_assert (NULL != peer2_listen_socket);
757     return peer2_listen_socket;
758   }
759   if (&peer_data[0] == pdata)
760   {
761     pdata->socket =
762         GNUNET_STREAM_open (cfg, &peer_data[1].id, 10, &stream_open_cb,
763                             &peer_data[0],
764                             GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
765                             payload_size[payload_size_index],
766                             GNUNET_STREAM_OPTION_END);
767     GNUNET_assert (NULL != pdata->socket);
768     return pdata->socket;
769   }
770   GNUNET_assert (0);
771   return NULL;
772 }
773
774
775 /**
776  * Callback to be called when the requested peer information is available
777  *
778  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
779  * @param op the operation this callback corresponds to
780  * @param pinfo the result; will be NULL if the operation has failed
781  * @param emsg error message if the operation has failed; will be NULL if the
782  *          operation is successfull
783  */
784 static void 
785 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
786              const struct GNUNET_TESTBED_PeerInformation *pinfo,
787              const char *emsg)
788 {
789   struct PeerData *pdata = cb_cls;
790
791   GNUNET_assert (NULL == emsg);
792   GNUNET_assert (common_op == op);
793   GNUNET_assert (NULL != pdata);
794   memcpy (&pdata->id, pinfo->result.id, sizeof (struct GNUNET_PeerIdentity));
795   GNUNET_TESTBED_operation_done (op);
796   if (pdata == &peer_data[0])
797   {
798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n",
799                 GNUNET_i2s (&pdata->id));
800     common_op = GNUNET_TESTBED_peer_get_information (peer_data[1].peer,
801                                                      GNUNET_TESTBED_PIT_IDENTITY,
802                                                      &peerinfo_cb, &peer_data[1]);
803   }
804   else if (pdata == &peer_data[1])
805   {
806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n",
807                 GNUNET_i2s (&pdata->id));
808     if (TEST_STEP_2_HOP == test_step)
809       peer_data[1].op = 
810           GNUNET_TESTBED_service_connect (&peer_data[1], peer_data[1].peer,
811                                           "stream", NULL, NULL, stream_ca,
812                                           stream_da, &peer_data[1]);
813     else
814       GNUNET_break (0);         /* FIXME: 3 hop test case here... */
815   }
816 }
817
818
819 /**
820  * Controller event callback
821  *
822  * @param cls NULL
823  * @param event the controller event
824  */
825 static void
826 controller_event_cb (void *cls,
827                      const struct GNUNET_TESTBED_EventInformation *event)
828 {
829   switch (event->type)
830   {
831   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
832     if (NULL != event->details.operation_finished.emsg)
833     {
834       FPRINTF (stderr, "Error while expecting an operation to succeed:%s \n",
835                event->details.operation_finished.emsg);
836       GNUNET_assert (0);
837     }
838     break;
839   case GNUNET_TESTBED_ET_CONNECT:
840     GNUNET_TESTBED_operation_done (common_op);
841     /* Get the peer identity and configuration of peers */
842     common_op =
843         GNUNET_TESTBED_peer_get_information (peer_data[0].peer,
844                                              GNUNET_TESTBED_PIT_IDENTITY,
845                                              &peerinfo_cb, &peer_data[0]);
846     break;
847   default:
848     GNUNET_assert (0);
849   }
850 }
851
852
853 /**
854  * Signature of a main function for a testcase.
855  *
856  * @param cls closure
857  * @param num_peers number of peers in 'peers'
858  * @param peers handle to peers run in the testbed
859  */
860 static void
861 test_master (void *cls, unsigned int num_peers_,
862              struct GNUNET_TESTBED_Peer **peers)
863 {
864   GNUNET_assert (NULL != peers);
865   GNUNET_assert (NULL != peers[0]);
866   GNUNET_assert (NULL != peers[1]);
867   GNUNET_assert (num_peers_ == num_peers);
868   peer_data[0].peer = peers[0];
869   peer_data[1].peer = peers[1];
870   if (2 == num_peers)
871     common_op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL,
872                                                 peer_data[0].peer,
873                                                 peer_data[1].peer);
874   else
875     GNUNET_break (0);
876   abort_task =
877       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
878                                     (GNUNET_TIME_UNIT_SECONDS, 120), &do_abort,
879                                     NULL);
880 }
881
882
883 /**
884  * Main function
885  */
886 int main (int argc, char **argv)
887 {
888   char *test_name = "perf_stream_api";
889   char *cfg_file = "test_stream_local.conf";
890   uint64_t event_mask;
891   unsigned int count;
892   int ret;
893
894   meter = create_meter ((sizeof (data) / 4), "Generating random data\n", GNUNET_YES);
895   for (count=0; count < (sizeof (data) / 4); count++)
896   {
897     data[count] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
898                                             UINT32_MAX);
899     update_meter (meter);
900   }
901   reset_meter (meter);
902   free_meter (meter);
903   meter = NULL;
904   test_step = TEST_STEP_1_HOP;
905   for (payload_size_index = 0;
906        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
907        payload_size_index++)
908   {
909     PRINTF ("\nTesting over loopback with payload size %hu\n",
910             payload_size[payload_size_index]);
911     (void) memset (peer_data, 0, sizeof (peer_data));
912     result = INIT;
913     ret = GNUNET_TESTING_peer_run (test_name, cfg_file, &run, NULL);
914     if ((0 != ret) || (DOWNLINK_OK != result))
915       goto return_fail;
916   }
917   test_step = TEST_STEP_2_HOP;
918   num_peers = 2;
919   event_mask = 0;
920   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
921   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
922   for (payload_size_index = 0;
923        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
924        payload_size_index++)
925   {
926     PRINTF ("\nTesting over 1 hop with payload size %hu\n",
927             payload_size[payload_size_index]);
928     (void) memset (peer_data, 0, sizeof (peer_data));
929     result = GNUNET_SYSERR;
930     GNUNET_TESTBED_test_run (test_name, cfg_file, num_peers, event_mask,
931                              &controller_event_cb, NULL, &test_master, NULL);
932     if (DOWNLINK_OK != result)
933       goto return_fail;
934   }
935   test_step = TEST_STEP_3_HOP;
936   for (payload_size_index = 0; 
937        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
938        payload_size_index++)
939   {
940     /* Initialize testbed here */
941   }
942   return 0;
943
944  return_fail:
945   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test failed\n");
946   return 1;
947 }
948
949 /* end of perf_stream_api.c */