fix to keep testing running
[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_WARNING, "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   if (GNUNET_STREAM_OK != status)
442   {
443     GNUNET_SCHEDULER_cancel (abort_task);
444     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
445     return;
446   }
447   GNUNET_assert (size <= DATA_SIZE);
448   pdata->bytes_wrote += size;
449   for (;size > 0; size--)
450     update_meter (meter);
451   if (pdata->bytes_wrote < DATA_SIZE) /* Have more data to send */
452   {      
453     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
454     {
455       GNUNET_SCHEDULER_cancel (abort_task);
456       abort_task = 
457           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
458                                         (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
459                                     NULL);
460     }
461     pdata->io_write_handle =
462         GNUNET_STREAM_write (pdata->socket,
463                              ((void *) data) + pdata->bytes_wrote,
464                              sizeof (data) - pdata->bytes_wrote,
465                              GNUNET_TIME_UNIT_FOREVER_REL, &write_completion,
466                              pdata);
467     GNUNET_assert (NULL != pdata->io_write_handle);
468   }
469   else
470   {
471     free_meter (meter);
472     meter = NULL;
473     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
474     prof_time_sec = (((double) prof_time.rel_value)/ ((double) 1000));
475     throughput = (((float) sizeof (data)) / prof_time_sec);
476     PRINTF ("Throughput %.2f kB/sec\n", throughput / 1000.00);
477     switch (result)
478     {
479     case INIT:
480       result = UPLINK_OK;
481       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
482       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == write_task);
483       pdata->bytes_read = 0;
484       meter = create_meter (sizeof (data), "Testing Downlink\n", GNUNET_YES);
485       read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer_data[0]);
486       write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, &peer_data[1]);
487       break;
488     case UPLINK_OK:
489       result = DOWNLINK_OK;
490       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
491       break;
492     case DOWNLINK_OK:
493       GNUNET_assert (0);
494     }
495   }
496 }
497
498
499 /**
500  * Task for calling STREAM_write with a chunk of random data
501  *
502  * @param cls the peer data entity
503  * @param tc the task context
504  */
505 static void
506 stream_write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
507 {
508   struct PeerData *pdata = cls;
509   
510   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
511   {
512     GNUNET_SCHEDULER_cancel (abort_task);
513     abort_task = 
514       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
515                                     (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
516                                     NULL);
517   }
518   write_task = GNUNET_SCHEDULER_NO_TASK;
519   prof_start_time = GNUNET_TIME_absolute_get ();
520   pdata->bytes_wrote = 0;
521   pdata->io_write_handle = GNUNET_STREAM_write (pdata->socket, data,
522                                                 sizeof (data),
523                                                 GNUNET_TIME_UNIT_FOREVER_REL,
524                                                 &write_completion, pdata);
525   GNUNET_assert (NULL != pdata->io_write_handle);
526 }
527
528
529 /**
530  * Scheduler call back; to be executed when a new stream is connected
531  * Called from listen connect for peer2
532  */
533 static void
534 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
535
536
537 /**
538  * Input processor
539  *
540  * @param cls peer2
541  * @param status the status of the stream at the time this function is called
542  * @param data traffic from the other side
543  * @param size the number of bytes available in data read 
544  * @return number of bytes of processed from 'data' (any data remaining should be
545  *         given to the next time the read processor is called).
546  */
547 static size_t
548 input_processor (void *cls, enum GNUNET_STREAM_Status status,
549                  const void *input_data, size_t size)
550 {
551   struct PeerData *pdata = cls;
552
553   if (GNUNET_STREAM_OK != status)
554   {
555     GNUNET_SCHEDULER_cancel (abort_task);
556     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
557     return 0;
558   }
559   GNUNET_assert (size < DATA_SIZE);
560   GNUNET_assert (0 == memcmp (((void *)data ) + pdata->bytes_read, 
561                               input_data, size));
562   pdata->bytes_read += size;  
563   if (pdata->bytes_read < DATA_SIZE)
564   {
565     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
566     read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, pdata);
567   }
568   else 
569   {
570     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
571   }
572   return size;
573 }
574
575   
576 /**
577  * Scheduler call back; to be executed when a new stream is connected
578  * Called from listen connect for peer2
579  */
580 static void
581 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
582 {
583   struct PeerData *pdata = cls;
584
585   read_task = GNUNET_SCHEDULER_NO_TASK;
586   pdata->io_read_handle =
587       GNUNET_STREAM_read (pdata->socket, GNUNET_TIME_UNIT_FOREVER_REL,
588                           &input_processor, pdata);
589   GNUNET_assert (NULL != pdata->io_read_handle);
590 }
591
592
593 /**
594  * Functions of this type are called upon new stream connection from other peers
595  *
596  * @param cls the closure from GNUNET_STREAM_listen
597  * @param socket the socket representing the stream
598  * @param initiator the identity of the peer who wants to establish a stream
599  *            with us
600  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
601  *             stream (the socket will be invalid after the call)
602  */
603 static int
604 stream_listen_cb (void *cls, struct GNUNET_STREAM_Socket *socket,
605                   const struct GNUNET_PeerIdentity *initiator)
606 {
607   struct PeerData *pdata = cls;
608
609   
610   if ((NULL == socket) || (NULL == initiator))
611   {
612     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
613     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
614       GNUNET_SCHEDULER_cancel (abort_task);
615     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
616     return GNUNET_OK;
617   }
618   GNUNET_assert (NULL != socket);
619   GNUNET_assert (pdata == &peer_data[1]);
620   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer connected: %s\n",
621               GNUNET_i2s(initiator));
622   pdata->socket = socket;
623   pdata->bytes_read = 0;
624   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, pdata);
625   return GNUNET_OK;
626 }
627
628
629 /**
630  * Function executed after stream has been established
631  *
632  * @param cls the closure from GNUNET_STREAM_open
633  * @param socket socket to use to communicate with the other side (read/write)
634  */
635 static void 
636 stream_open_cb (void *cls,
637                 struct GNUNET_STREAM_Socket *socket)
638 {
639   struct PeerData *pdata = cls;
640
641   GNUNET_assert (socket == pdata->socket);
642   meter = create_meter (sizeof (data), "Testing Uplink\n", GNUNET_YES);
643   write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, pdata);
644 }
645
646
647 /**
648  * Listen success callback; connects a peer to stream as client
649  */
650 static void
651 stream_connect (void)
652 {
653   peer_data[0].socket = 
654       GNUNET_STREAM_open (config, &peer_data[1].id, 10, &stream_open_cb,
655                           &peer_data[0],
656                           GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
657                           payload_size[payload_size_index],
658                           GNUNET_STREAM_OPTION_END);
659   GNUNET_assert (NULL != peer_data[0].socket);
660 }
661
662
663 /**
664  * Initialize framework and start test
665  *
666  * @param cls closure
667  * @param cfg configuration of the peer that was started
668  * @param peer identity of the peer that was created
669  */
670 static void
671 run (void *cls, 
672      const struct GNUNET_CONFIGURATION_Handle *cfg,
673      struct GNUNET_TESTING_Peer *peer)
674 {
675   struct GNUNET_PeerIdentity id;
676
677   GNUNET_TESTING_peer_get_identity (peer, &id);
678   config = cfg;
679   peer2_listen_socket = 
680       GNUNET_STREAM_listen (config, 10, &stream_listen_cb, &peer_data[1],
681                             GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
682                             &stream_connect,
683                             GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
684                             payload_size[payload_size_index],
685                             GNUNET_STREAM_OPTION_END);
686   GNUNET_assert (NULL != peer2_listen_socket);
687   peer_data[0].id = id;
688   peer_data[1].id = id;
689   abort_task =
690       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
691                                     (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
692                                     NULL);
693 }
694
695
696 /**
697  * Adapter function called to establish a connection to
698  * a service.
699  * 
700  * @param cls closure
701  * @param cfg configuration of the peer to connect to; will be available until
702  *          GNUNET_TESTBED_operation_done() is called on the operation returned
703  *          from GNUNET_TESTBED_service_connect()
704  * @return service handle to return in 'op_result', NULL on error
705  */
706 static void * 
707 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
708
709
710 /**
711  * Adapter function called to destroy a connection to
712  * a service.
713  * 
714  * @param cls closure
715  * @param op_result service handle returned from the connect adapter
716  */
717 static void
718 stream_da (void *cls, void *op_result)
719 {
720   if (&peer_data[1] == cls)
721   {
722     GNUNET_STREAM_listen_close (op_result);
723     return;
724   }
725   else if (&peer_data[0] == cls)
726   {
727     GNUNET_STREAM_close (op_result);
728     return;
729   }
730   GNUNET_assert (0);
731 }
732
733
734 /**
735  * Listen success callback; connects a peer to stream as client. Called from
736  * testbed stream_ca
737  */
738 static void
739 stream_connect2 (void)
740 {
741   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
742   peer_data[0].op =
743       GNUNET_TESTBED_service_connect (&peer_data[0], peer_data[0].peer,
744                                       "stream", NULL, NULL, stream_ca,
745                                       stream_da, &peer_data[0]);
746 }
747
748
749 /**
750  * Adapter function called to establish a connection to
751  * a service.
752  * 
753  * @param cls closure
754  * @param cfg configuration of the peer to connect to; will be available until
755  *          GNUNET_TESTBED_operation_done() is called on the operation returned
756  *          from GNUNET_TESTBED_service_connect()
757  * @return service handle to return in 'op_result', NULL on error
758  */
759 static void * 
760 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
761 {
762   struct PeerData *pdata = cls;
763
764   if (&peer_data[1] == pdata)
765   {
766     peer2_listen_socket = NULL;
767     peer2_listen_socket =
768         GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, &peer_data[1],
769                               GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
770                               &stream_connect2,
771                               GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
772                               payload_size[payload_size_index],
773                               GNUNET_STREAM_OPTION_END);
774     GNUNET_assert (NULL != peer2_listen_socket);
775     return peer2_listen_socket;
776   }
777   if (&peer_data[0] == pdata)
778   {
779     pdata->socket =
780         GNUNET_STREAM_open (cfg, &peer_data[1].id, 10, &stream_open_cb,
781                             &peer_data[0],
782                             GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE,
783                             payload_size[payload_size_index],
784                             GNUNET_STREAM_OPTION_END);
785     GNUNET_assert (NULL != pdata->socket);
786     return pdata->socket;
787   }
788   GNUNET_assert (0);
789   return NULL;
790 }
791
792
793 /**
794  * Callback to be called when the requested peer information is available
795  *
796  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
797  * @param op the operation this callback corresponds to
798  * @param pinfo the result; will be NULL if the operation has failed
799  * @param emsg error message if the operation has failed; will be NULL if the
800  *          operation is successfull
801  */
802 static void 
803 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
804              const struct GNUNET_TESTBED_PeerInformation *pinfo,
805              const char *emsg)
806 {
807   struct PeerData *pdata = cb_cls;
808
809   GNUNET_assert (NULL == emsg);
810   GNUNET_assert (common_op == op);
811   GNUNET_assert (NULL != pdata);
812   memcpy (&pdata->id, pinfo->result.id, sizeof (struct GNUNET_PeerIdentity));
813   GNUNET_TESTBED_operation_done (op);
814   if (pdata == &peer_data[0])
815   {
816     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n",
817                 GNUNET_i2s (&pdata->id));
818     common_op = GNUNET_TESTBED_peer_get_information (peer_data[1].peer,
819                                                      GNUNET_TESTBED_PIT_IDENTITY,
820                                                      &peerinfo_cb, &peer_data[1]);
821   }
822   else if (pdata == &peer_data[1])
823   {
824     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n",
825                 GNUNET_i2s (&pdata->id));
826     if (TEST_STEP_2_HOP == test_step)
827       peer_data[1].op = 
828           GNUNET_TESTBED_service_connect (&peer_data[1], peer_data[1].peer,
829                                           "stream", NULL, NULL, stream_ca,
830                                           stream_da, &peer_data[1]);
831     else
832       GNUNET_break (0);         /* FIXME: 3 hop test case here... */
833   }
834 }
835
836
837 /**
838  * Controller event callback
839  *
840  * @param cls NULL
841  * @param event the controller event
842  */
843 static void
844 controller_event_cb (void *cls,
845                      const struct GNUNET_TESTBED_EventInformation *event)
846 {
847   switch (event->type)
848   {
849   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
850     if (NULL != event->details.operation_finished.emsg)
851     {
852       FPRINTF (stderr, "Error while expecting an operation to succeed:%s \n",
853                event->details.operation_finished.emsg);
854       GNUNET_assert (0);
855     }
856     break;
857   case GNUNET_TESTBED_ET_CONNECT:
858     GNUNET_TESTBED_operation_done (common_op);
859     /* Get the peer identity and configuration of peers */
860     common_op =
861         GNUNET_TESTBED_peer_get_information (peer_data[0].peer,
862                                              GNUNET_TESTBED_PIT_IDENTITY,
863                                              &peerinfo_cb, &peer_data[0]);
864     break;
865   default:
866     GNUNET_assert (0);
867   }
868 }
869
870
871 /**
872  * Signature of a main function for a testcase.
873  *
874  * @param cls closure
875  * @param num_peers number of peers in 'peers'
876  * @param peers handle to peers run in the testbed
877  */
878 static void
879 test_master (void *cls, unsigned int num_peers_,
880              struct GNUNET_TESTBED_Peer **peers)
881 {
882   GNUNET_assert (NULL != peers);
883   GNUNET_assert (NULL != peers[0]);
884   GNUNET_assert (NULL != peers[1]);
885   GNUNET_assert (num_peers_ == num_peers);
886   peer_data[0].peer = peers[0];
887   peer_data[1].peer = peers[1];
888   if (2 == num_peers)
889     common_op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL,
890                                                 peer_data[0].peer,
891                                                 peer_data[1].peer);
892   else
893     GNUNET_break (0);
894   abort_task =
895       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
896                                     (GNUNET_TIME_UNIT_SECONDS, 120), &do_abort,
897                                     NULL);
898 }
899
900
901 /**
902  * Main function
903  */
904 int main (int argc, char **argv)
905 {
906   char *test_name = "perf_stream_api";
907   char *cfg_file = "test_stream_local.conf";
908   uint64_t event_mask;
909   unsigned int count;
910   int ret;
911
912   meter = create_meter ((sizeof (data) / 4), "Generating random data\n", GNUNET_YES);
913   for (count=0; count < (sizeof (data) / 4); count++)
914   {
915     data[count] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
916                                             UINT32_MAX);
917     update_meter (meter);
918   }
919   reset_meter (meter);
920   free_meter (meter);
921   meter = NULL;
922   test_step = TEST_STEP_1_HOP;
923   for (payload_size_index = 0;
924        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
925        payload_size_index++)
926   {
927     PRINTF ("\nTesting over loopback with payload size %hu\n",
928             payload_size[payload_size_index]);
929     (void) memset (peer_data, 0, sizeof (peer_data));
930     result = INIT;
931     ret = GNUNET_TESTING_peer_run (test_name, cfg_file, &run, NULL);
932     if ((0 != ret) || (DOWNLINK_OK != result))
933       goto return_fail;
934   }
935   test_step = TEST_STEP_2_HOP;
936   num_peers = 2;
937   event_mask = 0;
938   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
939   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
940   for (payload_size_index = 0;
941        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
942        payload_size_index++)
943   {
944     PRINTF ("\nTesting over 1 hop with payload size %hu\n",
945             payload_size[payload_size_index]);
946     (void) memset (peer_data, 0, sizeof (peer_data));
947     result = INIT;
948     GNUNET_TESTBED_test_run (test_name, cfg_file, num_peers, event_mask,
949                              &controller_event_cb, NULL, &test_master, NULL);
950     if (DOWNLINK_OK != result)
951       goto return_fail;
952   }
953   test_step = TEST_STEP_3_HOP;
954   for (payload_size_index = 0; 
955        payload_size_index < (sizeof (payload_size) / sizeof (uint16_t));
956        payload_size_index++)
957   {
958     /* Initialize testbed here */
959   }
960   return 0;
961
962  return_fail:
963   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test failed\n");
964   return 1;
965 }
966
967 /* end of perf_stream_api.c */