- client-side implementation of peer queries
[oweals/gnunet.git] / src / ats-tests / gnunet-ats-sim.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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  * @file ats-tests/gnunet-ats-sim.c
22  * @brief ats traffic simulator: this tool uses the ats-test library to setup a
23  * topology and generate traffic between these peers. The traffic description
24  * is loaded from a experiment description file
25  * @author Christian Grothoff
26  * @author Matthias Wachs
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_core_service.h"
33 #include "ats-testing.h"
34
35 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36
37 static struct BenchmarkPeer *masters_p;
38 static struct BenchmarkPeer *slaves_p;
39
40 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
41
42 struct Experiment *e;
43 struct LoggingHandle *l;
44
45 static void
46 evaluate (struct GNUNET_TIME_Relative duration_total)
47 {
48   int c_m;
49   int c_s;
50   unsigned int duration;
51   struct BenchmarkPeer *mp;
52   struct BenchmarkPartner *p;
53
54   unsigned int kb_sent_sec;
55   double kb_sent_percent;
56   unsigned int kb_recv_sec;
57   double kb_recv_percent;
58   unsigned int rtt;
59
60
61   duration = (duration_total.rel_value_us / (1000 * 1000));
62   for (c_m = 0; c_m < e->num_masters; c_m++)
63   {
64     mp = &masters_p[c_m];
65     fprintf (stderr,
66         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
67         mp->no, mp->total_bytes_sent / 1024, duration,
68         (mp->total_bytes_sent / 1024) / duration,
69         mp->total_bytes_received / 1024, duration,
70         (mp->total_bytes_received / 1024) / duration);
71
72     for (c_s = 0; c_s < e->num_slaves; c_s++)
73     {
74       p = &mp->partners[c_s];
75
76       kb_sent_sec = 0;
77       kb_recv_sec = 0;
78       kb_sent_percent = 0.0;
79       kb_recv_percent = 0.0;
80       rtt = 0;
81
82       if (duration > 0)
83       {
84           kb_sent_sec = (p->bytes_sent / 1024) / duration;
85           kb_recv_sec = (p->bytes_received / 1024) / duration;
86       }
87
88       if (mp->total_bytes_sent > 0)
89           kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
90       if (mp->total_bytes_received > 0)
91           kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
92       if (1000 * p->messages_sent > 0)
93           rtt = p->total_app_rtt / (1000 * p->messages_sent);
94       fprintf (stderr,
95           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
96           (mp->pref_partner == p->dest) ? '*' : ' ',
97           mp->no, p->dest->no,
98           kb_sent_sec, kb_sent_percent,
99                   kb_recv_sec, kb_recv_percent);
100       fprintf (stderr,
101           "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
102           (mp->pref_partner == p->dest) ? '*' : ' ',
103           mp->no, p->dest->no, rtt);
104     }
105   }
106 }
107
108 static void
109 do_shutdown ()
110 {
111   /* timeout */
112   if (NULL != e)
113   {
114     GNUNET_ATS_TEST_experimentation_stop (e);
115     e = NULL;
116   }
117   GNUNET_ATS_TEST_shutdown_topology ();
118 }
119
120 static void
121 transport_recv_cb (void *cls,
122                    const struct GNUNET_PeerIdentity * peer,
123                    const struct GNUNET_MessageHeader * message)
124 {
125
126 }
127
128 static void
129 log_request__cb (void *cls, const struct GNUNET_HELLO_Address *address,
130     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
131     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
132     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
133 {
134   if (NULL != l)
135     GNUNET_ATS_TEST_logging_now (l);
136 }
137
138 static void
139 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
140 {
141   if (GNUNET_OK == success)
142     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
143         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
144   else
145     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
146   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
147   {
148     GNUNET_SCHEDULER_cancel (timeout_task);
149     timeout_task = GNUNET_SCHEDULER_NO_TASK;
150   }
151   /* Stop logging */
152   GNUNET_ATS_TEST_logging_stop (l);
153   evaluate (duration);
154
155   /* Stop traffic generation */
156   GNUNET_ATS_TEST_generate_traffic_stop_all();
157   /* Clean up experiment */
158   GNUNET_ATS_TEST_experimentation_stop (e);
159   e = NULL;
160
161   /* Shutdown topology */
162   GNUNET_ATS_TEST_shutdown_topology ();
163 }
164
165 static void
166 episode_done_cb (struct Episode *ep)
167 {
168   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
169 }
170
171 static void topology_setup_done (void *cls,
172     struct BenchmarkPeer *masters,
173     struct BenchmarkPeer *slaves)
174 {
175   int c_m;
176   int c_s;
177   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");
178
179   masters_p = masters;
180   slaves_p = slaves;
181
182   l = GNUNET_ATS_TEST_logging_start (GNUNET_TIME_UNIT_SECONDS, e->name,
183       masters_p, e->num_masters);
184   GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb);
185
186   for (c_m = 0; c_m < e->num_masters; c_m++)
187   {
188       for (c_s = 0; c_s < e->num_slaves; c_s++)
189       {
190         /* Generate maximum traffic to all peers */
191         GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
192             &masters[c_m].partners[c_s],
193             10000,
194             GNUNET_TIME_UNIT_FOREVER_REL);
195       }
196   }
197
198   timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
199       e->max_duration), &do_shutdown, NULL);
200 }
201
202
203 int
204 main (int argc, char *argv[])
205 {
206   GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
207   if (argc < 2)
208   {
209     fprintf (stderr, "No experiment given...\n");
210     return 1;
211   }
212
213   fprintf (stderr, "Loading experiment `%s' \n", argv[1]);
214   e = GNUNET_ATS_TEST_experimentation_load (argv[1]);
215   if (NULL == e)
216   {
217     fprintf (stderr, "Invalid experiment\n");
218     return 1;
219   }
220   if (0 == e->num_episodes)
221   {
222     fprintf (stderr, "No episodes included\n");
223     return 1;
224   }
225
226   /* Setup a topology with */
227   GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
228       e->num_slaves,
229       e->num_masters,
230       GNUNET_NO,
231       &topology_setup_done,
232       NULL,
233       &transport_recv_cb,
234       &log_request__cb);
235   return 0;
236 }
237 /* end of file gnunet-ats-sim.c */