allow empty/NULL context message
[oweals/gnunet.git] / src / transport / gnunet-transport-profiler.c
index 8c41ea2b53a0b601e5e8a05c06d28c3e9f91bc3f..af47c5c567c4c7da16ed597b6f98a5f24181a2c9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- (C) 2011-2014 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2015 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 
 /**
- * @file src/transport/gnunet-transport.c
- * @brief Tool to help configure, measure and control the transport subsystem.
+ * @file src/transport/gnunet-transport-profiler.c
+ * @brief Tool to help benchmark the transport subsystem.
  * @author Christian Grothoff
  * @author Nathan Evans
  *
- * This utility can be used to test if a transport mechanism for
- * GNUnet is properly configured.
+ * This utility can be used to benchmark a transport mechanism for
+ * GNUnet.
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
+#include "gnunet_ats_service.h"
 #include "gnunet_transport_service.h"
 
 struct Iteration
@@ -41,6 +42,9 @@ struct Iteration
 
   struct GNUNET_TIME_Relative dur;
 
+  /* Transmission rate for this iteration in KB/s */
+  float rate;
+
   unsigned int msgs_sent;
 };
 
@@ -105,6 +109,11 @@ static char *cpid;
  */
 static struct GNUNET_TRANSPORT_Handle *handle;
 
+/**
+ * Handle to ATS service.
+ */
+static struct GNUNET_ATS_ConnectivityHandle *ats;
+
 /**
  * Configuration handle
  */
@@ -113,11 +122,11 @@ static struct GNUNET_CONFIGURATION_Handle *cfg;
 /**
  * Try_connect handle
  */
-struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
+static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
 
+static struct Iteration *ihead;
 
-struct Iteration *ihead;
-struct Iteration *itail;
+static struct Iteration *itail;
 
 /**
  * Global return value (0 success).
@@ -128,7 +137,7 @@ static int ret;
  */
 static struct GNUNET_TRANSPORT_TransmitHandle *th;
 
-struct GNUNET_TRANSPORT_Blacklist *bl_handle;
+static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
 
 /**
  * Identity of the peer we transmit to / connect to.
@@ -136,74 +145,126 @@ struct GNUNET_TRANSPORT_Blacklist *bl_handle;
  */
 static struct GNUNET_PeerIdentity pid;
 
-/**
- * Task scheduled for cleanup / termination of the process.
- */
-static GNUNET_SCHEDULER_TaskIdentifier end;
-
 /**
  * Selected level of verbosity.
  */
 static int verbosity;
 
+
 /**
  * Task run in monitor mode when the user presses CTRL-C to abort.
  * Stops monitoring activity.
  *
  * @param cls NULL
- * @param tc scheduler context
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   struct Iteration *icur;
   struct Iteration *inext;
 
-  if (NULL != tc_handle)
+  unsigned int iterations;
+
+  unsigned long long avg_duration;
+  float avg_rate;
+  float stddev_rate;
+  float stddev_duration;
+
+  if (NULL != ats_sh)
   {
-    GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
-    tc_handle = NULL;
+    GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
+    ats_sh = NULL;
   }
   if (NULL != th)
   {
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
     th = NULL;
   }
-
   if (NULL != bl_handle )
   {
     GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
     bl_handle = NULL;
   }
-
+  if (NULL != ats)
+  {
+    GNUNET_ATS_connectivity_done (ats);
+    ats = NULL;
+  }
   if (NULL != handle)
   {
     GNUNET_TRANSPORT_disconnect (handle);
     handle = NULL;
   }
 
+  if (verbosity > 0)
+    FPRINTF (stdout, "\n");
+
+  /* Output format:
+   * All time values in ms
+   * Rate in KB/s
+   * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
+
   if (benchmark_send)
   {
+    /* First iteration to calculcate avg and stddev */
+    iterations = 0;
+    avg_duration = 0;
+    avg_rate = 0.0;
+
     inext = ihead;
     while (NULL != (icur = inext))
     {
       inext = icur->next;
+      icur->rate = ((benchmark_count * benchmark_size) / 1024) /
+          ((float) icur->dur.rel_value_us / (1000 * 1000));
       if (verbosity > 0)
         FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
             ((long long unsigned int) benchmark_count * benchmark_size),
-            ((long long unsigned int) itail->dur.rel_value_us / 1000),
-            (float) ((benchmark_count * benchmark_size) / 1024)/ ((float) itail->dur.rel_value_us / (1000 * 1000)));
+            ((long long unsigned int) icur->dur.rel_value_us / 1000),
+            (float) icur->rate);
+
+      avg_duration += icur->dur.rel_value_us / (1000);
+      avg_rate  += icur->rate;
+      iterations ++;
     }
-    FPRINTF (stdout, _("%u;%u"),benchmark_count, benchmark_size);
+
+    /* Calculate average rate */
+    avg_rate /= iterations;
+    /* Calculate average duration */
+    avg_duration /= iterations;
+
+    stddev_rate = 0;
+    stddev_duration = 0;
+    inext = ihead;
+    while (NULL != (icur = inext))
+    {
+      inext = icur->next;
+      stddev_rate += ((icur->rate-avg_rate) *
+          (icur->rate-avg_rate));
+      stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
+          ((icur->dur.rel_value_us / 1000) - avg_duration));
+
+    }
+    /* Calculate standard deviation rate */
+    stddev_rate = stddev_rate / iterations;
+    stddev_rate = sqrtf(stddev_rate);
+
+    /* Calculate standard deviation duration */
+    stddev_duration = stddev_duration / iterations;
+    stddev_duration = sqrtf(stddev_duration);
+
+    /* Output */
+    FPRINTF (stdout, _("%u;%u;%llu;%llu;%.2f;%.2f"),benchmark_count, benchmark_size,
+        avg_duration, (unsigned long long) stddev_duration, avg_rate, stddev_rate);
+
     inext = ihead;
     while (NULL != (icur = inext))
     {
       inext = icur->next;
       GNUNET_CONTAINER_DLL_remove (ihead, itail, icur);
 
-      FPRINTF (stdout, _(";%llu"),
-      (long long unsigned int) icur->dur.rel_value_us);
+      FPRINTF (stdout, _(";%llu;%.2f"),
+          (long long unsigned int) (icur->dur.rel_value_us / 1000), icur->rate);
 
       GNUNET_free (icur);
     }
