2 This file is part of GNUnet.
3 Copyright (C) 2012, 2018, 2020 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/perf_mq.c
23 * @brief benchmark for mq
24 * @author Florian Dold
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
31 #define NUM_TRANSMISSIONS 1000000
34 * How long does the receiver take per message?
36 #define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply ( \
37 GNUNET_TIME_UNIT_MILLISECONDS, 1)
39 static unsigned int received_cnt;
42 GNUNET_NETWORK_STRUCT_BEGIN
46 struct GNUNET_MessageHeader header;
47 uint32_t x GNUNET_PACKED;
50 GNUNET_NETWORK_STRUCT_END
52 static int global_ret;
54 static struct GNUNET_SCHEDULER_Task *task;
56 static struct GNUNET_MQ_Handle *cmq;
60 do_shutdown (void *cls)
65 GNUNET_SCHEDULER_cancel (task);
70 GNUNET_MQ_destroy (cmq);
77 * Generic error handler, called with the appropriate
78 * error code and the same closure specified at the creation of
80 * Not every message queue implementation supports an error handler.
83 * @param error error code
87 enum GNUNET_MQ_Error error)
91 GNUNET_SCHEDULER_shutdown ();
96 handle_dummy (void *cls,
97 const struct MyMessage *msg)
99 struct GNUNET_SERVICE_Client *c = cls;
101 GNUNET_SERVICE_client_continue (c);
102 if (received_cnt != ntohl (msg->x))
106 GNUNET_SCHEDULER_shutdown ();
113 handle_dummy2 (void *cls,
114 const struct MyMessage *msg)
116 struct GNUNET_SERVICE_Client *c = cls;
118 GNUNET_SERVICE_client_continue (c);
119 if (NUM_TRANSMISSIONS != received_cnt)
124 GNUNET_SCHEDULER_shutdown ();
133 * Function called whenever MQ has sent a message.
136 notify_sent_cb (void *cls)
138 static unsigned int seen;
139 unsigned int *cnt = cls;
145 GNUNET_SCHEDULER_shutdown ();
149 task = GNUNET_SCHEDULER_add_now (&do_send,
157 static unsigned int i = 0;
159 struct GNUNET_MQ_Envelope *env;
163 if (NUM_TRANSMISSIONS == i)
165 env = GNUNET_MQ_msg (m,
166 GNUNET_MESSAGE_TYPE_DUMMY2);
171 cnt = GNUNET_new (unsigned int);
173 env = GNUNET_MQ_msg (m,
174 GNUNET_MESSAGE_TYPE_DUMMY);
175 GNUNET_MQ_notify_sent (env,
186 * Start running the actual test.
188 * @param cls closure passed to #GNUNET_SERVICE_MAIN
189 * @param cfg configuration to use for this service
190 * @param sh handle to the newly create service
194 const struct GNUNET_CONFIGURATION_Handle *cfg,
195 struct GNUNET_SERVICE_Handle *sh)
197 struct GNUNET_MQ_MessageHandler ch[] = {
198 GNUNET_MQ_handler_end ()
203 cmq = GNUNET_CLIENT_connect (cfg,
208 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
210 task = GNUNET_SCHEDULER_add_now (&do_send,
216 * Callback to be called when a client connects to the service.
218 * @param cls closure for the service
219 * @param c the new client that connected to the service
220 * @param mq the message queue used to send messages to the client
221 * @return the client-specific (`internal') closure
224 connect_cb (void *cls,
225 struct GNUNET_SERVICE_Client *c,
226 struct GNUNET_MQ_Handle *mq)
235 * Callback to be called when a client disconnected from the service
237 * @param cls closure for the service
238 * @param c the client that disconnected
239 * @param internal_cls the client-specific (`internal') closure
242 disconnect_cb (void *cls,
243 struct GNUNET_SERVICE_Client *c,
253 main (int argc, char **argv)
255 struct GNUNET_TIME_Absolute start;
256 char *test_argv[] = {
257 (char *) "test_client",
259 "test_client_data.conf",
262 struct GNUNET_MQ_MessageHandler mh[] = {
263 GNUNET_MQ_hd_fixed_size (dummy,
264 GNUNET_MESSAGE_TYPE_DUMMY,
267 GNUNET_MQ_hd_fixed_size (dummy2,
268 GNUNET_MESSAGE_TYPE_DUMMY2,
271 GNUNET_MQ_handler_end ()
276 GNUNET_log_setup ("perf-mq",
279 start = GNUNET_TIME_absolute_get ();
281 GNUNET_SERVICE_run_ (3,
284 GNUNET_SERVICE_OPTION_NONE,
291 printf ("Scheduler perf took %s\n",
292 GNUNET_STRINGS_relative_time_to_string (
293 GNUNET_TIME_absolute_get_duration (start),
295 GAUGER ("UTIL", "Scheduler",
296 received_cnt / 1024 / (1
297 + GNUNET_TIME_absolute_get_duration
298 (start).rel_value_us / 1000LL), "kmsg/ms");