wip
[oweals/gnunet.git] / src / transport / test_transport_ats.c
index 75bad05fd3777d845d890c33de2fe2d11f0c588c..55bc7e78030331579ffcbae8b1bc5411d84a22c4 100644 (file)
@@ -23,6 +23,7 @@
  */
 #include "platform.h"
 #include "gnunet_testing_lib.h"
+#include "gnunet_transport_service.h"
 #include "gnunet_scheduler_lib.h"
 #include "gauger.h"
 
 
 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
+#define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
 
+#define ATS_NEW 0
+#define ATS_Q_UPDATED 1
+#define ATS_C_UPDATED 2
+#define ATS_QC_UPDATED 3
+#define ATS_UNMODIFIED 4
 
 static int ok;
 
@@ -41,13 +48,16 @@ static int peers_left;
 
 static int failed_peers;
 
-static int measurement_started;
+static int measurement_started = GNUNET_NO;
+static char * config_file;
 
 static struct GNUNET_TESTING_PeerGroup *pg;
 
-static  GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
-static  GNUNET_SCHEDULER_TaskIdentifier stats_task;
+static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+static GNUNET_SCHEDULER_TaskIdentifier stats_task;
+static GNUNET_SCHEDULER_TaskIdentifier send_task;
 struct GNUNET_TESTING_Daemon * master_deamon;
+struct GNUNET_TESTING_Daemon * ping_deamon;
 
 struct GNUNET_STATISTICS_Handle * stats;
 
@@ -58,18 +68,43 @@ struct TEST_result
        uint64_t mechs;
        uint64_t peers;
        uint64_t solution;
+       uint64_t state;
 };
 
-static int r_index;
-//static int measurements;
+struct TestMessage
+{
+  struct GNUNET_MessageHeader header;
+  uint32_t num;
+};
+
+
+static int count;
+static int c_new;
+static int c_unmodified;
+static int c_modified;
+static int connected;
 static int peers;
-static struct TEST_result results[MEASUREMENTS];
 
