1 18 package org.apache.activemq.perf; 19 20 import javax.jms.Connection ; 21 import javax.jms.ConnectionFactory ; 22 import javax.jms.Destination ; 23 import javax.jms.JMSException ; 24 import javax.jms.Message ; 25 import javax.jms.MessageConsumer ; 26 import javax.jms.MessageProducer ; 27 import javax.jms.Session ; 28 import javax.jms.TemporaryQueue ; 29 import javax.jms.TemporaryTopic ; 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 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 bindAddress="vm://localhost"; 47 protected int topicCount=0; 48 49 public void testPerformance() throws Exception { 50 ConnectionFactory factory=createConnectionFactory(); 51 Connection connection=factory.createConnection(); 52 for(int i=0;i<MESSAGE_COUNT;i++){ 53 54 Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE); 55 Destination dest=session.createTemporaryTopic(); 56 MessageConsumer mc=session.createConsumer(dest); 57 MessageProducer mp=session.createProducer(dest); 58 Message 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 getDestination(Session session) throws JMSException { 68 String topicName=getClass().getName()+"."+topicCount++; 69 return session.createTopic(topicName); 70 } 71 72 protected void releaseDestination(Destination dest) throws JMSException { 73 if(dest instanceof TemporaryTopic ){ 74 TemporaryTopic tt=(TemporaryTopic ) dest; 75 tt.delete(); 76 }else if(dest instanceof TemporaryQueue ){ 77 TemporaryQueue tq=(TemporaryQueue ) dest; 78 tq.delete(); 79 } 80 } 81 82 protected void setUp() throws Exception { 83 if(broker==null){ 84 broker=createBroker(); 85 } 86 super.setUp(); 87 } 88 89 protected void tearDown() throws Exception { 90 super.tearDown(); 91 92 if(broker!=null){ 93 broker.stop(); 94 } 95 } 96 97 protected ActiveMQConnectionFactory createConnectionFactory() throws Exception { 98 ActiveMQConnectionFactory cf=new ActiveMQConnectionFactory(bindAddress); 99 return cf; 100 } 101 102 protected BrokerService createBroker() throws Exception { 103 BrokerService answer=new BrokerService(); 104 configureBroker(answer); 105 answer.start(); 106 return answer; 107 } 108 109 protected void configureBroker(BrokerService answer) throws Exception { 110 answer.setPersistent(false); 111 answer.addConnector(bindAddress); 112 answer.setDeleteAllMessagesOnStartup(true); 113 } 114 } 115 | Popular Tags |