RabbitMQ流控测试,客户端代码(ack)

#include "amqp_tcp_socket.h"
#include "amqp.h"
#include "amqp_framing.h"

#include 
#include 
#include 

#include 

using namespace std;

// g++ -o rabbitmq_producer -g -Wall -Werror -Winline -Wwrite-strings -D RABBITMQ_CLIENT_PRODUCER -std=c++11  rabbitmq-client.cpp  -I ../librabbitmq/  ./librabbitmq/librabbitmq.a  -lrt
// g++ -o rabbitmq_consumer -g -Wall -Werror -Winline -Wwrite-strings -D RABBITMQ_CLIENT_CONSUMER -std=c++11  rabbitmq-client.cpp  -I ../librabbitmq/  ./librabbitmq/librabbitmq.a  -lrt


unsigned int period = 3600;
unsigned int speed = 1;

string m_strIp          = "192.168.45.131";
int m_uPort             = 5672;
string m_strUserName    = "admin";
string m_strPassWord    = "admin123";

string m_strVHost       = "test_host";
string m_strExchange    = "test_exchange";
string m_strRoutingKey  = "test_routing-key";
string m_strQueue       = "test_queue";

unsigned int channel_id     = 100;
unsigned int prefetch_count = 2000;

amqp_connection_state_t m_MQconn;
amqp_basic_properties_t m_Props;

unsigned int count_ = 0;
unsigned int loop_ = 0;
struct timeval delay_;

bool connect_rabbitmq()
{
    m_MQconn = amqp_new_connection();

    if (NULL == m_MQconn)
    {
        printf("Call amqp_new_connection failed.\n");
        return false;
    }

    amqp_socket_t* pSocket = amqp_tcp_socket_new(m_MQconn);

    if (NULL == pSocket)
    {
        printf("Call amqp_tcp_socket_new failed.\n");
        return false;
    }

    int iStatus = amqp_socket_open(pSocket, m_strIp.data(), m_uPort);

    if (iStatus < 0)
    {
        printf("Call amqp_socket_open failed.\n");
        return false;
    }

    amqp_rpc_reply_t rLonin = amqp_login(m_MQconn,
                                            m_strVHost.data(),
                                            0,
                                            131072,
                                            0,
                                            AMQP_SASL_METHOD_PLAIN,
                                            m_strUserName.data(),
                                            m_strPassWord.data());

    if (AMQP_RESPONSE_NORMAL != rLonin.reply_type)
    {
        printf("Call amqp_login failed. reply_type:%d.\n", rLonin.reply_type);
        return false;
    }

    amqp_channel_open(m_MQconn, channel_id);

    amqp_rpc_reply_t rOpenChannel =  amqp_get_rpc_reply(m_MQconn);

    if (AMQP_RESPONSE_NORMAL != rOpenChannel.reply_type)
    {
        printf("Call amqp_channel_open failed. reply_type:%d.\n", rOpenChannel.reply_type);
        return false;
    }

    m_Props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
    m_Props.content_type = amqp_cstring_bytes("text/plain");
    m_Props.delivery_mode = 2;

    #if defined(RABBITMQ_CLIENT_CONSUMER)

    amqp_basic_qos(m_MQconn, channel_id, 0, prefetch_count, 0);

    amqp_rpc_reply_t rSetQos = amqp_get_rpc_reply(m_MQconn);

    if (AMQP_RESPONSE_NORMAL != rSetQos.reply_type)
    {
        printf("Call amqp_basic_qos failed. reply_type:%d.\n", rSetQos.reply_type);
        return false;
    }

    amqp_basic_consume(m_MQconn,
                        channel_id,
                        amqp_cstring_bytes(m_strQueue.data()),
                        amqp_cstring_bytes(to_string(channel_id).data()),
                        0,
                        0,
                        0,
                        amqp_empty_table);

    amqp_rpc_reply_t rConsume = amqp_get_rpc_reply(m_MQconn);

    if (AMQP_RESPONSE_NORMAL != rConsume.reply_type)
    {
        printf("Call amqp_basic_consume failed. reply_type:%d.\n", rConsume.reply_type);
        return false;
    }
    #endif

    printf("connect rabbitmq success.\n");
    return true;
}