-struct GNUNET_STATISTICS_GetHandle * s_solution;
-struct GNUNET_STATISTICS_GetHandle * s_time;
-struct GNUNET_STATISTICS_GetHandle * s_peers;
-struct GNUNET_STATISTICS_GetHandle * s_mechs;
-struct GNUNET_STATISTICS_GetHandle * s_duration;
+static int force_q_updates;
+static int force_rebuild;
+static int send_msg;
+static int machine_parsable;
+
+static struct TEST_result results_new       [MEASUREMENTS+1];
+static struct TEST_result results_modified  [MEASUREMENTS+1];
+static struct TEST_result results_unmodified[MEASUREMENTS+1];
+static struct TEST_result current;
+
+static struct GNUNET_STATISTICS_GetHandle * s_solution;
+static struct GNUNET_STATISTICS_GetHandle * s_time;
+static struct GNUNET_STATISTICS_GetHandle * s_peers;
+static struct GNUNET_STATISTICS_GetHandle * s_mechs;
+static struct GNUNET_STATISTICS_GetHandle * s_duration;
+static struct GNUNET_STATISTICS_GetHandle * s_invalid;
+static struct GNUNET_STATISTICS_GetHandle * s_state;
+
+struct GNUNET_TRANSPORT_TransmitHandle * t;
+struct GNUNET_TRANSPORT_Handle * th;
 
 /**
  * Check whether peers successfully shut down.
@@ -109,6 +144,19 @@ static void shutdown_peers()
                GNUNET_SCHEDULER_cancel(stats_task);
                stats_task = GNUNET_SCHEDULER_NO_TASK;
        }
+       if (send_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+               GNUNET_SCHEDULER_cancel(send_task);
+               send_task = GNUNET_SCHEDULER_NO_TASK;
+       }
+
+       if (t!=NULL)
+       {
+               GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
+               t = NULL;
+       }
+
+       GNUNET_TRANSPORT_disconnect(th);
 
        if (s_time != NULL)
        {
@@ -135,7 +183,16 @@ static void shutdown_peers()
                GNUNET_STATISTICS_get_cancel(s_duration);
                s_duration = NULL;
        }
-
+       if (s_invalid != NULL)
+       {
+               GNUNET_STATISTICS_get_cancel(s_invalid);
+               s_invalid = NULL;
+       }
+       if (s_state != NULL)
+       {
+               GNUNET_STATISTICS_get_cancel(s_state);
+               s_state = NULL;
+       }
 
     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
 }
@@ -143,35 +200,74 @@ static void shutdown_peers()
 static void evaluate_measurements()
 {
        int c;
-       char * output = NULL;
-       char * temp;
-       double average;
-       double stddev;
-       double measure = MEASUREMENTS;
-       for (c=0; c<MEASUREMENTS;c++)
+       //int mechs = 0;
+       double average[3];
+       double stddev[3];
+       //char * output;
+       c = 1;
+
+       //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
+
+       average[0] = 0.0;
+       for (c=0; c<c_new;c++)
        {
-               average += (double) results[c].duration;
-               GNUNET_asprintf(&temp, "%s ,%i,%llu,%llu,%llu,%llu,", (output==NULL) ? "" : output, c, results[c].peers, results[c].mechs, results[c].duration, results[c].solution);
-               GNUNET_free_non_null (output);
-               output = temp;
+               average[0] += (double) results_new[c].duration;
        }
-       average /= measure;
+       average[0] /= c_new;
 
-       for (c=0; c<MEASUREMENTS;c++)
+       stddev[0] = 0.0;
+       for (c=0; c<c_new;c++)
        {
-               stddev += (results[c].duration - average) * (results[c].duration - average);
+               stddev[0] += (results_new[c].duration - average[0]) * (results_new[c].duration - average[0]);
        }
-       stddev /= measure;
-       stddev = sqrt (stddev);
+       stddev[0] /= c_new;
+       stddev[0] = sqrt (stddev[0]);
+       if (!machine_parsable) GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"new, %i measurements, average: %f stddev: %f\n",
+                       c_new, average[0], stddev[0]);
 
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"%s,avg,%f,stddev,%f\n",output,average,stddev);
-       /* only log benchmark time for 10 peers */
+       average[1] = 0.0;
+       for (c=0; c<c_modified;c++)
+       {
+               average[1] += (double) results_modified[c].duration;
+       }
+       average[1] /= c_modified;
 
-       if (results[c].peers == (10))
+       stddev[1] = 0.0;
+       for (c=0; c<c_modified;c++)
        {
-               GAUGER ("TRANSPORT", "ATS execution time 10 peers", (int) average , "ms");
+               stddev[1] += (results_modified[c].duration - average[1]) * (results_modified[c].duration - average[1]);
        }
+       stddev[1] /= c_modified;
+       stddev[1] = sqrt (stddev[1]);
+       if (!machine_parsable) GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"modified, %i measurements, average: %f stddev: %f\n",
+                       c_modified, average[1], stddev[1]);
 
+       average[2] = 0.0;
+       for (c=0; c<c_unmodified;c++)
+       {
+               average[2] += (double) results_unmodified[c].duration;
+       }
+       average[2] /= c_unmodified;
+       stddev[2] = 0.0;
+       for (c=0; c<c_unmodified;c++)
+       {
+               stddev[2] += (results_unmodified[c].duration - average[2]) * (results_unmodified[c].duration - average[2]);
+       }
+       stddev[2] /= c_unmodified;
+       stddev[2] = sqrt (stddev[2]);
+
+       if (!machine_parsable) GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"unmodified, %i measurements, average: %f stddev: %f\n",
+                       c_unmodified, average[2], stddev[2]);
+
+       if (machine_parsable)
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,",peers,%i,mechs,%i,"
+                                       "new,%i,%f,%f,"
+                                       "mod,%i,%f,%f,"
+                                       "unmod,%i,%f,%f\n",
+                                       peers-1, results_unmodified[0].mechs,
+                                       c_new, average[0], stddev[0],
+                                       c_modified, average[1], stddev[1],
+                                       c_unmodified, average[2], stddev[2]);
        shutdown_peers();
 }
 
