- cancel SYNACK retry task when forced to SYNACK by an incoming SYN
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.c
index 234d4f0e2067c6cf75c58aca07078b8e75644815..5e2b08b28ca2760a4d352dc7aa675a7025498683 100644 (file)
 #define PING 1
 #define PONG 2
 
-/**
- * How namy peers to run
- */
-#define TOTAL_PEERS 10
-
-/**
- * Duration of each round.
- */
-#define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
 
 /**
  * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
  */
-#define PING_PERIOD 2000
+#define PING_PERIOD 1000
 
 /**
  * How long until we give up on connecting the peers?
  */
 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
 
-static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
+/**
+ * Total number of rounds.
+ */
+#define number_rounds sizeof(rounds)/sizeof(rounds[0])
+
+/**
+ * Ratio of peers active. First round always is 1.0.
+ */
+static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.0};
+
+/**
+ * Message type for pings.
+ */
+struct MeshPingMessage
+{
+  /**
+   * Header. Type PING/PONG.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Message number.
+   */
+  uint32_t counter;
+
+  /**
+   * Time the message was sent.
+   */
+  struct GNUNET_TIME_AbsoluteNBO timestamp;
+
+  /**
+   * Round number.
+   */
+  uint32_t round_number;
+};
 
+/**
+ * Peer description.
+ */
 struct MeshPeer
 {
   /**
@@ -86,6 +114,11 @@ struct MeshPeer
    */
   struct GNUNET_MESH_Channel *incoming_ch;
 
+  /**
+   * Channel handle for a warmup channel.
+   */
+  struct GNUNET_MESH_Channel *warmup_ch;
+
   /**
    * Number of payload packes sent
    */
@@ -96,14 +129,38 @@ struct MeshPeer
    */
   int data_received;
 
+  /**
+   * Is peer up?
+   */
   int up;
 
+  /**
+   * Destinaton to ping.
+   */
   struct MeshPeer *dest;
+
+  /**
+   * Incoming channel for pings.
+   */
   struct MeshPeer *incoming;
+
+  /**
+   * Task to do the next ping.
+   */
   GNUNET_SCHEDULER_TaskIdentifier ping_task;
-  struct GNUNET_TIME_Absolute timestamp;
+
+  float mean[number_rounds];
+  float var[number_rounds];
+  unsigned int pongs[number_rounds];
+  unsigned int pings[number_rounds];
+
 };
 
+/**
+ * Duration of each round.
+ */
+static struct GNUNET_TIME_Relative round_time;
+
 /**
  * GNUNET_PeerIdentity -> MeshPeer
  */
@@ -120,34 +177,29 @@ static struct GNUNET_TESTBED_Peer **testbed_handles;
 static struct GNUNET_TESTBED_Operation *stats_op;
 
 /**
- * How many events have happened
+ * Operation to get peer ids.
  */
-static int ok;
+struct MeshPeer *peers;
 
 /**
- * Number of events expected to conclude the test successfully.
+ * Peer ids counter.
  */
-static int ok_goal;
+static unsigned int p_ids;
 
 /**
- * Size of each test packet
+ * Total number of peers.
  */
-size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
+static unsigned long long peers_total;
 
 /**
- * Operation to get peer ids.
+ * Number of currently running peers.
  */
-struct MeshPeer peers[TOTAL_PEERS];
+static unsigned long long peers_running;
 
 /**
- * Peer ids counter.
+ * Number of peers doing pings.
  */
-static unsigned int p_ids;
-
-/**
- * Total number of currently running peers.
- */
-static unsigned long long peers_running;
+static unsigned long long peers_pinging;
 
 /**
  * Test context (to shut down).
@@ -169,12 +221,41 @@ static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
  */
 static GNUNET_SCHEDULER_TaskIdentifier test_task;
 
+/**
+ * Round number.
+ */
+static unsigned int current_round;
+
+
+/**
+ * Do preconnect? (Each peer creates a tunnel to one other peer).
+ */
+static int do_warmup;
+
+/**
+ * Warmup progress.
+ */
+static unsigned int peers_warmup;
 
 /**
  * Flag to notify callbacks not to generate any new traffic anymore.
  */
 static int test_finished;
 
