summaryrefslogtreecommitdiff
path: root/java/perftests/src/main/java/org/apache/qpid/topic/TopicWithSelectorsTransientVolumeTest.java
blob: e0c0b003351eab250a1aec7b6a50274156000eca (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * 
 * 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.
 * 
 */
package org.apache.qpid.topic;

import java.util.ArrayList;
import java.util.HashMap;

import javax.jms.BytesMessage;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.NamingException;

import org.apache.qpid.AMQException;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.client.AMQTopic;
import org.apache.qpid.exchange.ExchangeDefaults;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

public class TopicWithSelectorsTransientVolumeTest extends QpidBrokerTestCase
{   
    private static final int NUM_MSG_PER_ITERATION = 50;//must be a multiple of 10
    private static final int NUM_ITERATIONS = 1000;
    
    private static final int NUM_CONSUMERS = 50;
    private static final int MSG_SIZE = 1024;
    private static final byte[] BYTE_ARRAY = new byte[MSG_SIZE];
    
    ArrayList<MyMessageSubscriber> _subscribers = new ArrayList<MyMessageSubscriber>();
    HashMap<String,Long> _queueMsgCounts = new HashMap<String,Long>();
    
    private final static Object _lock=new Object();
    private boolean _producerFailed;
    private static int _finishedCount;
    private static int _failedCount;
    
    protected void setUp() throws Exception
    {
        super.setUp();
        init();
    }

    protected void tearDown() throws Exception
    {
        super.tearDown();
    }
    
    private void init()
    {
        _finishedCount = 0;
        _failedCount = 0;
        _producerFailed = false;
        _subscribers.clear();
        _queueMsgCounts.clear();
    }
    
    
    private Message createMessage(Session session) throws JMSException
    {
        BytesMessage message = session.createBytesMessage();
        message.writeBytes(BYTE_ARRAY);
        
        return message;
    }
    
    /**
     * 1 Topic with 50 subscribers using a selector, and 1 producer sending 50,000 1K messages with 90% selector success ratio.
     */
    public void test50SubscribersWith90PercentMatched() throws Exception
    {
        Topic topic = new AMQTopic(ExchangeDefaults.TOPIC_EXCHANGE_NAME, "test50ConsumersWith10PercentUnmatched");

        System.out.println("Creating consumers");

        MyMessageSubscriber sub;

        for(int i=1; i <= NUM_CONSUMERS; i++)
        {
            sub = new MyMessageSubscriber(topic, "consumer" + i, ((9 * NUM_MSG_PER_ITERATION * NUM_ITERATIONS) / 10));
            _subscribers.add(sub);
        }
        
        System.out.println("Starting consumers");
        for(MyMessageSubscriber s: _subscribers)
        {
            Thread consumer = new Thread(s);
            consumer.start();
        }

        System.out.println("Creating producer");
        MyMessageProducer prod = new MyMessageProducer(topic);
        
        long startTime = System.currentTimeMillis();
        
        System.out.println("Starting producer");
        Thread producer = new Thread(prod);
        producer.start();
        
        
        // Wait for all the messageConsumers to have finished or failed
        synchronized (_lock)
        {
            while (_finishedCount + _failedCount < NUM_CONSUMERS)
            {
                try
                {
                    _lock.wait();
                }
                catch (InterruptedException e)
                {
                    //ignore
                }
            }
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("Elapsed time for messaging: " + (endTime-startTime) + "ms");
        
        assertFalse("Producer failed to send all messages", _producerFailed);
        
        //check if all messages received by consumers, or if there were failures
        if (_finishedCount != NUM_CONSUMERS)
        {
            fail(_failedCount + " consumers did not recieve all their expected messages");
        }
        
        //check if all queue depths were 0
        for(String consumer: _queueMsgCounts.keySet())
        {
            long depth = _queueMsgCounts.get(consumer);
            assertEquals(consumer + " subscription queue msg count was not 0", 0, depth);
        }

    }
    
    private class MyMessageProducer implements Runnable
    {
        private TopicConnection _connection;
        private TopicSession _session;
        private TopicPublisher _messagePublisher;

        public MyMessageProducer(Topic topic) throws JMSException, NamingException
        {
            _connection = (TopicConnection) getConnection();
            _session = (TopicSession) _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            _messagePublisher = _session.createPublisher(topic);
        }

        public void run()
        {
            try
            {
                for(int iter = 0; iter < NUM_ITERATIONS; iter++)
                {
                    int i = 0;
                    
                    //send 90% matching messages
                    for (; i < (9 * NUM_MSG_PER_ITERATION)/10; i++)
                    {
                        Message message = createMessage(_session);
                        message.setStringProperty("testprop", "true");

                        _messagePublisher.publish(message, DeliveryMode.NON_PERSISTENT, 
                                Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);

                        Thread.yield();
                    }

                    //send remaining 10% non-matching messages
                    for (; i < NUM_MSG_PER_ITERATION; i++)
                    {
                        Message message = _session.createMessage();
                        message.setStringProperty("testprop", "false");

                        _messagePublisher.publish(message, DeliveryMode.NON_PERSISTENT, 
                                Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);

                        Thread.yield();
                    }
                }

            }
            catch (Exception exp)
            {
                System.out.println("producer: caught an exception, probably exiting before all messages sent");
                exp.printStackTrace();
                synchronized (_lock)
                {
                    _producerFailed=true;
                    _lock.notifyAll();
                }
            }
        }
    }
    
    
    private class MyMessageSubscriber implements Runnable
    {
        /* The topic this subscriber is subscribing  to */
        private Topic _topic;
        private String _consumerName;
        private int _outstandingMsgCount;
        private TopicConnection _connection;
        private TopicSession _session;
        private TopicSubscriber _durSub;

        public MyMessageSubscriber(Topic topic, String consumerName, int messageCount) throws JMSException, NamingException
        {
            _outstandingMsgCount = messageCount;
            _topic=topic;
            _consumerName = consumerName;
            _connection = (TopicConnection) getConnection();
            _session = (TopicSession) _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            _durSub = _session.createDurableSubscriber(_topic, _consumerName,"testprop='true'", false);
            _connection.start();
        }

        public void run()
        {
  
                boolean failed = false;
                do
                {
                    Message m = null;
                    try
                    {
                        m = _durSub.receive(10000);
                    }
                    catch (JMSException exp)
                    {
                        System.out.println(_consumerName + ": caught an exception handling a received message");
                        exp.printStackTrace();

                        failed = true;
                        break;
                    }

                    Thread.yield();

                    _outstandingMsgCount--;
                    
                    if(_outstandingMsgCount % 500 == 0)
                    {
                        System.out.println(_consumerName + ": outstanding message count: " + _outstandingMsgCount);
                    }
                    
                    if(m == null)
                    {
                        if(_outstandingMsgCount != 0)
                        {
                            failed = true;
                        }
                        break;
                    }
                }
                while(_outstandingMsgCount > 0);
                
                System.out.println(_consumerName + ": outstanding message count: " + _outstandingMsgCount);
                
                try
                {
                    AMQQueue subcriptionQueue = new AMQQueue(ExchangeDefaults.TOPIC_EXCHANGE_NAME,"clientid" + ":" + _consumerName);

                    ((AMQSession)_session).sync();
                    Long depth = ((AMQSession)_session).getQueueDepth(subcriptionQueue);
                    _queueMsgCounts.put(_consumerName, depth);
                    
                    System.out.println(_consumerName + ": completion queue msg count: " + depth);
                }
                catch (AMQException exp)
                {
                    System.out.println(_consumerName + ": caught an exception determining completion queue depth");
                    exp.printStackTrace();
                }
                finally
                {
                    try
                    {
                        _session.unsubscribe(_consumerName);
                    }
                    catch (JMSException e)
                    {
                        System.out.println(_consumerName + ": caught an exception whilst unsubscribing");
                        e.printStackTrace();
                    }
                }

                synchronized (_lock)
                {
                    if (_outstandingMsgCount == 0 && !failed)
                    {
                        _finishedCount++;
                        System.out.println(_consumerName + ": finished");
                    }
                    else
                    {
                        _failedCount++;
                        System.out.println(_consumerName + ": failed");
                    }
                    _lock.notifyAll();
                }

        }
    }

    //helper method to allow easily running against an external standalone broker
//    public static void main(String[] args) throws Exception
//    {
//        System.setProperty("broker.config", "/dev/null");
//        System.setProperty("broker", "external");
//        System.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
//        System.setProperty("java.naming.provider.url", "test-profiles/test-provider.properties");
//        
//        TopicWithSelectorsTransientVolumeTest test = new TopicWithSelectorsTransientVolumeTest();
//        test.init();
//        test.test50SubscribersWith90PercentMatched();
//        test.tearDown();
//    }
}