1 18 package org.apache.activemq.tool; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.activemq.tool.sampler.ThroughputSamplerTask; 23 import org.apache.activemq.tool.sampler.CpuSamplerTask; 24 import org.apache.activemq.tool.reports.PerformanceReportWriter; 25 import org.apache.activemq.tool.reports.XmlFilePerfReportWriter; 26 import org.apache.activemq.tool.reports.VerbosePerfReportWriter; 27 import org.apache.activemq.tool.properties.JmsClientSystemProperties; 28 import org.apache.activemq.tool.properties.AbstractObjectProperties; 29 import org.apache.activemq.tool.properties.JmsFactoryProperties; 30 import org.apache.activemq.tool.properties.ReflectionUtil; 31 import org.apache.activemq.tool.properties.JmsClientProperties; 32 import org.apache.activemq.tool.spi.SPIConnectionFactory; 33 34 import javax.jms.ConnectionFactory ; 35 import javax.jms.JMSException ; 36 import javax.jms.ConnectionMetaData ; 37 import java.util.Properties ; 38 import java.util.Enumeration ; 39 import java.io.IOException ; 40 import java.io.File ; 41 import java.io.FileInputStream ; 42 43 public abstract class AbstractJmsClientSystem extends AbstractObjectProperties { 44 private static final Log log = LogFactory.getLog(AbstractJmsClientSystem.class); 45 46 private int clientDestIndex, clientDestCount; 47 48 protected ThreadGroup clientThreadGroup; 49 protected ConnectionFactory jmsConnFactory; 50 51 protected JmsFactoryProperties factory = new JmsFactoryProperties(); 53 protected ThroughputSamplerTask tpSampler = new ThroughputSamplerTask(); 54 protected CpuSamplerTask cpuSampler = new CpuSamplerTask(); 55 56 public void runSystemTest() throws JMSException { 57 jmsConnFactory = loadJmsFactory(getSysTest().getSpiClass(), factory.getFactorySettings()); 59 60 setProviderMetaData(jmsConnFactory.createConnection().getMetaData(), getJmsClientProperties()); 61 62 PerformanceReportWriter writer = createPerfWriter(); 64 tpSampler.setPerfReportWriter(writer); 65 cpuSampler.setPerfReportWriter(writer); 66 67 writer.openReportWriter(); 68 writer.writeProperties("jvmSettings", System.getProperties()); 69 writer.writeProperties("testSystemSettings", ReflectionUtil.retrieveObjectProperties(getSysTest())); 70 writer.writeProperties("jmsFactorySettings", ReflectionUtil.retrieveObjectProperties(jmsConnFactory)); 71 writer.writeProperties("jmsClientSettings", ReflectionUtil.retrieveObjectProperties(getJmsClientProperties())); 72 writer.writeProperties("tpSamplerSettings", ReflectionUtil.retrieveObjectProperties(tpSampler)); 73 writer.writeProperties("cpuSamplerSettings", ReflectionUtil.retrieveObjectProperties(cpuSampler)); 74 75 clientThreadGroup = new ThreadGroup (getSysTest().getClientPrefix() + " Thread Group"); 76 for (int i=0; i<getSysTest().getNumClients(); i++) { 77 distributeDestinations(getSysTest().getDestDistro(), i, getSysTest().getNumClients(), getSysTest().getTotalDests()); 78 79 final String clientName = getSysTest().getClientPrefix() + i; 80 final int clientDestIndex = this.clientDestIndex; 81 final int clientDestCount = this.clientDestCount; 82 Thread t = new Thread (clientThreadGroup, new Runnable () { 83 public void run() { 84 runJmsClient(clientName, clientDestIndex, clientDestCount); 85 } 86 }); 87 t.setName(getSysTest().getClientPrefix() + i + " Thread"); 88 t.start(); 89 } 90 91 if (getSysTest().getSamplers().indexOf(JmsClientSystemProperties.SAMPLER_TP) > -1) { 93 tpSampler.startSampler(); 94 } 95 96 if (getSysTest().getSamplers().indexOf(JmsClientSystemProperties.SAMPLER_CPU) > -1) { 97 try { 98 cpuSampler.createPlugin(); 99 cpuSampler.startSampler(); 100 } catch (IOException e) { 101 log.warn("Unable to start CPU sampler plugin. Reason: " + e.getMessage()); 102 } 103 } 104 105 tpSampler.waitUntilDone(); 106 cpuSampler.waitUntilDone(); 107 108 writer.closeReportWriter(); 109 } 110 111 public ThroughputSamplerTask getTpSampler() { 112 return tpSampler; 113 } 114 115 public void setTpSampler(ThroughputSamplerTask tpSampler) { 116 this.tpSampler = tpSampler; 117 } 118 119 public CpuSamplerTask getCpuSampler() { 120 return cpuSampler; 121 } 122 123 public void setCpuSampler(CpuSamplerTask cpuSampler) { 124 this.cpuSampler = cpuSampler; 125 } 126 127 public JmsFactoryProperties getFactory() { 128 return factory; 129 } 130 131 public void setFactory(JmsFactoryProperties factory) { 132 this.factory = factory; 133 } 134 135 public abstract JmsClientSystemProperties getSysTest(); 136 public abstract void setSysTest(JmsClientSystemProperties sysTestProps); 137 public abstract JmsClientProperties getJmsClientProperties(); 138 139 protected PerformanceReportWriter createPerfWriter() { 140 if (getSysTest().getReportType().equalsIgnoreCase(JmsClientSystemProperties.REPORT_XML_FILE)) { 141 String reportName; 142 143 if ((reportName = getSysTest().getReportName()) == null) { 144 reportName = getSysTest().getClientPrefix() + "_" + 145 "numClients" + getSysTest().getNumClients() + "_" + 146 "numDests" + getSysTest().getTotalDests() + "_" + 147 getSysTest().getDestDistro(); 148 } 149 return new XmlFilePerfReportWriter(getSysTest().getReportDir(), reportName); 150 } else if (getSysTest().getReportType().equalsIgnoreCase(JmsClientSystemProperties.REPORT_VERBOSE)) { 151 return new VerbosePerfReportWriter(); 152 } else { 153 return new VerbosePerfReportWriter(); 155 } 156 } 157 158 protected void distributeDestinations(String distroType, int clientIndex, int numClients, int numDests) { 159 if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_ALL)) { 160 clientDestCount = numDests; 161 clientDestIndex = 0; 162 } else if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_EQUAL)) { 163 int destPerClient = (numDests / numClients); 164 if (destPerClient > 0) { 166 clientDestCount = destPerClient; 167 clientDestIndex = destPerClient * clientIndex; 168 } else { 170 clientDestCount = 1; clientDestIndex = clientIndex % numDests; 172 } 173 } else if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_DIVIDE)) { 174 int destPerClient = (numDests / numClients); 175 if (destPerClient > 0) { 177 int remain = numDests % numClients; 178 int nextIndex; 179 if (clientIndex < remain) { 180 destPerClient++; 181 nextIndex = clientIndex * destPerClient; 182 } else { 183 nextIndex = (clientIndex * destPerClient) + remain; 184 } 185 186 clientDestCount = destPerClient; 187 clientDestIndex = nextIndex; 188 189 } else { 191 clientDestCount = 1; clientDestIndex = clientIndex % numDests; 193 } 194 195 } else { 197 log.warn("Unknown destination distribution type: " + distroType); 198 clientDestCount = numDests; 199 clientDestIndex = 0; 200 } 201 } 202 203 protected ConnectionFactory loadJmsFactory(String spiClass, Properties factorySettings) throws JMSException { 204 try { 205 Class spi = Class.forName(spiClass); 206 SPIConnectionFactory spiFactory = (SPIConnectionFactory)spi.newInstance(); 207 ConnectionFactory jmsFactory = spiFactory.createConnectionFactory(factorySettings); 208 log.info("Created: " + jmsFactory.getClass().getName() + " using SPIConnectionFactory: " + spiFactory.getClass().getName()); 209 return jmsFactory; 210 } catch (Exception e) { 211 e.printStackTrace(); 212 throw new JMSException (e.getMessage()); 213 } 214 } 215 216 protected void setProviderMetaData(ConnectionMetaData metaData, JmsClientProperties props) throws JMSException { 217 props.setJmsProvider(metaData.getJMSProviderName() + "-" + metaData.getProviderVersion()); 218 props.setJmsVersion(metaData.getJMSVersion()); 219 220 String jmsProperties = ""; 221 Enumeration jmsProps = metaData.getJMSXPropertyNames(); 222 while (jmsProps.hasMoreElements()) { 223 jmsProperties += (jmsProps.nextElement().toString() + ","); 224 } 225 if (jmsProperties.length() > 0) { 226 jmsProperties = jmsProperties.substring(0, jmsProperties.length()-1); 228 } 229 props.setJmsProperties(jmsProperties); 230 } 231 232 protected abstract void runJmsClient(String clientName, int clientDestIndex, int clientDestCount); 233 234 protected static Properties parseStringArgs(String [] args) { 235 File configFile = null; 236 Properties props = new Properties(); 237 238 if (args == null || args.length == 0) { 239 return props; } 241 242 for (int i=0; i<args.length; i++) { 243 String arg = args[i]; 244 if (arg.startsWith("-D") || arg.startsWith("-d")) { 245 arg = arg.substring(2); 246 } 247 int index = arg.indexOf("="); 248 String key = arg.substring(0, index); 249 String val = arg.substring(index + 1); 250 251 if (key.equalsIgnoreCase("sysTest.propsConfigFile")) { 252 if (!val.endsWith(".properties")) { 253 val += ".properties"; 254 } 255 configFile = new File (val); 256 } 257 props.setProperty(key, val); 258 } 259 260 Properties fileProps = new Properties(); 261 try { 262 if (configFile != null) { 263 log.info("Loading properties file: " + configFile.getAbsolutePath()); 264 fileProps.load(new FileInputStream (configFile)); 265 } 266 } catch (IOException e) { 267 e.printStackTrace(); 268 } 269 fileProps.putAll(props); 271 return fileProps; 272 } 273 } 274 | Popular Tags |