+
+/**
+ * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
+ *
+ * Testcase continues when the root receives confirmation of connected peers,
+ * on callback funtion ch.
+ *
+ * @param cls Closure (unsued).
+ * @param tc Task Context.
+ */
+static void
+start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
 /**
  * Calculate a random delay.
  *
@@ -212,6 +293,21 @@ get_index (struct MeshPeer *peer)
 static void
 show_end_data (void)
 {
+  struct MeshPeer *peer;
+  unsigned int i;
+  unsigned int j;
+
+  for (i = 0; i < number_rounds; i++)
+  {
+    for (j = 0; j < peers_pinging; j++)
+    {
+      peer = &peers[j];
+      FPRINTF (stdout,
+               "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n",
+               i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)),
+               peer->pings[i], peer->pongs[i]);
+    }
+  }
 }
 
 
@@ -242,15 +338,33 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned int i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "disconnecting mesh service of peers, called from line %ld\n",
-              line);
+              "disconnecting mesh service, called from line %ld\n", line);
   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  for (i = 0; i < TOTAL_PEERS; i++)
+  for (i = 0; i < peers_total; i++)
   {
-    GNUNET_TESTBED_operation_done (peers[i].op);
-    GNUNET_MESH_channel_destroy (peers[i].ch);
+    if (NULL != peers[i].op)
+      GNUNET_TESTBED_operation_done (peers[i].op);
+
+    if (peers[i].up != GNUNET_YES)
+      continue;
+
+    if (NULL != peers[i].ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
+      GNUNET_MESH_channel_destroy (peers[i].ch);
+    }
+    if (NULL != peers[i].warmup_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: warmup channel %p\n",
+                  i, peers[i].warmup_ch);
+      GNUNET_MESH_channel_destroy (peers[i].warmup_ch);
+    }
     if (NULL != peers[i].incoming_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
+                  i, peers[i].incoming_ch);
       GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
+    }
   }
   GNUNET_MESH_TEST_cleanup (test_ctx);
   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
@@ -338,9 +452,8 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
     return;
 
-  disconnect_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
-  stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
+  stats_op = GNUNET_TESTBED_get_statistics (peers_total, testbed_handles,
                                             NULL, NULL,
                                             stats_iterator, stats_cont, NULL);
 }
@@ -377,8 +490,9 @@ adjust_running_peers (unsigned int target)
   unsigned int i;
   unsigned int r;
 
-  GNUNET_assert (target <= TOTAL_PEERS);
+  GNUNET_assert (target <= peers_total);
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
   if (target > peers_running)
   {
     delta = target - peers_running;
@@ -393,13 +507,34 @@ adjust_running_peers (unsigned int target)
   for (i = 0; i < delta; i++)
   {
     do {
-      r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
-    } while (peers[r].up == run);
+      r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                    peers_total - peers_pinging);
+      r += peers_pinging;
+    } while (peers[r].up == run || NULL != peers[r].incoming);
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
                 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
+
+    if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
+      GNUNET_SCHEDULER_cancel (peers[r].ping_task);
+    peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
+
+    peers[r].up = run;
+
+    if (NULL != peers[r].ch)
+      GNUNET_MESH_channel_destroy (peers[r].ch);
+    peers[r].ch = NULL;
+    if (NULL != peers[r].dest)
+    {
+      if (NULL != peers[r].dest->incoming_ch)
+        GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
+      peers[r].dest->incoming_ch = NULL;
+    }
+
     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
                                              "mesh", NULL, NULL, run);
     GNUNET_break (NULL != op);
+    peers_running += run ? 1 : -1;
+    GNUNET_assert (peers_running > 0);
   }
 }
 
@@ -411,27 +546,27 @@ adjust_running_peers (unsigned int target)
  * @param tc Task context.
  */
 static void
-next_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  long round = (long) cls;
-
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
-  if (0.0 == rounds[round])
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", current_round);
+  if (0.0 == rounds[current_round])
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
     return;
   }
-  adjust_running_peers (rounds[round] * TOTAL_PEERS);
+  adjust_running_peers (rounds[current_round] * peers_total);
+  current_round++;
 
