KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > test > SimplePerfTest


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.test;
22
23 import com.presumo.jms.client.JmsTopicConnectionFactory;
24
25 import javax.jms.ExceptionListener JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.jms.TopicSubscriber JavaDoc;
30 import javax.jms.TopicPublisher JavaDoc;
31 import javax.jms.Message JavaDoc;
32 import javax.jms.BytesMessage JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.MessageListener JavaDoc;
35 import javax.jms.Session JavaDoc;
36
37 /**
38  * Simple functional test
39  *
40  * @author Dan Greff
41  */

42 public class SimplePerfTest implements MessageListener JavaDoc, ExceptionListener JavaDoc
43 {
44
45   public final static int PUBLISHER = 0;
46   public final static int SUBSCRIBER = 1;
47   public final static int BOTH = 2;
48   
49   public static String JavaDoc hostname = "localhost";
50   public static int serverPort;
51   public static int localPort;
52   
53   private final int numOfMessages;
54   private final int iterations;
55   private final int totalMessages;;
56   
57   private long firstMessageReceived;
58   private int messagesReceived;
59   private int totalMessagesReceived;
60   private volatile boolean exceptionOccurred = false;
61   
62   private final int type;
63   private TopicConnection JavaDoc connection;
64   
65   SimplePerfTest(int type)
66   {
67     this(type, 10, 2);
68   }
69   
70   SimplePerfTest(int type, int numOfMessages, int iterations)
71   {
72     this.type = type;
73     this.numOfMessages = numOfMessages;
74     this.iterations = iterations;
75     this.totalMessages = numOfMessages * iterations;
76   }
77   
78   void startTest() throws Exception JavaDoc
79   {
80     JmsTopicConnectionFactory factory = new JmsTopicConnectionFactory();
81     factory.setHost(hostname);
82     factory.setPort(2323);
83     
84     if (type == PUBLISHER || type == SUBSCRIBER) {
85       //factory.setServerName(hostname);
86
//factory.setServerPort(serverPort);
87
}
88     
89     connection = factory.createTopicConnection();
90     connection.setExceptionListener(this);
91     
92     connection.start();
93     String JavaDoc topicName = "testing";
94     
95     if ( type == SUBSCRIBER || type == BOTH )
96     {
97       TopicSession JavaDoc session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
98       Topic JavaDoc topic = session.createTopic(topicName);
99       TopicSubscriber JavaDoc sub = session.createSubscriber(topic, null, false);
100       sub.setMessageListener(this);
101       System.out.println("Subscriber Created");
102     }
103     
104     if (type == PUBLISHER || type == BOTH )
105     {
106       TopicSession JavaDoc session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
107       Topic JavaDoc topic = session.createTopic(topicName);
108       TopicPublisher JavaDoc pub = session.createPublisher(topic);
109       pub.setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
110       
111       for (int i=0; i < iterations; i++) {
112         long startTime = System.currentTimeMillis();
113   
114         for (int j=0; j < numOfMessages; j++) {
115           //BytesMessage msg = session.createBytesMessage();
116
//msg.writeBytes(new byte[2000]);
117
Message JavaDoc msg = session.createMessage();
118           msg.setIntProperty("Count", (i* numOfMessages + j));
119           pub.publish(msg);
120         }
121         long finishTime = System.currentTimeMillis();
122         long time = finishTime - startTime;
123         System.out.println("Published "+ numOfMessages +" messages in " + time +" millseconds.");
124         try {
125           Thread.sleep(2000);
126         } catch (InterruptedException JavaDoc ie) {}
127       }
128      
129       if (type == PUBLISHER) {
130         try { Thread.sleep(10000); } catch (InterruptedException JavaDoc ie) {}
131         totalMessagesReceived = totalMessages;
132       }
133         
134     }
135   }
136   
137   public void onException(JMSException JavaDoc jmsex)
138   {
139     jmsex.printStackTrace();
140     exceptionOccurred = true;
141   }
142   
143   public void onMessage(Message JavaDoc message)
144   {
145     ++messagesReceived;
146     ++totalMessagesReceived;
147     if (messagesReceived == 1) {
148       firstMessageReceived = System.currentTimeMillis();
149     }
150     if (messagesReceived == numOfMessages) {
151       long time = System.currentTimeMillis() - firstMessageReceived;
152       System.out.println("Received "+ numOfMessages +" messages in " + time +" milliseconds.");
153       messagesReceived = 0;
154     }
155     //System.out.println("Received Message: " + message);
156
}
157   
158   boolean isComplete() {
159     return ( exceptionOccurred ||
160              totalMessagesReceived == totalMessages);
161   }
162   
163   void close() throws Exception JavaDoc
164   {
165     if (connection != null)
166       connection.close();
167
168   }
169
170   public static void main(String JavaDoc [] args) throws Exception JavaDoc
171   {
172 // hostname = java.net.InetAddress.getLocalHost().getHostName();
173
// serverPort = 2400;
174

175     SimplePerfTest test = null;
176     if (args.length == 0) {
177       test = new SimplePerfTest(BOTH);
178     }
179     else if (args[0].equals("both")) {
180       test = new SimplePerfTest(BOTH, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
181     }
182     else if (args[0].equals("pub")) {
183       test = new SimplePerfTest(PUBLISHER, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
184     }
185     else if (args[0].equals("sub")) {
186       test = new SimplePerfTest(SUBSCRIBER, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
187     }
188     if (args.length == 4)
189       hostname = args[3];
190       
191     test.startTest();
192     
193     while (!test.isComplete() ) {
194       try { Thread.sleep(1000); } catch (InterruptedException JavaDoc ie) {}
195       System.gc(); System.gc(); System.gc();
196     }
197     
198     System.out.println("Closing connection");
199     test.close();
200     System.out.println("Test completed");
201   }
202 }
Popular Tags