@@ -225,6 +286,7 @@ shutdown_task (void *cls,
 static void
 iteration_done ();
 
+
 /**
  * Function called to notify a client about the socket
  * begin ready to queue more data.  @a buf will be
@@ -302,6 +364,7 @@ iteration_start ()
   }
 }
 
+
 static void
 iteration_done ()
 {
@@ -312,9 +375,7 @@ iteration_done ()
   if (it_count == benchmark_iterations)
   {
     benchmark_running = GNUNET_NO;
-    if (GNUNET_SCHEDULER_NO_TASK != end)
-      GNUNET_SCHEDULER_cancel (end);
-    end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   else
@@ -324,7 +385,6 @@ iteration_done ()
 }
 
 
-
 /**
  * Function called to notify transport users that another
  * peer connected to us.
@@ -336,10 +396,13 @@ static void
 notify_connect (void *cls,
                 const struct GNUNET_PeerIdentity *peer)
 {
-  if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
+  if (0 != memcmp (&pid,
+                   peer,
+                   sizeof(struct GNUNET_PeerIdentity)))
   {
     FPRINTF (stdout,
-        _("Connected to different peer `%s'\n"), GNUNET_i2s (&pid));
+             _("Connected to different peer `%s'\n"),
+             GNUNET_i2s (&pid));
     return;
   }
 
@@ -347,13 +410,6 @@ notify_connect (void *cls,
     FPRINTF (stdout,
         _("Successfully connected to `%s'\n"),
         GNUNET_i2s (&pid));
-
-  if (NULL != tc_handle)
-  {
-    GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
-    tc_handle = NULL;
-  }
-
   iteration_start ();
 }
 
@@ -379,6 +435,7 @@ notify_disconnect (void *cls,
   }
 }
 
+
 /**
  * Function called by the transport for each received message.
  *
@@ -405,49 +462,16 @@ notify_receive (void *cls,
 }
 
 
-
-static void
-try_connect_cb (void *cls,
-                const int result)
-{
-  static int retries = 0;
-
-  if (GNUNET_OK == result)
-  {
-    tc_handle = NULL;
-    return;
-  }
-
-  retries++;
-  if (retries < 10)
-  {
-    if (verbosity > 0)
-      FPRINTF (stdout, _("Retrying to connect to `%s'\n"), GNUNET_i2s (&pid));
-
-    tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
-        NULL);
-  }
-  else
-  {
-    FPRINTF (stderr,
-             "%s",
-             _("Failed to send connect request to transport service\n"));
-    if (GNUNET_SCHEDULER_NO_TASK != end)
-      GNUNET_SCHEDULER_cancel (end);
-    end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
-    ret = 1;
-    return;
-  }
-}
-
 static int
-blacklist_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
+blacklist_cb (void *cls,
+              const struct GNUNET_PeerIdentity *peer)
 {
   if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
   {
     if (verbosity > 0)
       FPRINTF (stdout,
-          _("Denying connection to `%s'\n"), GNUNET_i2s (peer));
+               _("Denying connection to `%s'\n"),
+               GNUNET_i2s (peer));
     return GNUNET_SYSERR;
   }
 
@@ -485,14 +509,14 @@ testservice_task (void *cls, int result)
     FPRINTF (stderr, _("No peer identity given\n"));
     return;
   }
-  if ((GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
-              &pid.public_key)))
+  if ((GNUNET_OK !=
+       GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
+                                                   &pid.public_key)))
   {
     FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
     return;
   }
 
-
   if (1 == benchmark_send)
   {
     if (verbosity > 0)
@@ -512,22 +536,35 @@ testservice_task (void *cls, int result)
     return;
   }
 
-  handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
-      &notify_connect, &notify_disconnect);
+  ats = GNUNET_ATS_connectivity_init (cfg);
+  if (NULL == ats)
+  {
+    FPRINTF (stderr, "%s", _("Failed to connect to ATS service\n"));
+    ret = 1;
+    return;
+  }
 
+  handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
+                                     &notify_receive,
+                                     &notify_connect,
+                                     &notify_disconnect);
   if (NULL == handle)
   {
     FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
+    GNUNET_ATS_connectivity_done (ats);
+    ats = NULL;
     ret = 1;
     return;
   }
 
-  bl_handle = GNUNET_TRANSPORT_blacklist (cfg, blacklist_cb, NULL);
-  tc_handle = GNUNET_TRANSPORT_try_connect(handle, &pid, try_connect_cb, NULL);
-
-  end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                      &shutdown_task,
-                                      NULL);
+  bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
+                                          &blacklist_cb,
+                                          NULL);
+  ats_sh = GNUNET_ATS_connectivity_suggest (ats,
+                                            &pid,
+                                            1);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 }
 
 
@@ -550,6 +587,7 @@ run (void *cls,
       &testservice_task, (void *) cfg);
 }
 
+
 int
 main (int argc, char * const *argv)
 {
@@ -597,4 +635,4 @@ main (int argc, char * const *argv)
   return 1;
 }
 
-/* end of gnunet-transport.c */
+/* end of gnunet-transport-profiler.c */