-  GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_round, (void *) (round + 1));
+  GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
 }
 
 
 /**
- * Transmit ready callback.
+ * Transmit ping callback.
  *
  * @param cls Closure (peer for PING, NULL for PONG).
  * @param size Size of the tranmist buffer.
@@ -440,7 +575,34 @@ next_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return Number of bytes written to buf.
  */
 static size_t
-tmt_rdy (void *cls, size_t size, void *buf);
+tmt_rdy_ping (void *cls, size_t size, void *buf);
+
+
+/**
+ * Transmit pong callback.
+ *
+ * @param cls Closure (copy of PING message, to be freed).
+ * @param size Size of the buffer we have.
+ * @param buf Buffer to copy data to.
+ */
+static size_t
+tmt_rdy_pong (void *cls, size_t size, void *buf)
+{
+  struct MeshPingMessage *ping = cls;
+  struct MeshPingMessage *pong;
+
+  if (0 == size || NULL == buf)
+  {
+    GNUNET_free (ping);
+    return 0;
+  }
+  pong = (struct MeshPingMessage *) buf;
+  memcpy (pong, ping, sizeof (*ping));
+  pong->header.type = htons (PONG);
+
+  GNUNET_free (ping);
+  return sizeof (*ping);
+}
 
 
 /**
@@ -457,16 +619,16 @@ ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
 
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
-      || GNUNET_YES == test_finished
-      || 0 != peer->timestamp.abs_value_us)
+      || GNUNET_YES == test_finished)
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
-              get_index (peer), get_index (peer->dest));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u (%u)\n",
+              get_index (peer), get_index (peer->dest), peer->data_sent);
 
   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                     size_payload, &tmt_rdy, peer);
+                                     sizeof (struct MeshPingMessage),
+                                     &tmt_rdy_ping, peer);
 }
 
 /**
@@ -476,30 +638,34 @@ ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param tc Task context.
  */
 static void
-pong (struct GNUNET_MESH_Channel *channel)
+pong (struct GNUNET_MESH_Channel *channel, const struct MeshPingMessage *ping)
 {
+  struct MeshPingMessage *copy;
+
+  copy = GNUNET_new (struct MeshPingMessage);
+  memcpy (copy, ping, sizeof (*ping));
   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                     size_payload, &tmt_rdy, NULL);
+                                     sizeof (struct MeshPingMessage),
+                                     &tmt_rdy_pong, copy);
 }
 
 
 /**
- * Transmit ready callback
+ * Transmit ping callback
  *
- * @param cls Closure (peer for PING, NULL for PONG).
+ * @param cls Closure (peer).
  * @param size Size of the buffer we have.
  * @param buf Buffer to copy data to.
  */
 static size_t
-tmt_rdy (void *cls, size_t size, void *buf)
+tmt_rdy_ping (void *cls, size_t size, void *buf)
 {
   struct MeshPeer *peer = (struct MeshPeer *) cls;
-  struct GNUNET_MessageHeader *msg = buf;
-  uint32_t *data;
+  struct MeshPingMessage *msg = buf;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
-  if (size < size_payload || NULL == buf)
+  if (size < sizeof (struct MeshPingMessage) || NULL == buf)
   {
     GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -508,23 +674,17 @@ tmt_rdy (void *cls, size_t size, void *buf)
 
     return 0;
   }
-  msg->size = htons (size);
-  if (NULL == peer)
-  {
-    msg->type = htons (PONG);
-    return sizeof (*msg);
-  }
-
-  msg->type = htons (PING);
-  data = (uint32_t *) &msg[1];
-  *data = htonl (peer->data_sent);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
-  peer->data_sent++;
-  peer->timestamp = GNUNET_TIME_absolute_get ();
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending: msg %d\n", peer->data_sent);
+  msg->header.size = htons (size);
+  msg->header.type = htons (PING);
+  msg->counter = htonl (peer->data_sent++);
+  msg->round_number = htonl (current_round);
+  msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
+  peer->pings[current_round]++;
   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
                                                   &ping, peer);
 
-  return size_payload;
+  return sizeof (struct MeshPingMessage);
 }
 
 
@@ -548,7 +708,7 @@ ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
   GNUNET_MESH_receive_done (channel);
   if (GNUNET_NO == test_finished)
-    pong (channel);
+    pong (channel, (struct MeshPingMessage *) message);
 
   return GNUNET_OK;
 }
