1 18 package org.apache.activemq.benchmark; 19 20 import java.util.concurrent.atomic.AtomicInteger ; 21 22 import org.apache.activemq.ActiveMQConnectionFactory; 23 import org.apache.activemq.util.IdGenerator; 24 25 import javax.jms.Connection ; 26 import javax.jms.Destination ; 27 import javax.jms.JMSException ; 28 import javax.jms.Session ; 29 30 import java.text.NumberFormat ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 40 public class BenchmarkSupport { 41 42 protected int connectionCount = 1; 43 protected int batch = 1000; 44 protected Destination destination; 45 private boolean topic = true; 46 private boolean durable = false; 47 48 private ActiveMQConnectionFactory factory; 49 private String url; 50 protected String [] subjects; 51 private long time = System.currentTimeMillis(); 52 private int counter; 53 private List resources = new ArrayList (); 54 private NumberFormat formatter = NumberFormat.getInstance(); 55 private AtomicInteger connectionCounter = new AtomicInteger (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 [connectionCount]; 64 for (int i = 0; i < connectionCount; i++) { 65 subjects[i] = "BENCHMARK.FEED" + i; 66 } 67 if (useTimerLoop()) { 68 Thread timer = new Thread () { 69 public void run() { 70 timerLoop(); 71 } 72 }; 73 timer.start(); 74 } 75 } 76 77 public String getUrl() { 78 return url; 79 } 80 81 public void setUrl(String 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 subject) { 102 connectionCount = 1; 103 subjects = new String []{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 createSession() throws JMSException { 123 if (factory == null) { 124 factory = createFactory(); 125 } 126 Connection 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 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 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 runtime = Runtime.getRuntime(); 172 173 while (true) { 174 try { 175 Thread.sleep(1000); 176 } 177 catch (InterruptedException 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 asMemoryString(long value) { 208 return formatter.format(value / 1024) + " K"; 209 } 210 211 protected boolean useTimerLoop() { 212 return true; 213 } 214 215 protected Destination createDestination(Session session, String subject) throws JMSException { 216 if (topic) { 217 return session.createTopic(subject); 218 } 219 else { 220 return session.createQueue(subject); 221 } 222 } 223 224 protected void addResource(Object resource) { 225 resources.add(resource); 226 } 227 228 protected static boolean parseBoolean(String text) { 229 return text.equalsIgnoreCase("true"); 230 } 231 } 232 | Popular Tags |