001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.component;
018:
019: import java.util.Date;
020: import java.util.Properties;
021: import java.util.concurrent.CountDownLatch;
022: import java.util.concurrent.ExecutorService;
023: import java.util.concurrent.Executors;
024: import java.util.concurrent.TimeUnit;
025:
026: import javax.jbi.messaging.ExchangeStatus;
027: import javax.jbi.messaging.InOut;
028: import javax.xml.namespace.QName;
029:
030: import org.apache.activemq.xbean.BrokerFactoryBean;
031: import org.apache.log4j.PropertyConfigurator;
032: import org.apache.servicemix.client.DefaultServiceMixClient;
033: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
034: import org.apache.servicemix.jbi.jaxp.StringSource;
035: import org.apache.servicemix.tck.SpringTestSupport;
036: import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
037: import org.springframework.context.support.AbstractXmlApplicationContext;
038: import org.springframework.core.io.ClassPathResource;
039:
040: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
041:
042: public class LoanBrokerPerformanceTest extends SpringTestSupport {
043:
044: private BrokerFactoryBean bean;
045:
046: @Override
047: protected void setUp() throws Exception {
048: bean = new BrokerFactoryBean();
049: bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
050: bean.afterPropertiesSet();
051: super .setUp();
052: }
053:
054: @Override
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: bean.destroy();
058: }
059:
060: public LoanBrokerPerformanceTest() {
061: Properties properties = new Properties();
062: properties.put("log4j.rootCategory", "INFO, stdout");
063: properties.put("log4j.appender.stdout",
064: "org.apache.log4j.ConsoleAppender");
065: properties.put("log4j.appender.stdout.layout",
066: "org.apache.log4j.PatternLayout");
067: properties.put(
068: "log4j.appender.stdout.layout.ConversionPattern",
069: "%p [%t] %c{1}.%M(%L) | %m%n");
070: PropertyConfigurator.configure(properties);
071: }
072:
073: public void testRegularLoanBroker() throws Exception {
074: final DefaultServiceMixClient client = new DefaultServiceMixClient(
075: jbi);
076: {
077: InOut me = client.createInOutExchange();
078: me.setService(new QName("http://bpmscript.org/jbi",
079: "loanbroker"));
080: me.getInMessage().setContent(
081: new StringSource("<hello>world</hello>"));
082: assertTrue(client.sendSync(me, 1000000));
083: if (me.getStatus() == ExchangeStatus.ERROR) {
084: if (me.getFault() != null) {
085: fail("Received fault: "
086: + new SourceTransformer().toString(me
087: .getFault().getContent()));
088: } else if (me.getError() != null) {
089: throw me.getError();
090: } else {
091: fail("Received ERROR status");
092: }
093: }
094: }
095:
096: int total = 100;
097: long startTime = System.currentTimeMillis();
098: ExecutorService executorService = Executors
099: .newFixedThreadPool(2);
100: final AtomicInteger count = new AtomicInteger(0);
101: for (int i = 0; i < total; i++) {
102: executorService.execute(new Runnable() {
103: public void run() {
104: try {
105: InOut me = client.createInOutExchange();
106: me.setService(new QName(
107: "http://bpmscript.org/jbi",
108: "loanbroker"));
109: me
110: .getInMessage()
111: .setContent(
112: new StringSource(
113: "<hello>world</hello>"));
114: assertTrue(client.sendSync(me, 1000000));
115: if (me.getStatus() == ExchangeStatus.ERROR) {
116: if (me.getFault() != null) {
117: fail("Received fault: "
118: + new SourceTransformer()
119: .toString(me.getFault()
120: .getContent()));
121: } else if (me.getError() != null) {
122: throw me.getError();
123: } else {
124: fail("Received ERROR status");
125: }
126: }
127: count.incrementAndGet();
128: } catch (Throwable t) {
129: fail(t.getMessage());
130: }
131: }
132:
133: });
134: }
135: executorService.shutdown();
136: while (!executorService.awaitTermination(2, TimeUnit.SECONDS)) {
137: log
138: .info(new Date() + ": " + count.get()
139: + " have finished");
140: }
141: long duration = System.currentTimeMillis() - startTime;
142: log.info("Average time " + ((double) duration / (double) total)
143: + "ms");
144: log.info("Average throughput "
145: + (((double) total / (double) duration) * 60000.0)
146: + "tpm");
147: }
148:
149: protected AbstractXmlApplicationContext createBeanFactory() {
150: return new ClassPathXmlApplicationContext(
151: "/loanbroker/spring.xml");
152: }
153:
154: }
|