/* * * 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. * */ #include "TestOptions.h" #include "qpid/client/Session_0_10.h" #include "qpid/client/SubscriptionManager.h" #include "qpid/client/Connection.h" #include "qpid/client/Message.h" #include "qpid/sys/Time.h" #include #include using namespace std; using namespace qpid; using namespace client; using namespace sys; struct Opts : public TestOptions { bool listen; bool publish; int count; int size; bool durable; int consumers; std::string mode; int autoAck; Opts() : listen(false), publish(false), count(500000), size(64), consumers(1), mode("shared"), autoAck(100) { addOptions() ("listen", optValue(listen), "Consume messages.") ("publish", optValue(publish), "Produce messages.") ("count", optValue(count, "N"), "Messages to send.") ("size", optValue(size, "BYTES"), "Size of messages.") ("durable", optValue(durable, "N"), "Publish messages as durable.") ("consumers", optValue(consumers, "N"), "Number of consumers.") ("mode", optValue(mode, "shared|fanout|topic"), "consume mode") ("auto-ack", optValue(autoAck, "N"), "ack every N messages."); } }; Opts opts; enum Mode { SHARED, FANOUT, TOPIC }; Mode mode; struct ListenThread : public Runnable { Thread thread; void run(); }; struct PublishThread : public Runnable { Thread thread; void run(); }; int main(int argc, char** argv) { try { opts.parse(argc, argv); if (opts.mode=="shared") mode=SHARED; else if (opts.mode=="fanout") mode = FANOUT; else if (opts.mode=="topic") mode = TOPIC; else throw Exception("Invalid mode"); if (!opts.listen && !opts.publish) opts.listen = opts.publish = true; std::vector listen(opts.consumers); PublishThread publish; if (opts.listen) for (int i = 0; i < opts.consumers; ++i) listen[i].thread=Thread(listen[i]); if (opts.publish) publish.thread=Thread(publish); if (opts.listen) for (int i = 0; i < opts.consumers; ++i) listen[i].thread.join(); if (opts.publish) publish.thread.join(); } catch (const std::exception& e) { cout << "Unexpected exception: " << e.what() << endl; } } double secs(Duration d) { return double(d)/TIME_SEC; } double secs(AbsTime start, AbsTime finish) { return secs(Duration(start,finish)); } void expect(string actual, string expect) { if (expect != actual) throw Exception("Expecting "+expect+" but received "+actual); } const char* exchange() { switch (mode) { case SHARED: return ""; // Deafult exchange. case FANOUT: return "amq.fanout"; case TOPIC: return "amq.topic"; } assert(0); return 0; } void PublishThread::run() { try { Connection connection; opts.open(connection); Session_0_10 session = connection.newSession(); session.queueDeclare(arg::queue="control"); // Control queue session.queuePurge(arg::queue="control"); if (mode==SHARED) { session.queueDeclare(arg::queue="perftest"); // Shared data queue session.queuePurge(arg::queue="perftest"); } // Wait for consumers. SubscriptionManager subs(session); LocalQueue control; subs.subscribe(control, "control"); for (int i = 0; i < opts.consumers; ++i) expect(control.pop().getData(), "ready"); // Create test message size_t msgSize=max(opts.size, 32); Message msg(string(msgSize, 'X'), "perftest"); char* msgBuf = const_cast(msg.getData().data()); if (opts.durable) msg.getDeliveryProperties().setDeliveryMode(framing::PERSISTENT); // Time sending message. AbsTime start=now(); cout << "Publishing " << opts.count << " messages " << flush; for (int i=0; i