@@ -181,6 +277,20 @@ int stats_cb (void *cls,
                           uint64_t value,
                           int is_persistent)
 {
+       static int printed = GNUNET_NO;
+       //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s = %llu\n", name ,value);
+       if (0 == strcmp (name,"ATS invalid solutions"))
+       {
+               if (stats_task != GNUNET_SCHEDULER_NO_TASK)
+               {
+                       GNUNET_SCHEDULER_cancel(stats_task);
+                       stats_task = GNUNET_SCHEDULER_NO_TASK;
+               }
+               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MLP produced invalid %llu result(s)!\n", value);
+               shutdown_peers();
+               return GNUNET_SYSERR;
+       }
+
        if (0 == strcmp (name,"ATS solution"))
        {
                s_solution = NULL;
@@ -204,64 +314,137 @@ int stats_cb (void *cls,
        {
                s_time = NULL;
        }
+       if (0 == strcmp (name,"ATS state"))
+       {
+               s_state = NULL;
+       }
 
     if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) && (value == peers-1))
     {
                measurement_started = GNUNET_YES;
-               r_index = 0;
+               count = 1;
                GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
+#if !VERBOSE
+               if (!machine_parsable)  fprintf(stderr, "%i", count);
+#endif
     }
+
     if (measurement_started == GNUNET_YES)
     {
                // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
                if (0 == strcmp (name,"ATS timestamp"))
                {
-                       if (results[r_index].timestamp == 0)
-                               results[r_index].timestamp = value;
-                       if (results[r_index].timestamp != value)
+                       if (current.timestamp == 0)
+                       {
+                               printed = GNUNET_NO;
+                               current.timestamp = value;
+                       }
+                       if (current.timestamp == value)
+                       {
+                               printed = GNUNET_YES;
+                       }
+                       if (current.timestamp != value)
                        {
-                               r_index++;
-                               if (r_index >= MEASUREMENTS)
+                               if (current.state == ATS_NEW)
+                               {
+                                       if (c_new < MEASUREMENTS)
+                                       {
+                                               results_new[c_new] = current;
+                                               c_new++;
+                                       }
+                                       else
+                                       {
+                                         force_rebuild = GNUNET_NO;
+                                         force_q_updates = GNUNET_NO;
+                                         send_msg = GNUNET_NO;
+                                       }
+                               }
+                               if (current.state == ATS_UNMODIFIED)
                                {
+                                       if (c_unmodified < MEASUREMENTS)
+                                       {
+                                               results_unmodified[c_unmodified] = current;
+                                               c_unmodified++;
+                                       }
+
+                               }
+                               if (current.state == ATS_Q_UPDATED)
+                               {
+                                       if (c_modified < MEASUREMENTS)
+                                       {
+                                               results_modified[c_modified] = current;
+                                               c_modified++;
+                                       }
+                                       else
+                                       {
+                                               force_q_updates = GNUNET_NO;
+                                               force_rebuild = GNUNET_YES;
+                                       }
+                               }
+                               count ++;
+
+                               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(new: %i / modified: %i / unmodified: %i) of %i \n", c_new, c_modified, c_unmodified , MEASUREMENTS);
+                               if ((c_modified >= MEASUREMENTS) && (c_new >= MEASUREMENTS) && (c_unmodified >= MEASUREMENTS))
+                               {
+#if !VERBOSE
+                                       if (!machine_parsable) fprintf(stdout, "\n");
+#endif
                                        if (stats_task != GNUNET_SCHEDULER_NO_TASK)
                                        {
                                                GNUNET_SCHEDULER_cancel(stats_task);
                                                stats_task = GNUNET_SCHEDULER_NO_TASK;
                                        }
                                        evaluate_measurements();
-                                       return GNUNET_NO;
+                                       return GNUNET_SYSERR;
                                }
-                               results[r_index].timestamp = value;
-                       }
 
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                                               "[%i] ATS solution: %s %llu \n", r_index, name, value);
+                               printed = GNUNET_NO;
+                               current.timestamp = value;
+#if !VERBOSE
+                               if (!machine_parsable) fprintf(stderr, "..%i", count);
+#endif
+                               return GNUNET_OK;
+                       }
                }
 
                if (0 == strcmp (name,"ATS solution"))
                {
-                       results[r_index].solution = value;
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                                               "[%i] ATS solution: %s %llu \n", r_index, name, value);
+                       current.solution = value;
+                       if (printed == GNUNET_NO) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
                }
 
                if (0 == strcmp (name,"ATS peers"))
                {
-                       results[r_index].peers = value;
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                                               "[%i] ATS peers: %s %llu \n", r_index, name, value);
+                       current.peers = value;
+                       if (printed == GNUNET_NO) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
                }
 
                if (0 == strcmp (name,"ATS mechanisms"))
                {
-                       results[r_index].mechs = value;
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS mechanisms: %s %llu \n", r_index, name, value);
+                       current.mechs = value;
+                       if (printed == GNUNET_NO) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
                }
 
                if (0 == strcmp (name,"ATS duration"))
                {
-                       results[r_index].duration = value;
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS duration: %s %llu \n", r_index, name, value);
+                       current.duration = value;
+                       if (printed == GNUNET_NO) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
+               }
+               if (0 == strcmp (name,"ATS state"))
+               {
+                       current.state = value;
+                       char * cont;
+                       if (value == ATS_NEW)
+                               cont = "NEW";
+                       if (value == ATS_C_UPDATED)
+                               cont = "C_UPDATED";
+                       if (value == ATS_Q_UPDATED)
+                               cont = "Q_UPDATED";
+                       if (value == ATS_QC_UPDATED)
+                               cont = "QC_UPDATED";
+                       if (value == ATS_UNMODIFIED)
+                               cont = "UNMODIFIED";
+                       if (printed == GNUNET_NO) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS state: %s\n", count, cont);
                }
     }
     return GNUNET_OK;
