KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > perf > MemoryAllocationTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.perf;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.ConnectionFactory JavaDoc;
22 import javax.jms.Destination JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageConsumer JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TemporaryQueue JavaDoc;
29 import javax.jms.TemporaryTopic JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.apache.activemq.ActiveMQConnectionFactory;
34 import org.apache.activemq.broker.BrokerService;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 /**
38  * @version $Revision: 1.3 $
39  */

40 public class MemoryAllocationTest extends TestCase{
41     
42     protected static final Log log = LogFactory.getLog(MemoryAllocationTest.class);
43
44     protected static final int MESSAGE_COUNT=2000;
45     protected BrokerService broker;
46     protected String JavaDoc bindAddress="vm://localhost";
47     protected int topicCount=0;
48
49     public void testPerformance() throws Exception JavaDoc{
50         ConnectionFactory JavaDoc factory=createConnectionFactory();
51         Connection JavaDoc connection=factory.createConnection();
52         for(int i=0;i<MESSAGE_COUNT;i++){
53            
54             Session JavaDoc session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
55             Destination JavaDoc dest=session.createTemporaryTopic();
56             MessageConsumer JavaDoc mc=session.createConsumer(dest);
57             MessageProducer JavaDoc mp=session.createProducer(dest);
58             Message JavaDoc msg=session.createTextMessage("test"+i);
59             mp.send(msg);
60             session.close();
61            releaseDestination(dest);
62             if (i%500==0)log.info("Iterator "+i);
63         }
64         connection.close();
65     }
66
67     protected Destination JavaDoc getDestination(Session JavaDoc session) throws JMSException JavaDoc{
68         String JavaDoc topicName=getClass().getName()+"."+topicCount++;
69         return session.createTopic(topicName);
70     }
71
72     protected void releaseDestination(Destination JavaDoc dest) throws JMSException JavaDoc{
73         if(dest instanceof TemporaryTopic JavaDoc){
74             TemporaryTopic JavaDoc tt=(TemporaryTopic JavaDoc) dest;
75             tt.delete();
76         }else if(dest instanceof TemporaryQueue JavaDoc){
77             TemporaryQueue JavaDoc tq=(TemporaryQueue JavaDoc) dest;
78             tq.delete();
79         }
80     }
81
82     protected void setUp() throws Exception JavaDoc{
83         if(broker==null){
84             broker=createBroker();
85         }
86         super.setUp();
87     }
88
89     protected void tearDown() throws Exception JavaDoc{
90         super.tearDown();
91         
92         if(broker!=null){
93           broker.stop();
94         }
95     }
96
97     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc{
98         ActiveMQConnectionFactory cf=new ActiveMQConnectionFactory(bindAddress);
99         return cf;
100     }
101
102     protected BrokerService createBroker() throws Exception JavaDoc{
103         BrokerService answer=new BrokerService();
104         configureBroker(answer);
105         answer.start();
106         return answer;
107     }
108
109     protected void configureBroker(BrokerService answer) throws Exception JavaDoc{
110         answer.setPersistent(false);
111         answer.addConnector(bindAddress);
112         answer.setDeleteAllMessagesOnStartup(true);
113     }
114 }
115
Popular Tags