summaryrefslogtreecommitdiff
path: root/cpp/src/tests/storePerftools/asyncPerf/PerfTest.cpp
blob: 85dd7fa715a03b12e8b4f80fc5fc3dbc180328f7 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * 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.
 */

/**
 * \file PerfTest.cpp
 */

#include "tests/storePerftools/asyncPerf/MessageConsumer.h"
#include "tests/storePerftools/asyncPerf/MessageProducer.h"
#include "tests/storePerftools/asyncPerf/PerfTest.h"
#include "tests/storePerftools/common/ScopedTimer.h"
#include "tests/storePerftools/common/Thread.h"
#include "tests/storePerftools/version.h"

#include "qpid/Modules.h" // Use with loading store as module
#include "qpid/asyncStore/AsyncStoreImpl.h"
#include "qpid/asyncStore/AsyncStoreOptions.h"
#include "qpid/broker/SimpleQueue.h"

#include <iomanip>

namespace tests {
namespace storePerftools {
namespace asyncPerf {

PerfTest::PerfTest(const TestOptions& to,
                   const qpid::asyncStore::AsyncStoreOptions& aso) :
        m_testOpts(to),
        m_storeOpts(aso),
        m_testResult(to),
        m_msgData(new char[to.m_msgSize]),
        m_poller(new qpid::sys::Poller),
        m_pollingThread(m_poller.get()),
        m_resultQueue(m_poller),
        m_store(0)
{
    std::memset(m_msgData, 0, (size_t)to.m_msgSize);
}

PerfTest::~PerfTest() {
    m_poller->shutdown();
    m_pollingThread.join();

    m_queueList.clear();
    m_queueList.clear();
    m_producers.clear();

    delete[] m_msgData;
}

void
PerfTest::run() {
    if (m_testOpts.m_durable) {
        prepareStore();
    }
    prepareQueues();

    // TODO: replace with qpid::sys::Thread
    std::deque<boost::shared_ptr<tests::storePerftools::common::Thread> > threads;
    { // --- Start of timed section ---
        tests::storePerftools::common::ScopedTimer st(m_testResult);

        for (uint16_t q = 0; q < m_testOpts.m_numQueues; q++) {
            for (uint16_t t = 0; t < m_testOpts.m_numEnqThreadsPerQueue; t++) { // TODO - replace with qpid threads
                boost::shared_ptr<MessageProducer> mp(new MessageProducer(m_testOpts,
                                                                          m_msgData,
                                                                          m_store,
                                                                          m_resultQueue,
                                                                          m_queueList[q]));
                m_producers.push_back(mp);
                boost::shared_ptr<tests::storePerftools::common::Thread> tp(new tests::storePerftools::common::Thread(mp->startProducers,
                                                                                                                      reinterpret_cast<void*>(mp.get())));
                threads.push_back(tp);
            }
            for (uint16_t dt = 0; dt < m_testOpts.m_numDeqThreadsPerQueue; ++dt) { // TODO - replace with qpid threads
                boost::shared_ptr<MessageConsumer> mc(new MessageConsumer(m_testOpts,
                                                                          m_store,
                                                                          m_resultQueue,
                                                                          m_queueList[q]));
                m_consumers.push_back(mc);
                boost::shared_ptr<tests::storePerftools::common::Thread> tp(new tests::storePerftools::common::Thread(mc->startConsumers,
                                                                                                                      reinterpret_cast<void*>(mc.get())));
                threads.push_back(tp);
            }
        }
        while (threads.size()) {
            threads.front()->join();
            threads.pop_front();
        }
    } // --- End of timed section ---
    destroyQueues();
    destroyStore();
}

void
PerfTest::toStream(std::ostream& os) const {
    m_testOpts.printVals(os);
    os << std::endl;
    m_storeOpts.printVals(os);
    os << std::endl;
    os << m_testResult << std::endl;
}

// private
void
PerfTest::prepareStore() {
    qpid::asyncStore::AsyncStoreImpl* s = new qpid::asyncStore::AsyncStoreImpl(m_poller, m_storeOpts);
    s->initialize();
    m_store = s;
}

// private
void
PerfTest::destroyStore() {
    if (m_store) {
        delete m_store;
    }
}

// private
void
PerfTest::prepareQueues() {
    for (uint16_t i = 0; i < m_testOpts.m_numQueues; ++i) {
        std::ostringstream qname;
        qname << "queue_" << std::setw(4) << std::setfill('0') << i;
        boost::shared_ptr<qpid::broker::SimpleQueue> mpq(new qpid::broker::SimpleQueue(qname.str(), m_queueArgs, m_store, m_resultQueue));
        mpq->asyncCreate();
        m_queueList.push_back(mpq);
    }
}

// private
void
PerfTest::destroyQueues() {
    while (m_queueList.size() > 0) {
        m_queueList.front()->asyncDestroy(m_testOpts.m_destroyQueuesOnCompletion);
        m_queueList.pop_front();
    }
}

int
runPerfTest(int argc, char** argv) {
    // Load async store module
    qpid::tryShlib ("asyncStore.so", false);

    qpid::CommonOptions co;
    qpid::asyncStore::AsyncStoreOptions aso;
    TestOptions to;
    qpid::Options opts;
    opts.add(co).add(aso).add(to);
    try {
        opts.parse(argc, argv);
        aso.validate();
        to.validate();

        // Handle options that just print information then exit.
        if (co.version) {
            std::cout << tests::storePerftools::name() << " v." << tests::storePerftools::version() << std::endl;
            return 0;
        }
        if (co.help) {
            std::cout << tests::storePerftools::name() << ": asyncPerf" << std::endl;
            std::cout << "Performance test for the async store through the qpid async store interface." << std::endl;
            std::cout << "Usage: asyncPerf [options]" << std::endl;
            std::cout << opts << std::endl;
            return 0;
        }

        // Create and start test
        tests::storePerftools::asyncPerf::PerfTest apt(to, aso);
        apt.run();

        // Print test result
        std::cout << apt << std::endl;
        //::sleep(1);
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

}}} // namespace tests::storePerftools::asyncPerf

// -----------------------------------------------------------------

int
main(int argc, char** argv) {
    return tests::storePerftools::asyncPerf::runPerfTest(argc, argv);
}