@@ -281,8 +464,10 @@ stats_get_task (void *cls,
        s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
        s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
        s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);
+       s_invalid = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions", TIMEOUT, NULL, &stats_cb, NULL);
+       s_state = GNUNET_STATISTICS_get (stats, "transport", "ATS state", TIMEOUT, NULL, &stats_cb, NULL);
 
-       stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250), &stats_get_task, NULL);
+       stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100), &stats_get_task, NULL);
 }
 
 void
@@ -305,6 +490,46 @@ static void connect_peers()
 
 }
 
+size_t send_dummy_data_task (void *cls, size_t size, void *buf)
+{
+
+       int s = sizeof (struct TestMessage);
+       struct TestMessage hdr;
+
+       hdr.header.size = htons (s);
+       hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
+       if (force_rebuild)
+               hdr.num = htonl (1);
+       if (force_q_updates)
+               hdr.num = htonl (2);
+
+
+       memcpy (buf,&hdr, s);
+       // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
+       t  = NULL;
+       return s;
+}
+
+void send_task_f (void *cls,
+                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+       send_task = GNUNET_SCHEDULER_NO_TASK;
+       if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+           return;
+
+       if (t!=NULL)
+       {
+               GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
+               t = NULL;
+       }
+       // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i to %s\n", size, GNUNET_i2s(&master_deamon->id));
+       if (send_msg == GNUNET_YES)
+               t = GNUNET_TRANSPORT_notify_transmit_ready(th, &master_deamon->id, sizeof (struct TestMessage), 0, SEND_TIMEOUT, &send_dummy_data_task, NULL);
+       send_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,1000), &send_task_f, NULL);
+}
+
+
+
 void daemon_connect_cb(void *cls,
                                                const struct GNUNET_PeerIdentity *first,
                                                const struct GNUNET_PeerIdentity *second,
@@ -315,7 +540,21 @@ void daemon_connect_cb(void *cls,
                                                struct GNUNET_TESTING_Daemon *second_daemon,
                                                const char *emsg)
 {
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s': `%s' \n", GNUNET_i2s(first), GNUNET_i2s(second), (emsg==NULL) ? "OK" : emsg);
+       char * firstc =  strdup(GNUNET_i2s(first));
+       char * secondc =  strdup(GNUNET_i2s(second));
+       connected++;
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s' (%i/%i)\n", firstc, secondc, connected, peers-1);
+       GNUNET_free(firstc);
+       GNUNET_free(secondc);
+
+       if (((first_daemon == ping_deamon) || (second_daemon == ping_deamon)) && (master_deamon != NULL) && (ping_deamon != NULL))
+       {
+                 th = GNUNET_TRANSPORT_connect (ping_deamon->cfg,&ping_deamon->id, NULL, NULL,NULL, NULL);
+                 t = NULL;
+                 force_q_updates = GNUNET_YES;
+                 send_msg = GNUNET_YES;
+                 send_task = GNUNET_SCHEDULER_add_now(&send_task_f, NULL);
+       }
 }
 
 void cont_cb (void *cls, int success)
@@ -364,7 +603,13 @@ daemon_start_cb (void *cls,
 
   if (peers_left == 0)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+         if (ping_deamon == NULL)
+         {
+                 ping_deamon = d;
+                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ping peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
+         }
+
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "All peers started successfully!\n");
       connect_peers();
       ok = 0;
@@ -373,7 +618,7 @@ daemon_start_cb (void *cls,
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Too many peers failed, ending test!\n");
-      GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+      shutdown_peers();
       ok = 1;
     }
 }
