KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > benchmark > BenchmarkSupport


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.benchmark;
19
20 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
21
22 import org.apache.activemq.ActiveMQConnectionFactory;
23 import org.apache.activemq.util.IdGenerator;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.Destination JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Session JavaDoc;
29
30 import java.text.NumberFormat JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Abstract base class for some simple benchmark tools
36  *
37  * @author James Strachan
38  * @version $Revision$
39  */

40 public class BenchmarkSupport {
41
42     protected int connectionCount = 1;
43     protected int batch = 1000;
44     protected Destination JavaDoc destination;
45     private boolean topic = true;
46     private boolean durable = false;
47
48     private ActiveMQConnectionFactory factory;
49     private String JavaDoc url;
50     protected String JavaDoc[] subjects;
51     private long time = System.currentTimeMillis();
52     private int counter;
53     private List JavaDoc resources = new ArrayList JavaDoc();
54     private NumberFormat JavaDoc formatter = NumberFormat.getInstance();
55     private AtomicInteger JavaDoc connectionCounter = new AtomicInteger JavaDoc(0);
56     private IdGenerator idGenerator = new IdGenerator();
57
58     public BenchmarkSupport() {
59     }
60
61     public void start() {
62         System.out.println("Using: " + connectionCount + " connection(s)");
63         subjects = new String JavaDoc[connectionCount];
64         for (int i = 0; i < connectionCount; i++) {
65             subjects[i] = "BENCHMARK.FEED" + i;
66         }
67         if (useTimerLoop()) {
68             Thread JavaDoc timer = new Thread JavaDoc() {
69                 public void run() {
70                     timerLoop();
71                 }
72             };
73             timer.start();
74         }
75     }
76
77     public String JavaDoc getUrl() {
78         return url;
79     }
80
81     public void setUrl(String JavaDoc url) {
82         this.url = url;
83     }
84
85     public boolean isTopic() {
86         return topic;
87     }
88
89     public void setTopic(boolean topic) {
90         this.topic = topic;
91     }
92
93     public ActiveMQConnectionFactory getFactory() {
94         return factory;
95     }
96
97     public void setFactory(ActiveMQConnectionFactory factory) {
98         this.factory = factory;
99     }
100
101     public void setSubject(String JavaDoc subject) {
102         connectionCount = 1;
103         subjects = new String JavaDoc[]{subject};
104     }
105
106     public boolean isDurable() {
107         return durable;
108     }
109
110     public void setDurable(boolean durable) {
111         this.durable = durable;
112     }
113
114     public int getConnectionCount() {
115         return connectionCount;
116     }
117
118     public void setConnectionCount(int connectionCount) {
119         this.connectionCount = connectionCount;
120     }
121
122     protected Session JavaDoc createSession() throws JMSException JavaDoc {
123         if (factory == null) {
124             factory = createFactory();
125         }
126         Connection JavaDoc connection = factory.createConnection();
127         int value = connectionCounter.incrementAndGet();
128         System.out.println("Created connection: " + value + " = " + connection);
129         if (durable) {
130             connection.setClientID(idGenerator.generateId());
131         }
132         addResource(connection);
133         connection.start();
134
135         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
136         addResource(session);
137         return session;
138     }
139
140     protected ActiveMQConnectionFactory createFactory() {
141         ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory(getUrl());
142         return answer;
143     }
144
145     protected synchronized void count(int count) {
146         counter += count;
147         /*
148         if (counter > batch) {
149             counter = 0;
150             long current = System.currentTimeMillis();
151             double end = current - time;
152             end /= 1000;
153             time = current;
154
155             System.out.println("Processed " + batch + " messages in " + end + " (secs)");
156         }
157         */

158     }
159
160     protected synchronized int resetCount() {
161         int answer = counter;
162         counter = 0;
163         return answer;
164     }
165
166
167     protected void timerLoop() {
168         int times = 0;
169         int total = 0;
170         int dumpVmStatsFrequency = 10;
171         Runtime JavaDoc runtime = Runtime.getRuntime();
172
173         while (true) {
174             try {
175                 Thread.sleep(1000);
176             }
177             catch (InterruptedException JavaDoc e) {
178                 e.printStackTrace();
179             }
180             int processed = resetCount();
181             double average = 0;
182             if (processed > 0) {
183                 total += processed;
184                 times++;
185             }
186             if (times > 0) {
187                 average = total / times;
188             }
189
190             long oldtime = time;
191             time = System.currentTimeMillis();
192
193             double diff = time - oldtime;
194
195             System.out.println(getClass().getName() + " Processed: " + processed + " messages this second. Average: " + average);
196
197             if ((times % dumpVmStatsFrequency) == 0 && times != 0) {
198                 System.out.println("Used memory: " + asMemoryString(runtime.totalMemory() - runtime.freeMemory())
199                         + " Free memory: " + asMemoryString(runtime.freeMemory())
200                         + " Total memory: " + asMemoryString(runtime.totalMemory())
201                         + " Max memory: " + asMemoryString(runtime.maxMemory()));
202             }
203
204         }
205     }
206
207     protected String JavaDoc asMemoryString(long value) {
208         return formatter.format(value / 1024) + " K";
209     }
210
211     protected boolean useTimerLoop() {
212         return true;
213     }
214
215     protected Destination JavaDoc createDestination(Session JavaDoc session, String JavaDoc subject) throws JMSException JavaDoc {
216         if (topic) {
217             return session.createTopic(subject);
218         }
219         else {
220             return session.createQueue(subject);
221         }
222     }
223
224     protected void addResource(Object JavaDoc resource) {
225         resources.add(resource);
226     }
227
228     protected static boolean parseBoolean(String JavaDoc text) {
229         return text.equalsIgnoreCase("true");
230     }
231 }
232
Popular Tags