@@ -571,26 +731,29 @@ pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
 {
   long n = (long) cls;
   struct MeshPeer *peer;
+  struct MeshPingMessage *msg;
+  struct GNUNET_TIME_Absolute send_time;
   struct GNUNET_TIME_Relative latency;
+  unsigned int r /* Ping round */;
+  float delta;
 
   GNUNET_MESH_receive_done (channel);
   peer = &peers[n];
 
-  GNUNET_break (0 != peer->timestamp.abs_value_us);
-  latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
-  FPRINTF (stderr, "%ld latency: %s\n",
-           n, GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
+  msg = (struct MeshPingMessage *) message;
 
-  if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
-  {
-    peer->timestamp = GNUNET_TIME_absolute_get ();
-    peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
-                                                    &ping, peer);
-  }
-  else
-  {
-    peer->timestamp.abs_value_us = 0;
-  }
+  send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp);
+  latency = GNUNET_TIME_absolute_get_duration (send_time);
+  r = ntohl (msg->round_number);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n",
+              get_index (peer), get_index (peer->dest), ntohl (msg->counter),
+              GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
+
+  /* Online variance calculation */
+  peer->pongs[r]++;
+  delta = latency.rel_value_us - peer->mean[r];
+  peer->mean[r] = peer->mean[r] + delta/peer->pongs[r];
+  peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]);
 
   return GNUNET_OK;
 }
@@ -600,8 +763,8 @@ pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
  * Handlers, for diverse services
  */
 static struct GNUNET_MESH_MessageHandler handlers[] = {
-  {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
-  {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
+  {&ping_handler, PING, sizeof (struct MeshPingMessage)},
+  {&pong_handler, PONG, sizeof (struct MeshPingMessage)},
   {NULL, 0, 0}
 };
 
@@ -628,16 +791,22 @@ incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
 
   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
   GNUNET_assert (NULL != peer);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
-  peers[n].incoming_ch = channel;
-
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+  if (NULL == peers[n].incoming)
   {
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
-                                                    &disconnect_mesh_peers,
-                                                    (void *) __LINE__);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %3u: %u <= %u\n",
+                peers_warmup, n, get_index (peer));
+    peers_warmup++;
+    if (peers_warmup < peers_total)
+      return NULL;
+    test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                              &start_test, NULL);
+    return NULL;
   }
+  GNUNET_assert (peer == peers[n].incoming);
+  GNUNET_assert (peer->dest == &peers[n]);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
+              n, get_index (peer), channel);
+  peers[n].incoming_ch = channel;
 
   return NULL;
 }
@@ -656,20 +825,22 @@ channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
                  void *channel_ctx)
 {
   long n = (long) cls;
+  struct MeshPeer *peer = &peers[n];
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Incoming channel disconnected at peer %ld\n", n);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
-
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
-                                                (void *) __LINE__);
-  }
+              "Channel %p disconnected at peer %ld\n", channel, n);
+  if (peer->ch == channel)
+    peer->ch = NULL;
 }
 
 
+/**
+ * Select a random peer that has no incoming channel
+ *
+ * @param peer ID of the peer connecting. NULL if irrelevant (warmup).
+ *
+ * @return Random peer not yet connected to.
+ */
 static struct MeshPeer *
 select_random_peer (struct MeshPeer *peer)
 {
@@ -677,7 +848,7 @@ select_random_peer (struct MeshPeer *peer)
 
   do
   {
-    r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
+    r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total);
   } while (NULL != peers[r].incoming);
   peers[r].incoming = peer;
 
@@ -704,30 +875,62 @@ start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
 
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-  disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
-                                                  &disconnect_mesh_peers,
-                                                  (void *) __LINE__);
-
   flags = GNUNET_MESH_OPTION_DEFAULT;
-  for (i = 0; i < TOTAL_PEERS; i++)
+  for (i = 0; i < peers_pinging; i++)
   {
-
     peers[i].dest = select_random_peer (&peers[i]);
     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
                                               &peers[i].dest->id,
                                               1, flags);
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u\n",
-                i, get_index (peers[i].dest));
+    if (NULL == peers[i].ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i);
+      GNUNET_MESH_TEST_cleanup (test_ctx);
+      return;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
+                i, get_index (peers[i].dest), peers[i].ch);
     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
                                                        &ping, &peers[i]);
   }
