Blame examples/amqp_producer.c

Packit 12c978
/*
Packit 12c978
 * ***** BEGIN LICENSE BLOCK *****
Packit 12c978
 * Version: MIT
Packit 12c978
 *
Packit 12c978
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
Packit 12c978
 * Alan Antonuk. All Rights Reserved.
Packit 12c978
 *
Packit 12c978
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
Packit 12c978
 * All Rights Reserved.
Packit 12c978
 *
Packit 12c978
 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
Packit 12c978
 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
Packit 12c978
 *
Packit 12c978
 * Permission is hereby granted, free of charge, to any person
Packit 12c978
 * obtaining a copy of this software and associated documentation
Packit 12c978
 * files (the "Software"), to deal in the Software without
Packit 12c978
 * restriction, including without limitation the rights to use, copy,
Packit 12c978
 * modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 12c978
 * of the Software, and to permit persons to whom the Software is
Packit 12c978
 * furnished to do so, subject to the following conditions:
Packit 12c978
 *
Packit 12c978
 * The above copyright notice and this permission notice shall be
Packit 12c978
 * included in all copies or substantial portions of the Software.
Packit 12c978
 *
Packit 12c978
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 12c978
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 12c978
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 12c978
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 12c978
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 12c978
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 12c978
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 12c978
 * SOFTWARE.
Packit 12c978
 * ***** END LICENSE BLOCK *****
Packit 12c978
 */
Packit 12c978
Packit 12c978
#include <stdint.h>
Packit 12c978
#include <stdio.h>
Packit 12c978
#include <stdlib.h>
Packit 12c978
#include <string.h>
Packit 12c978
Packit 12c978
#include <amqp.h>
Packit 12c978
#include <amqp_tcp_socket.h>
Packit 12c978
Packit 12c978
#include "utils.h"
Packit 12c978
Packit 12c978
#define SUMMARY_EVERY_US 1000000
Packit 12c978
Packit 12c978
static void send_batch(amqp_connection_state_t conn, char const *queue_name,
Packit 12c978
                       int rate_limit, int message_count) {
Packit 12c978
  uint64_t start_time = now_microseconds();
Packit 12c978
  int i;
Packit 12c978
  int sent = 0;
Packit 12c978
  int previous_sent = 0;
Packit 12c978
  uint64_t previous_report_time = start_time;
Packit 12c978
  uint64_t next_summary_time = start_time + SUMMARY_EVERY_US;
Packit 12c978
Packit 12c978
  char message[256];
Packit 12c978
  amqp_bytes_t message_bytes;
Packit 12c978
Packit 12c978
  for (i = 0; i < (int)sizeof(message); i++) {
Packit 12c978
    message[i] = i & 0xff;
Packit 12c978
  }
Packit 12c978
Packit 12c978
  message_bytes.len = sizeof(message);
Packit 12c978
  message_bytes.bytes = message;
Packit 12c978
Packit 12c978
  for (i = 0; i < message_count; i++) {
Packit 12c978
    uint64_t now = now_microseconds();
Packit 12c978
Packit 12c978
    die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes("amq.direct"),
Packit 12c978
                                    amqp_cstring_bytes(queue_name), 0, 0, NULL,
Packit 12c978
                                    message_bytes),
Packit 12c978
                 "Publishing");
Packit 12c978
    sent++;
Packit 12c978
    if (now > next_summary_time) {
Packit 12c978
      int countOverInterval = sent - previous_sent;
Packit 12c978
      double intervalRate =
Packit 12c978
          countOverInterval / ((now - previous_report_time) / 1000000.0);
Packit 12c978
      printf("%d ms: Sent %d - %d since last report (%d Hz)\n",
Packit 12c978
             (int)(now - start_time) / 1000, sent, countOverInterval,
Packit 12c978
             (int)intervalRate);
Packit 12c978
Packit 12c978
      previous_sent = sent;
Packit 12c978
      previous_report_time = now;
Packit 12c978
      next_summary_time += SUMMARY_EVERY_US;
Packit 12c978
    }
Packit 12c978
Packit 12c978
    while (((i * 1000000.0) / (now - start_time)) > rate_limit) {
Packit 12c978
      microsleep(2000);
Packit 12c978
      now = now_microseconds();
Packit 12c978
    }
Packit 12c978
  }
Packit 12c978
Packit 12c978
  {
Packit 12c978
    uint64_t stop_time = now_microseconds();
Packit 12c978
    int total_delta = (int)(stop_time - start_time);
Packit 12c978
Packit 12c978
    printf("PRODUCER - Message count: %d\n", message_count);
Packit 12c978
    printf("Total time, milliseconds: %d\n", total_delta / 1000);
Packit 12c978
    printf("Overall messages-per-second: %g\n",
Packit 12c978
           (message_count / (total_delta / 1000000.0)));
Packit 12c978
  }
Packit 12c978
}
Packit 12c978
Packit 12c978
int main(int argc, char const *const *argv) {
Packit 12c978
  char const *hostname;
Packit 12c978
  int port, status;
Packit 12c978
  int rate_limit;
Packit 12c978
  int message_count;
Packit 12c978
  amqp_socket_t *socket = NULL;
Packit 12c978
  amqp_connection_state_t conn;
Packit 12c978
Packit 12c978
  if (argc < 5) {
Packit 12c978
    fprintf(stderr,
Packit 12c978
            "Usage: amqp_producer host port rate_limit message_count\n");
Packit 12c978
    return 1;
Packit 12c978
  }
Packit 12c978
Packit 12c978
  hostname = argv[1];
Packit 12c978
  port = atoi(argv[2]);
Packit 12c978
  rate_limit = atoi(argv[3]);
Packit 12c978
  message_count = atoi(argv[4]);
Packit 12c978
Packit 12c978
  conn = amqp_new_connection();
Packit 12c978
Packit 12c978
  socket = amqp_tcp_socket_new(conn);
Packit 12c978
  if (!socket) {
Packit 12c978
    die("creating TCP socket");
Packit 12c978
  }
Packit 12c978
Packit 12c978
  status = amqp_socket_open(socket, hostname, port);
Packit 12c978
  if (status) {
Packit 12c978
    die("opening TCP socket");
Packit 12c978
  }
Packit 12c978
Packit 12c978
  die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
Packit 12c978
                               "guest", "guest"),
Packit 12c978
                    "Logging in");
Packit 12c978
  amqp_channel_open(conn, 1);
Packit 12c978
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
Packit 12c978
Packit 12c978
  send_batch(conn, "test queue", rate_limit, message_count);
Packit 12c978
Packit 12c978
  die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
Packit 12c978
                    "Closing channel");
Packit 12c978
  die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
Packit 12c978
                    "Closing connection");
Packit 12c978
  die_on_error(amqp_destroy_connection(conn), "Ending connection");
Packit 12c978
  return 0;
Packit 12c978
}