bool close_rabbitmq()
{
    amqp_channel_close(m_MQconn, 1, AMQP_REPLY_SUCCESS);
    amqp_connection_close(m_MQconn, AMQP_REPLY_SUCCESS);
    int ret = amqp_destroy_connection(m_MQconn);

    if (AMQP_STATUS_OK != ret)
    {
        printf("Call amqp_destroy_connection failed. retcode:%d.\n", ret);
        return false;
    }

    return true;
}

bool base_publish(const string& strData)
{
    int iRet = amqp_basic_publish(m_MQconn,
                                    channel_id,
                                    amqp_cstring_bytes(m_strExchange.data()),
                                    amqp_cstring_bytes(m_strRoutingKey.data()),
                                    0,
                                    0,
                                    &m_Props,
                                    amqp_cstring_bytes(strData.data()));
    if (iRet < 0)
    {
        printf("Call amqp_basic_publish failed. return code:%d.\n", iRet);
        close_rabbitmq();
        return false;
    }

    return true;
}

int base_consumer()
{
    amqp_maybe_release_buffers(m_MQconn);
    amqp_envelope_t envelope;

    amqp_rpc_reply_t ret = amqp_consume_message(m_MQconn, &envelope, NULL, 0);

    switch (ret.reply_type)
    {
        case AMQP_RESPONSE_NORMAL:
        {
            break;
        }

        case AMQP_RESPONSE_LIBRARY_EXCEPTION:
        {
            if (ret.library_error == AMQP_STATUS_TIMEOUT)
            {
                printf("Call amqp_consume_message [-0x%x:%s].\n",
                    0 - ret.library_error,
                    amqp_error_string2(ret.library_error));

                amqp_destroy_envelope(&envelope);
                return false;
            }
            else
            {
                printf("Call amqp_consume_message [-0x%x:%s].\n",
                    0 - ret.library_error,
                    amqp_error_string2(ret.library_error));

                amqp_destroy_envelope(&envelope);
                return false;
            }
        }

        default:
        {
            printf("Call amqp_consume_message failed. "
                "ret.reply_type[%d] ret.reply.id[%x] ret.library_error[-0x%x:%s].\n",
                ret.reply_type,
                ret.reply.id,
                0 - ret.library_error,
                amqp_error_string2(ret.library_error));

            amqp_destroy_envelope(&envelope);
            return false;
        }
    }

    int result = amqp_basic_ack(m_MQconn,
                                envelope.channel,
                                envelope.delivery_tag,
                                0);
    if (result != 0)
    {
        printf("Call amqp_basic_ack failed.\n");
        return false;
    }

    amqp_destroy_envelope(&envelope);
    return true;
}

void publish()
{
    string str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    for (unsigned int i = 0; i < period; ++i)
    {
        for (unsigned int j = 0; j < loop_; ++j)
        {
            if (!base_publish(str))
            {
                return;
            }
            else
            {
                count_++;
                printf("publish message success %u--------------count:%u.\n", j, count_);
            }

            struct timeval delay = delay_;
            select(0, NULL,NULL, NULL, &delay);
        }

        printf("---%u seconds---\n", i);
    }
}

void consume()
{
    for (unsigned int i = 0; i < period; ++i)
    {
        for (unsigned int j = 0; j < loop_; ++j)
        {
            if (!base_consumer())
            {
            }
            else
            {
                count_++;
                printf("consumer message success %u--------------count:%u.\n", j, count_);
            }

            struct timeval delay = delay_;
            select(0, NULL,NULL, NULL, &delay);
        }
        printf("---%u seconds---\n", i);
    }
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("Argument error.\n");
        return -1;
    }

    speed = atoi(argv[1]);
    loop_ = speed;
    delay_.tv_usec = (1000 / speed ) * 1000;

    connect_rabbitmq();

    #if defined(RABBITMQ_CLIENT_PRODUCER)
    publish();
    #endif

    #if defined(RABBITMQ_CLIENT_CONSUMER)
    consume();
    #endif

    return 0;
}

你可能感兴趣的:(技术)