| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <wait.h> |
| 28 |
|
| 29 |
#include <benchmark/benchmark.h> |
| 30 |
|
| 31 |
#include "proton/connection_driver.h" |
| 32 |
#include "proton/engine.h" |
| 33 |
#include "proton/log.h" |
| 34 |
#include "proton/message.h" |
| 35 |
#include "proton/transport.h" |
| 36 |
#include <proton/listener.h> |
| 37 |
#include <proton/netaddr.h> |
| 38 |
#include <proton/proactor.h> |
| 39 |
#include <proton/sasl.h> |
| 40 |
|
| 41 |
|
| 42 |
// variant of the receive.c proactor example |
| 43 |
// borrowed things from the quiver arrow, too |
| 44 |
|
| 45 |
#define MAX_SIZE 1024 |
| 46 |
|
| 47 |
typedef char str[MAX_SIZE]; |
| 48 |
|
| 49 |
typedef struct app_data_t { |
| 50 |
str container_id; |
| 51 |
pn_rwbytes_t message_buffer{}; |
| 52 |
int message_count; |
| 53 |
int received = 0; |
| 54 |
pn_message_t *message; |
| 55 |
int sent = 0; |
| 56 |
pn_rwbytes_t msgout; |
| 57 |
int credit_window = 5000; |
| 58 |
int acknowledged = 0; |
| 59 |
int closed = 0; |
| 60 |
} app_data_t; |
| 61 |
|
| 62 |
static void decode_message(pn_delivery_t *dlv); |
| 63 |
static void check_condition(pn_event_t *e, pn_condition_t *cond); |
| 64 |
static void shovel(pn_connection_driver_t &sender, pn_connection_driver_t &receiver); |
| 65 |
|
| 66 |
static void handle_receiver(app_data_t *app, pn_event_t *event); |
| 67 |
static void handle_sender(app_data_t *app, pn_event_t *event); |
| 68 |
|
| 69 |
const bool VERBOSE = false; |
| 70 |
const bool ERRORS = true; |
| 71 |
|
| 72 |
// useful for debugging |
| 73 |
static void turn_on_transport_logging(pn_transport_t& transport) { |
| 74 |
pn_log_enable(true); |
| 75 |
pn_transport_trace(&transport, |
| 76 |
PN_TRACE_FRM | PN_TRACE_EVT); |
| 77 |
} |
| 78 |
|
| 79 |
static void BM_InitCloseSender(benchmark::State &state) { |
| 80 |
if (VERBOSE) |
| 81 |
printf("BEGIN BM_InitCloseSender\n"); |
| 82 |
|
| 83 |
for (auto _ : state) { |
| 84 |
pn_connection_driver_t sender; |
| 85 |
if (pn_connection_driver_init(&sender, NULL, NULL) != 0) { |
| 86 |
printf("sender: pn_connection_driver_init failed\n"); |
| 87 |
exit(1); |
| 88 |
} |
| 89 |
|
| 90 |
pn_connection_driver_close(&sender); |
| 91 |
pn_connection_driver_destroy(&sender); |
| 92 |
} |
| 93 |
|
| 94 |
if (VERBOSE) |
| 95 |
printf("END BM_InitCloseSender\n"); |
| 96 |
} |
| 97 |
|
| 98 |
BENCHMARK(BM_InitCloseSender)->Unit(benchmark::kMicrosecond); |
| 99 |
|
| 100 |
static void BM_InitCloseReceiver(benchmark::State &state) { |
| 101 |
if (VERBOSE) |
| 102 |
printf("BEGIN BM_InitCloseReceiver\n"); |
| 103 |
|
| 104 |
for (auto _ : state) { |
| 105 |
pn_connection_driver_t receiver; |
| 106 |
if (pn_connection_driver_init(&receiver, NULL, NULL) != 0) { |
| 107 |
printf("receiver: pn_connection_driver_init failed\n"); |
| 108 |
exit(1); |
| 109 |
} |
| 110 |
|
| 111 |
pn_connection_driver_close(&receiver); |
| 112 |
pn_connection_driver_destroy(&receiver); |
| 113 |
} |
| 114 |
|
| 115 |
if (VERBOSE) |
| 116 |
printf("END BM_InitCloseReceiver\n"); |
| 117 |
} |
| 118 |
|
| 119 |
BENCHMARK(BM_InitCloseReceiver)->Unit(benchmark::kMicrosecond); |
| 120 |
|
| 121 |
// sends single message and closes the connection |
| 122 |
static void BM_EstablishConnection(benchmark::State &state) { |
| 123 |
if (VERBOSE) |
| 124 |
printf("BEGIN BM_EstablishConnection\n"); |
| 125 |
|
| 126 |
for (auto _ : state) { |
| 127 |
app_data_t app = {}; |
| 128 |
app.message_count = 1; |
| 129 |
app.message = pn_message(); |
| 130 |
sprintf(app.container_id, "%s:%06x", "BM_EstablishConnection", |
| 131 |
rand() & 0xffffff); |
| 132 |
|
| 133 |
pn_connection_driver_t receiver; |
| 134 |
if (pn_connection_driver_init(&receiver, NULL, NULL) != 0) { |
| 135 |
printf("receiver: pn_connection_driver_init failed\n"); |
| 136 |
exit(1); |
| 137 |
} |
| 138 |
|
| 139 |
pn_connection_driver_t sender; |
| 140 |
if (pn_connection_driver_init(&sender, NULL, NULL) != 0) { |
| 141 |
printf("sender: pn_connection_driver_init failed\n"); |
| 142 |
exit(1); |
| 143 |
} |
| 144 |
|
| 145 |
do { |
| 146 |
pn_event_t *event; |
| 147 |
while ((event = pn_connection_driver_next_event(&sender)) != NULL) { |
| 148 |
handle_sender(&app, event); |
| 149 |
} |
| 150 |
shovel(sender, receiver); |
| 151 |
while ((event = pn_connection_driver_next_event(&receiver)) != NULL) { |
| 152 |
handle_receiver(&app, event); |
| 153 |
} |
| 154 |
shovel(receiver, sender); |
| 155 |
} while (app.closed < 2); // until both sender and receiver are closed |
| 156 |
|
| 157 |
pn_message_free(app.message); |
| 158 |
|
| 159 |
pn_connection_driver_close(&receiver); |
| 160 |
pn_connection_driver_close(&sender); |
| 161 |
|
| 162 |
shovel(receiver, sender); |
| 163 |
shovel(sender, receiver); |
| 164 |
|
| 165 |
// this can take long time, up to 500 ms |
| 166 |
pn_connection_driver_destroy(&receiver); |
| 167 |
pn_connection_driver_destroy(&sender); |
| 168 |
} |
| 169 |
|
| 170 |
state.SetLabel("connections"); |
| 171 |
state.SetItemsProcessed(state.iterations()); |
| 172 |
|
| 173 |
if (VERBOSE) |
| 174 |
printf("END BM_EstablishConnection\n"); |
| 175 |
} |
| 176 |
|
| 177 |
BENCHMARK(BM_EstablishConnection)->Unit(benchmark::kMicrosecond); |
| 178 |
|
| 179 |
static void BM_SendReceiveMessages(benchmark::State &state) { |
| 180 |
if (VERBOSE) |
| 181 |
printf("BEGIN BM_SendReceiveMessages\n"); |
| 182 |
|
| 183 |
app_data_t app = {}; |
| 184 |
app.message_count = -1; // unlimited |
| 185 |
app.message = pn_message(); |
| 186 |
app.credit_window = state.range(0); |
| 187 |
sprintf(app.container_id, "%s:%06x", "BM_SendReceiveMessages", |
| 188 |
rand() & 0xffffff); |
| 189 |
|
| 190 |
pn_connection_driver_t receiver; |
| 191 |
if (pn_connection_driver_init(&receiver, NULL, NULL) != 0) { |
| 192 |
printf("receiver: pn_connection_driver_init failed\n"); |
| 193 |
exit(1); |
| 194 |
} |
| 195 |
|
| 196 |
pn_connection_driver_t sender; |
| 197 |
if (pn_connection_driver_init(&sender, NULL, NULL) != 0) { |
| 198 |
printf("sender: pn_connection_driver_init failed\n"); |
| 199 |
exit(1); |
| 200 |
} |
| 201 |
|
| 202 |
for (auto _ : state) { |
| 203 |
pn_event_t *event; |
| 204 |
while ((event = pn_connection_driver_next_event(&sender)) != NULL) { |
| 205 |
handle_sender(&app, event); |
| 206 |
} |
| 207 |
shovel(sender, receiver); |
| 208 |
while ((event = pn_connection_driver_next_event(&receiver)) != NULL) { |
| 209 |
handle_receiver(&app, event); |
| 210 |
} |
| 211 |
shovel(receiver, sender); |
| 212 |
} |
| 213 |
|
| 214 |
pn_connection_driver_close(&receiver); |
| 215 |
pn_connection_driver_close(&sender); |
| 216 |
|
| 217 |
shovel(receiver, sender); |
| 218 |
shovel(sender, receiver); |
| 219 |
|
| 220 |
// this can take long time, up to 500 ms |
| 221 |
pn_connection_driver_destroy(&receiver); |
| 222 |
pn_connection_driver_destroy(&sender); |
| 223 |
|
| 224 |
state.SetLabel("messages"); |
| 225 |
state.SetItemsProcessed(app.acknowledged); |
| 226 |
|
| 227 |
if (VERBOSE) |
| 228 |
printf("END BM_SendReceiveMessages\n"); |
| 229 |
} |
| 230 |
|
| 231 |
BENCHMARK(BM_SendReceiveMessages) |
| 232 |
->RangeMultiplier(3) |
| 233 |
->Range(1, 200000) |
| 234 |
->ArgName("creditWindow") |
| 235 |
->Arg(1000) |
| 236 |
->Unit(benchmark::kMillisecond); |
| 237 |
|
| 238 |
|
| 239 |
///* Create a message with a map { "sequence" : number } encode it and return |
| 240 |
/// the encoded buffer. */ |
| 241 |
static void send_message(app_data_t *app, pn_link_t *sender) { |
| 242 |
/* Construct a message with the map { "sequence": 42 } */ |
| 243 |
pn_data_t *body; |
| 244 |
pn_message_clear(app->message); |
| 245 |
body = pn_message_body(app->message); |
| 246 |
pn_data_put_int(pn_message_id(app->message), |
| 247 |
app->sent); /* Set the message_id also */ |
| 248 |
pn_data_put_map(body); |
| 249 |
pn_data_enter(body); |
| 250 |
pn_data_put_string(body, pn_bytes(sizeof("sequence") - 1, "sequence")); |
| 251 |
pn_data_put_int(body, 42); /* The sequence number */ |
| 252 |
pn_data_exit(body); |
| 253 |
if (pn_message_send(app->message, sender, &app->message_buffer) < 0) { |
| 254 |
fprintf(stderr, "error sending message: %s\n", |
| 255 |
pn_error_text(pn_message_error(app->message))); |
| 256 |
exit(1); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static void handle_sender(app_data_t *app, pn_event_t *event) { |
| 261 |
switch (pn_event_type(event)) { |
| 262 |
|
| 263 |
case PN_CONNECTION_INIT: { |
| 264 |
pn_connection_t *c = pn_event_connection(event); |
| 265 |
pn_connection_set_container(c, "sendercid"); |
| 266 |
pn_connection_open(c); |
| 267 |
pn_session_t *s = pn_session(c); |
| 268 |
pn_session_open(s); |
| 269 |
|
| 270 |
pn_link_t *l = pn_sender(s, "my_sender"); |
| 271 |
pn_terminus_set_address(pn_link_target(l), "example"); |
| 272 |
pn_link_set_snd_settle_mode(l, PN_SND_UNSETTLED); |
| 273 |
pn_link_set_rcv_settle_mode(l, PN_RCV_FIRST); |
| 274 |
pn_link_open(l); |
| 275 |
} break; |
| 276 |
|
| 277 |
case PN_LINK_FLOW: { |
| 278 |
if (VERBOSE) |
| 279 |
printf("BEGIN handle_sender: PN_LINK_FLOW\n"); |
| 280 |
/* The peer has given us some credit, now we can send messages */ |
| 281 |
pn_link_t *sender = pn_event_link(event); |
| 282 |
while (pn_link_credit(sender) > 0 && app->sent != app->message_count) { |
| 283 |
++app->sent; |
| 284 |
/* Use sent counter as unique delivery tag. */ |
| 285 |
pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent))); |
| 286 |
send_message(app, sender); |
| 287 |
} |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
case PN_DELIVERY: { |
| 292 |
/* We received acknowledgement from the peer that a message was delivered. */ |
| 293 |
pn_delivery_t *d = pn_event_delivery(event); |
| 294 |
if (pn_delivery_remote_state(d) == PN_ACCEPTED) { |
| 295 |
if (VERBOSE) |
| 296 |
printf("got PN_ACCEPTED\n"); |
| 297 |
if (++app->acknowledged == app->message_count) { |
| 298 |
if (VERBOSE) |
| 299 |
printf("%d messages sent and acknowledged\n", app->acknowledged); |
| 300 |
pn_connection_close(pn_event_connection(event)); |
| 301 |
/* Continue handling events till we receive TRANSPORT_CLOSED */ |
| 302 |
} |
| 303 |
} else { |
| 304 |
fprintf(stderr, "unexpected delivery state %d\n", (int) pn_delivery_remote_state(d)); |
| 305 |
pn_connection_close(pn_event_connection(event)); |
| 306 |
exit(EXIT_FAILURE); |
| 307 |
} |
| 308 |
break; |
| 309 |
} |
| 310 |
|
| 311 |
case PN_CONNECTION_REMOTE_OPEN: |
| 312 |
pn_connection_open(pn_event_connection(event)); /* Complete the open */ |
| 313 |
break; |
| 314 |
|
| 315 |
case PN_SESSION_REMOTE_OPEN: |
| 316 |
pn_session_open(pn_event_session(event)); |
| 317 |
break; |
| 318 |
|
| 319 |
case PN_TRANSPORT_ERROR: |
| 320 |
check_condition(event, pn_transport_condition(pn_event_transport(event))); |
| 321 |
pn_connection_close(pn_event_connection(event)); |
| 322 |
break; |
| 323 |
|
| 324 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 325 |
check_condition(event, |
| 326 |
pn_connection_remote_condition(pn_event_connection(event))); |
| 327 |
pn_connection_close(pn_event_connection(event)); |
| 328 |
break; |
| 329 |
|
| 330 |
case PN_SESSION_REMOTE_CLOSE: |
| 331 |
check_condition(event, |
| 332 |
pn_session_remote_condition(pn_event_session(event))); |
| 333 |
pn_connection_close(pn_event_connection(event)); |
| 334 |
break; |
| 335 |
|
| 336 |
case PN_LINK_REMOTE_CLOSE: |
| 337 |
case PN_LINK_REMOTE_DETACH: |
| 338 |
check_condition(event, pn_link_remote_condition(pn_event_link(event))); |
| 339 |
pn_connection_close(pn_event_connection(event)); |
| 340 |
break; |
| 341 |
|
| 342 |
case PN_TRANSPORT_CLOSED: |
| 343 |
app->closed++; |
| 344 |
break; |
| 345 |
|
| 346 |
default: |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
static void handle_receiver(app_data_t *app, pn_event_t *event) { |
| 352 |
switch (pn_event_type(event)) { |
| 353 |
|
| 354 |
case PN_CONNECTION_INIT: { |
| 355 |
pn_connection_t *c = pn_event_connection(event); |
| 356 |
pn_connection_set_container(c, app->container_id); |
| 357 |
// pn_connection_open(c); |
| 358 |
pn_session_t *s = pn_session(c); |
| 359 |
pn_session_open(s); |
| 360 |
|
| 361 |
pn_link_t *l = pn_receiver(s, "my_receiver"); |
| 362 |
pn_terminus_set_address(pn_link_source(l), "example"); |
| 363 |
pn_link_open(l); |
| 364 |
} break; |
| 365 |
|
| 366 |
case PN_LINK_REMOTE_OPEN: { |
| 367 |
pn_link_t *l = pn_event_link(event); |
| 368 |
pn_terminus_t *t = pn_link_target(l); |
| 369 |
pn_terminus_t *rt = pn_link_remote_target(l); |
| 370 |
pn_terminus_set_address(t, pn_terminus_get_address(rt)); |
| 371 |
pn_link_open(l); |
| 372 |
if (pn_link_is_receiver(l)) { |
| 373 |
pn_link_flow(l, app->credit_window); |
| 374 |
} |
| 375 |
break; |
| 376 |
} |
| 377 |
|
| 378 |
case PN_DELIVERY: { |
| 379 |
/* A message has been received */ |
| 380 |
pn_link_t *link = NULL; |
| 381 |
pn_delivery_t *dlv = pn_event_delivery(event); |
| 382 |
if (pn_delivery_readable(dlv) && !pn_delivery_partial(dlv)) { |
| 383 |
link = pn_delivery_link(dlv); |
| 384 |
decode_message(dlv); |
| 385 |
/* Accept the delivery */ |
| 386 |
pn_delivery_update(dlv, PN_ACCEPTED); |
| 387 |
/* done with the delivery, move to the next and free it */ |
| 388 |
pn_link_advance(link); |
| 389 |
pn_delivery_settle(dlv); /* dlv is now freed */ |
| 390 |
} |
| 391 |
pn_link_flow(link, app->credit_window - pn_link_credit(link)); |
| 392 |
} break; |
| 393 |
|
| 394 |
case PN_CONNECTION_REMOTE_OPEN: |
| 395 |
pn_connection_open(pn_event_connection(event)); /* Complete the open */ |
| 396 |
break; |
| 397 |
|
| 398 |
case PN_SESSION_REMOTE_OPEN: |
| 399 |
pn_session_open(pn_event_session(event)); |
| 400 |
break; |
| 401 |
|
| 402 |
case PN_TRANSPORT_ERROR: |
| 403 |
check_condition(event, pn_transport_condition(pn_event_transport(event))); |
| 404 |
pn_connection_close(pn_event_connection(event)); |
| 405 |
break; |
| 406 |
|
| 407 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 408 |
check_condition(event, |
| 409 |
pn_connection_remote_condition(pn_event_connection(event))); |
| 410 |
pn_connection_close(pn_event_connection(event)); |
| 411 |
break; |
| 412 |
|
| 413 |
case PN_SESSION_REMOTE_CLOSE: |
| 414 |
check_condition(event, |
| 415 |
pn_session_remote_condition(pn_event_session(event))); |
| 416 |
pn_connection_close(pn_event_connection(event)); |
| 417 |
break; |
| 418 |
|
| 419 |
case PN_LINK_REMOTE_CLOSE: |
| 420 |
case PN_LINK_REMOTE_DETACH: |
| 421 |
check_condition(event, pn_link_remote_condition(pn_event_link(event))); |
| 422 |
pn_connection_close(pn_event_connection(event)); |
| 423 |
break; |
| 424 |
|
| 425 |
case PN_TRANSPORT_CLOSED: |
| 426 |
app->closed++; |
| 427 |
break; |
| 428 |
|
| 429 |
default: |
| 430 |
break; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
static void check_condition(pn_event_t *e, pn_condition_t *cond) { |
| 435 |
if (VERBOSE) |
| 436 |
printf("beginning check_condition\n"); |
| 437 |
if (pn_condition_is_set(cond)) { |
| 438 |
if (VERBOSE || ERRORS) |
| 439 |
fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)), |
| 440 |
pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
///* Copies output from first connection driver into input of the second. */ |
| 445 |
static void shovel(pn_connection_driver_t &sender, pn_connection_driver_t &receiver) { |
| 446 |
pn_bytes_t wbuf = pn_connection_driver_write_buffer(&receiver); |
| 447 |
if (wbuf.size == 0) { |
| 448 |
pn_connection_driver_write_done(&receiver, 0); |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
pn_rwbytes_t rbuf = pn_connection_driver_read_buffer(&sender); |
| 453 |
if (rbuf.start == NULL) { |
| 454 |
printf("shovel: rbuf.start is null\n"); |
| 455 |
fflush(stdout); |
| 456 |
fflush(stderr); |
| 457 |
exit(1); |
| 458 |
} |
| 459 |
|
| 460 |
size_t s = rbuf.size < wbuf.size ? rbuf.size : wbuf.size; |
| 461 |
memcpy(rbuf.start, wbuf.start, s); |
| 462 |
|
| 463 |
pn_connection_driver_read_done(&sender, s); |
| 464 |
pn_connection_driver_write_done(&receiver, s); |
| 465 |
} |
| 466 |
|
| 467 |
static void decode_message(pn_delivery_t *dlv) { |
| 468 |
static char buffer[MAX_SIZE]; |
| 469 |
ssize_t len; |
| 470 |
// try to decode the message body |
| 471 |
if (pn_delivery_pending(dlv) < MAX_SIZE) { |
| 472 |
// read in the raw data |
| 473 |
len = pn_link_recv(pn_delivery_link(dlv), buffer, MAX_SIZE); |
| 474 |
if (len > 0) { |
| 475 |
// decode it into a proton message |
| 476 |
pn_message_t *m = pn_message(); |
| 477 |
if (PN_OK == pn_message_decode(m, buffer, len)) { |
| 478 |
pn_string_t *s = pn_string(NULL); |
| 479 |
pn_inspect(pn_message_body(m), s); |
| 480 |
if (VERBOSE) |
| 481 |
printf("%s\n", pn_string_get(s)); |
| 482 |
pn_free(s); |
| 483 |
} |
| 484 |
pn_message_free(m); |
| 485 |
} |
| 486 |
} |
| 487 |
} |