-  peers_running = TOTAL_PEERS;
-  GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_round, NULL);
+  peers_running = peers_total;
+  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+    GNUNET_SCHEDULER_cancel (disconnect_task);
+  disconnect_task =
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(round_time,
+                                                                number_rounds + 1),
+                                  &disconnect_mesh_peers,
+                                  (void *) __LINE__);
+  GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
 }
 
 
+/**
+ * Do warmup: create some channels to spread information about the topology.
+ */
+static void
+warmup (void)
+{
+  struct MeshPeer *peer;
+  unsigned int i;
+
+  for (i = 0; i < peers_total; i++)
+  {
+    peer = select_random_peer (NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
+                i, get_index (peer));
+    peers[i].warmup_ch =
+      GNUNET_MESH_channel_create (peers[i].mesh, NULL, &peer->id,
+                                  1, GNUNET_MESH_OPTION_DEFAULT);
+    if (NULL == peers[i].warmup_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i);
+      GNUNET_MESH_TEST_cleanup (test_ctx);
+      return;
+    }
+  }
+}
+
 /**
  * Callback to be called when the requested peer information is available
  *
@@ -757,10 +960,19 @@ peer_id_cb (void *cls,
   GNUNET_break (GNUNET_OK ==
                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+
+  GNUNET_TESTBED_operation_done (peers[n].op);
+  peers[n].op = NULL;
+
   p_ids++;
-  if (p_ids < TOTAL_PEERS)
+  if (p_ids < peers_total)
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
+  if (do_warmup)
+  {
+    warmup();
+    return; /* start_test from incoming_channel */
+  }
   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                             &start_test, NULL);
 }
@@ -784,9 +996,8 @@ tmain (void *cls,
   unsigned long i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
-  ok = 0;
   test_ctx = ctx;
-  GNUNET_assert (TOTAL_PEERS == num_peers);
+  GNUNET_assert (peers_total == num_peers);
   peers_running = num_peers;
   testbed_handles = testbed_peers;
   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
@@ -794,7 +1005,7 @@ tmain (void *cls,
                                                   (void *) __LINE__);
   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                                   &shutdown_task, NULL);
-  for (i = 0; i < TOTAL_PEERS; i++)
+  for (i = 0; i < peers_total; i++)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
     peers[i].up = GNUNET_YES;
@@ -818,26 +1029,52 @@ main (int argc, char *argv[])
   static uint32_t ports[2];
   const char *config_file;
 
-  config_file = "test_mesh.conf";
+  config_file = ".profiler.conf";
 
-  ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
+  if (4 > argc)
+  {
+    fprintf (stderr, "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", argv[0]);
+    fprintf (stderr, "example: %s 30s 16 1 Y\n", argv[0]);
+    return 1;
+  }
+
+  if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[1], &round_time))
+  {
+    fprintf (stderr, "%s is not a valid time\n", argv[1]);
+    return 1;
+  }
+
+  peers_total = atoll (argv[2]);
+  if (2 > peers_total)
+  {
+    fprintf (stderr, "%s peers is not valid (> 2)\n", argv[1]);
+    return 1;
+  }
+  peers = GNUNET_malloc (sizeof (struct MeshPeer) * peers_total);
+
+  peers_pinging = atoll (argv[3]);
+
+  if (peers_total < 2 * peers_pinging)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "not enough peers, total should be > 2 * peers_pinging\n");
+    return 1;
+  }
+
+  do_warmup = (5 > argc || argv[4][0] == 'N');
+
+  ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, GNUNET_YES);
   GNUNET_assert (NULL != ids);
   p_ids = 0;
   test_finished = GNUNET_NO;
   ports[0] = 1;
   ports[1] = 0;
-  GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
+  GNUNET_MESH_TEST_run ("mesh-profiler", config_file, peers_total,
                         &tmain, NULL, /* tmain cls */
                         &incoming_channel, &channel_cleaner,
                         handlers, ports);
+  GNUNET_free (peers);
 
-  if (ok_goal > ok)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "FAILED! (%d/%d)\n", ok, ok_goal);
-    return 1;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
   return 0;
 }