@@ -387,7 +632,7 @@ run (void *cls,
   ok = 1;
   measurement_started = GNUNET_NO;
 #if VERBOSE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", NUM_PEERS);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
 #endif
   peers_left = peers;
   pg = GNUNET_TESTING_daemons_start (cfg,
@@ -405,7 +650,7 @@ check ()
 {
   char *const argv[] = { "test-testing",
     "-c",
-    "test_transport_ats.conf",
+    config_file,
 #if VERBOSE
     "-L", "DEBUG",
 #endif
@@ -415,7 +660,7 @@ check ()
     GNUNET_GETOPT_OPTION_END
   };
   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                      argv, "test-testing-group", "nohelp",
+                      argv, "test-transport-ats", "nohelp",
                       options, &run, &ok);
   return ok;
 }
@@ -423,24 +668,55 @@ check ()
 int
 main (int argc, char *argv[])
 {
+
   int ret;
 
   GNUNET_log_setup ("test-transport-ats",
 #if VERBOSE
                     "DEBUG",
 #else
-                    "WARNING",
+                    "INFO",
 #endif
                     NULL);
 
+#if !HAVE_LIBGLPK
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, exiting testcase\n");
+       return 0;
+#endif
+
+  GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
+
+
+  machine_parsable = GNUNET_NO;
   peers = NUM_PEERS;
+  config_file = "test_transport_ats_4addr.conf";
+
+  int c = 0;
   if (argc >= 2)
   {
-         peers = atoi(argv[1]);
-         peers++;
-         if(peers <1)
-                 peers = NUM_PEERS;
+        for (c=0; c<argc; c++)
+        {
+                /* set peers */
+                if ((strcmp(argv[c], "-p") == 0) && c < (argc-1))
+                {
+                         peers = atoi(argv[c+1]);
+                         peers++;
+                }
+                /* set machine parsable */
+                if (strcmp(argv[c], "-m") == 0)
+                {
+                        machine_parsable = GNUNET_YES;
+                }
+                /* set config file */
+                if ((strcmp(argv[c], "-c") == 0) && c < (argc-1))
+                {
+                        config_file = argv[c+1];
+                }
+        }
   }
+
+
+
   ret = check ();
   /**
    * Still need to remove the base testing directory here,