summaryrefslogtreecommitdiff
path: root/cpp/perftest/topic_publisher.cpp
blob: 53b2c20775dc11a06bf6c0e434258105d69a03eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

/**
 * Test hack code -- will upadte into a harness, work in progress
 */

#include "qpid/QpidError.h"
#include "qpid/client/ClientChannel.h"
#include "qpid/client/Connection.h"
#include "qpid/client/ClientExchange.h"
#include "qpid/client/MessageListener.h"
#include "qpid/client/ClientQueue.h"
#include "qpid/sys/Monitor.h"
#include <unistd.h>
#include "qpid/sys/Time.h"
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace qpid::client;
using namespace qpid::sys;
using std::string;


bool done = 0;


class Listener : public MessageListener{


void set_time() {
    timespec ts;
    if (::clock_gettime(CLOCK_REALTIME, &ts))
	std::cout << "Error" << std::endl;
    _ts_sec = ts.tv_sec;
    _ts_nsec = ts.tv_nsec;
  }

void print_time() {
    timespec ts;
    if (::clock_gettime(CLOCK_REALTIME, &ts))
	std::cout << "Error" << std::endl;
    std::cout << "Total Time:" << ts.tv_sec-_ts_sec <<"." <<ts.tv_nsec - _ts_nsec << std::endl;
    float rate = messageCount/(ts.tv_sec-_ts_sec);
    std::cout << "returned Messages:" << messageCount  << std::endl;
    std::cout << "round trip Rate:" << rate  << std::endl;
  }

  time_t _ts_sec;	  ///< Timestamp of journal initilailization
  u_int32_t _ts_nsec;     ///< Timestamp of journal initilailization
  int messageCount;

 public:
 

     Listener(int mcount): messageCount(mcount) {
        set_time();
     }
	
    virtual void received(Message& msg) {
	print_time();
	std::cout << "Message: " << msg.getData() << std::endl;
        done = 1;
     }
     
    ~Listener() { };
    
    
    
    
};


int main() {
    Connection connection;
    Channel channel;
    Message msg;
    try {
        connection.open("127.0.0.1", 5672, "guest", "guest", "/test");
        connection.openChannel(channel);
        channel.start();

 	string queueControl = "control";
        Queue response(queueControl);
        channel.declareQueue(response);
        channel.bind(Exchange::STANDARD_TOPIC_EXCHANGE, response, queueControl);
        
	string  queueName ="queue01";
	string  queueNameC =queueName+"-1";

	// create publish queue
	Queue publish(queueName);
        channel.declareQueue(publish);
        channel.bind(Exchange::STANDARD_TOPIC_EXCHANGE, publish, queueName);
  
  	// create completion queue
	Queue completion(queueNameC);
        channel.declareQueue(completion);
        channel.bind(Exchange::STANDARD_TOPIC_EXCHANGE, completion, queueNameC);
      
	// pass queue name
	msg.setData(queueName);
	channel.publish(msg, Exchange::STANDARD_TOPIC_EXCHANGE, queueControl);

	std::cout << "Setup return queue:"<< queueNameC << std::endl;
	
	int count = 100000;
        Listener listener(count);
        channel.consume(completion, queueNameC, &listener);
	std::cout << "Setup consumer:"<< queueNameC << std::endl;




  time_t _ts_sec;	  ///< Timestamp of journal initilailization
  u_int32_t _ts_nsec;     ///< Timestamp of journal initilailization
    timespec ts;
    if (::clock_gettime(CLOCK_REALTIME, &ts))
	std::cout << "Error" << std::endl;
    _ts_sec = ts.tv_sec;
    _ts_nsec = ts.tv_nsec;


	
	for (int i=0; i<count; i++) {
	    msg.setData("Message 0123456789 ");
	    channel.publish(msg, Exchange::STANDARD_TOPIC_EXCHANGE, queueName);
	}

    if (::clock_gettime(CLOCK_REALTIME, &ts))
	std::cout << "Error" << std::endl;
    std::cout << "publish Time:" << ts.tv_sec-_ts_sec <<"." <<ts.tv_nsec - _ts_nsec << std::endl;
    float rate = count/(ts.tv_sec-_ts_sec);
    std::cout << "publish Messages:" << count  << std::endl;
    std::cout << "publish Rate:" << rate  << std::endl;


	msg.setData(queueName);  // last message to queue.
	channel.publish(msg, Exchange::STANDARD_TOPIC_EXCHANGE, queueName);
	
	std::cout << "wait:"<< queueNameC << std::endl;
	
        while (!done)
            sleep(1);

 
         channel.close();
         connection.close();
        return 0;
    } catch(const std::exception& error) {
        std::cout << error.what() << std::endl;
    }
    return 1;
}