aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-10-26 11:41:50 +0000
committerChristian Grothoff <christian@grothoff.org>2011-10-26 11:41:50 +0000
commit8a8da32a2b0c3f46fe5439651c4546d0760ea867 (patch)
tree559a382e0042d51706cf56f5e0a20774d39c2623 /src
parent6a1a61e90c1f483687cefa1d5838982d04cb995f (diff)
downloadgnunet-8a8da32a2b0c3f46fe5439651c4546d0760ea867.tar.gz
gnunet-8a8da32a2b0c3f46fe5439651c4546d0760ea867.zip
expanding gnunet-transport towards a benchmarking tool
Diffstat (limited to 'src')
-rw-r--r--src/transport/gnunet-transport.c204
1 files changed, 196 insertions, 8 deletions
diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c
index d3b8aabca..10994fbf8 100644
--- a/src/transport/gnunet-transport.c
+++ b/src/transport/gnunet-transport.c
@@ -20,7 +20,7 @@
20 20
21/** 21/**
22 * @file src/transport/gnunet-transport.c 22 * @file src/transport/gnunet-transport.c
23 * @brief Tool to help configure the transports. 23 * @brief Tool to help configure, measure and control the transport subsystem.
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * 25 *
26 * This utility can be used to test if a transport mechanism for 26 * This utility can be used to test if a transport mechanism for
@@ -36,11 +36,183 @@ static char *cpid;
36 36
37static struct GNUNET_TRANSPORT_Handle *handle; 37static struct GNUNET_TRANSPORT_Handle *handle;
38 38
39static int benchmark_send;
40
41static int benchmark_receive;
42
43static int ret;
44
45static unsigned long long traffic_received;
46
47static unsigned long long traffic_sent;
48
49static struct GNUNET_TIME_Absolute start_time;
50
51static struct GNUNET_TRANSPORT_TransmitHandle *th;
52
53static struct GNUNET_PeerIdentity pid;
54
55static GNUNET_SCHEDULER_TaskIdentifier end;
56
57
58/**
59 * Shutdown, print statistics.
60 */
39static void 61static void
40do_disconnect (void *cls, 62do_disconnect (void *cls,
41 const struct GNUNET_SCHEDULER_TaskContext *tc) 63 const struct GNUNET_SCHEDULER_TaskContext *tc)
42{ 64{
43 GNUNET_TRANSPORT_disconnect (handle); 65 struct GNUNET_TIME_Relative duration;
66
67 if (NULL != th)
68 {
69 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
70 th = NULL;
71 }
72 GNUNET_TRANSPORT_disconnect (handle);
73 if (benchmark_receive)
74 {
75 duration = GNUNET_TIME_absolute_get_duration (start_time);
76 fprintf (stderr,
77 "Received %llu bytes/s\n",
78 traffic_received / (1+duration.rel_value));
79 }
80 if (benchmark_send)
81 {
82 duration = GNUNET_TIME_absolute_get_duration (start_time);
83 fprintf (stderr,
84 "Transmitted %llu bytes/s\n",
85 traffic_sent / (1+duration.rel_value));
86 }
87}
88
89
90/**
91 * Function called to notify a client about the socket
92 * begin ready to queue more data. "buf" will be
93 * NULL and "size" zero if the socket was closed for
94 * writing in the meantime.
95 *
96 * @param cls closure
97 * @param size number of bytes available in buf
98 * @param buf where the callee should write the message
99 * @return number of bytes written to buf
100 */
101static size_t
102transmit_data (void *cls, size_t size,
103 void *buf)
104{
105 struct GNUNET_MessageHeader *m = buf;
106
107 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
108 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
109 m->size = ntohs (size);
110 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
111 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
112 traffic_sent += size;
113 th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
114 &pid,
115 32 * 1024,
116 0,
117 GNUNET_TIME_UNIT_FOREVER_REL,
118 &transmit_data, NULL);
119 return size;
120}
121
122
123/**
124 * Function called to notify transport users that another
125 * peer connected to us.
126 *
127 * @param cls closure
128 * @param peer the peer that connected
129 * @param ats performance data
130 * @param ats_count number of entries in ats (excluding 0-termination)
131 */
132static void
133notify_connect (void *cls,
134 const struct GNUNET_PeerIdentity
135 * peer,
136 const struct
137 GNUNET_ATS_Information
138 * ats, uint32_t ats_count)
139{
140 if (0 != memcmp (&pid,
141 peer,
142 sizeof (struct GNUNET_PeerIdentity)))
143 return;
144 ret = 0;
145 if (benchmark_send)
146 {
147 start_time = GNUNET_TIME_absolute_get ();
148 th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
149 peer,
150 32 * 1024,
151 0,
152 GNUNET_TIME_UNIT_FOREVER_REL,
153 &transmit_data, NULL);
154 }
155 else
156 {
157 /* all done, terminate instantly */
158 GNUNET_SCHEDULER_cancel (end);
159 end = GNUNET_SCHEDULER_add_now (&do_disconnect,
160 NULL);
161 }
162}
163
164
165/**
166 * Function called to notify transport users that another
167 * peer disconnected from us.
168 *
169 * @param cls closure
170 * @param peer the peer that disconnected
171 */
172static void
173notify_disconnect (void *cls,
174 const struct
175 GNUNET_PeerIdentity * peer)
176{
177 if ( (0 == memcmp (&pid,
178 peer,
179 sizeof (struct GNUNET_PeerIdentity))) &&
180 (NULL != th) )
181 {
182 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
183 th = NULL;
184 GNUNET_SCHEDULER_cancel (end);
185 end = GNUNET_SCHEDULER_add_now (&do_disconnect,
186 NULL);
187 }
188}
189
190
191/**
192 * Function called by the transport for each received message.
193 *
194 * @param cls closure
195 * @param peer (claimed) identity of the other peer
196 * @param message the message
197 * @param ats performance data
198 * @param ats_count number of entries in ats
199 */
200static void
201notify_receive (void *cls,
202 const struct
203 GNUNET_PeerIdentity * peer,
204 const struct
205 GNUNET_MessageHeader *
206 message,
207 const struct
208 GNUNET_ATS_Information
209 * ats, uint32_t ats_count)
210{
211 if (! benchmark_receive)
212 return;
213 if (traffic_received == 0)
214 start_time = GNUNET_TIME_absolute_get ();
215 traffic_received += ntohs (message->size);
44} 216}
45 217
46 218
@@ -56,11 +228,19 @@ static void
56run (void *cls, char *const *args, const char *cfgfile, 228run (void *cls, char *const *args, const char *cfgfile,
57 const struct GNUNET_CONFIGURATION_Handle *cfg) 229 const struct GNUNET_CONFIGURATION_Handle *cfg)
58{ 230{
59 struct GNUNET_PeerIdentity pid; 231 if (benchmark_send && (NULL == cpid))
232 {
233 fprintf (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
234 "-s", "-C");
235 return;
236 }
60 if (NULL != cpid) 237 if (NULL != cpid)
61 { 238 {
239 ret = 1;
62 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, 240 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
63 NULL, NULL, NULL); 241 &notify_receive,
242 &notify_connect,
243 &notify_disconnect);
64 if (GNUNET_OK != 244 if (GNUNET_OK !=
65 GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)) 245 GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
66 { 246 {
@@ -71,9 +251,11 @@ run (void *cls, char *const *args, const char *cfgfile,
71 return; 251 return;
72 } 252 }
73 GNUNET_TRANSPORT_try_connect (handle, &pid); 253 GNUNET_TRANSPORT_try_connect (handle, &pid);
74 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 254 end = GNUNET_SCHEDULER_add_delayed (benchmark_send
75 &do_disconnect, 255 ? GNUNET_TIME_UNIT_FOREVER_REL
76 NULL); 256 : GNUNET_TIME_UNIT_SECONDS,
257 &do_disconnect,
258 NULL);
77 } 259 }
78} 260}
79 261
@@ -82,15 +264,21 @@ int
82main (int argc, char *const *argv) 264main (int argc, char *const *argv)
83{ 265{
84 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 266 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
267 {'b', "benchmark", NULL,
268 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
269 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
85 {'C', "connect", "PEER", 270 {'C', "connect", "PEER",
86 gettext_noop ("try to connect to the given peer"), 271 gettext_noop ("try to connect to the given peer"),
87 1, &GNUNET_GETOPT_set_string, &cpid}, 272 1, &GNUNET_GETOPT_set_string, &cpid},
273 {'s', "send", NULL,
274 gettext_noop ("send data for benchmarking to the other peer (until CTRL-C)"),
275 0, &GNUNET_GETOPT_set_one, &benchmark_send},
88 GNUNET_GETOPT_OPTION_END 276 GNUNET_GETOPT_OPTION_END
89 }; 277 };
90 return (GNUNET_OK == 278 return (GNUNET_OK ==
91 GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", 279 GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
92 gettext_noop ("Direct access to transport service."), 280 gettext_noop ("Direct access to transport service."),
93 options, &run, NULL)) ? 0 : 1; 281 options, &run, NULL)) ? ret : 1;
94} 282}
95 283
96 284