    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.benchmark;
    19
    20  import org.codehaus.activemq.ActiveMQConnectionFactory;
    21
    22  import javax.jms.Connection;
    23  import javax.jms.Destination;
    24  import javax.jms.JMSException;
    25  import javax.jms.Session;
    26  import java.util.ArrayList;
    27  import java.util.List;
    28
    29  /***
    30   * Abstract base class for some simple benchmark tools
    31   *
    32   * @author James Strachan
    33   * @version $Revision: 1.10 $
    34   */
    35  public class BenchmarkSupport {
    36
    37      protected int connectionCount = 1;
    38      protected int batch = 1000;
    39      protected Destination destination;
    40      protected boolean embeddedBroker = false;
    41      private boolean topic = true;
    42      private boolean durable = false;
    43
    44      private ActiveMQConnectionFactory factory;
    45      private String url;
    46      protected String[] subjects;
    47      private long time = System.currentTimeMillis();
    48      private int counter;
    49      private List resources = new ArrayList();
    50      private int clientIDCounter;
    51
    52      public BenchmarkSupport() {
    53      }
    54
    55      public void start() {
    56          System.out.println("Using: " + connectionCount + " connection(s)");
    57          subjects = new String[connectionCount];
    58          for (int i = 0; i < connectionCount; i++) {
    59              subjects[i] = "BENCHMARK.FEED" + i;
    60          }
    61          if (useTimerLoop()) {
    62              Thread timer = new Thread() {
    63                  public void run() {
    64                      timerLoop();
    65                  }
    66              };
    67              timer.start();
    68          }
    69      }
    70
    71      public String getUrl() {
    72          return url;
    73      }
    74
    75      public void setUrl(String url) {
    76          this.url = url;
    77      }
    78
    79      public boolean isTopic() {
    80          return topic;
    81      }
    82
    83      public void setTopic(boolean topic) {
    84          this.topic = topic;
    85      }
    86
    87      public ActiveMQConnectionFactory getFactory() {
    88          return factory;
    89      }
    90
    91      public void setFactory(ActiveMQConnectionFactory factory) {
    92          this.factory = factory;
    93      }
    94
    95      public void setSubject(String subject) {
    96          connectionCount = 1;
    97          subjects = new String[]{subject};
    98      }
    99
    100     public boolean isDurable() {
    101         return durable;
    102     }
    103
    104     public void setDurable(boolean durable) {
    105         this.durable = durable;
    106     }
    107
    108     public boolean isEmbeddedBroker() {
    109         return embeddedBroker;
    110     }
    111
    112     public void setEmbeddedBroker(boolean embeddedBroker) {
    113         this.embeddedBroker = embeddedBroker;
    114     }
    115
    116     public int getConnectionCount() {
    117         return connectionCount;
    118     }
    119
    120     public void setConnectionCount(int connectionCount) {
    121         this.connectionCount = connectionCount;
    122     }
    123
    124     protected Session createSession() throws JMSException {
    125         if (factory == null) {
    126             factory = createFactory();
    127         }
    128         Connection connection = factory.createConnection();
    129         if (durable) {
    130             connection.setClientID(getClass().getName() + clientIDCounter++ );
    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         if (embeddedBroker) {
    143             answer.setUseEmbeddedBroker(true);
    144         }
    145         return answer;
    146     }
    147
    148     protected synchronized void count(int count) {
    149         counter += count;
    150         /*
    151         if (counter > batch) {
    152             counter = 0;
    153             long current = System.currentTimeMillis();
    154             double end = current - time;
    155             end /= 1000;
    156             time = current;
    157
    158             System.out.println("Processed " + batch + " messages in " + end + " (secs)");
    159         }
    160         */
    161     }
    162
    163     protected synchronized int resetCount() {
    164         int answer = counter;
    165         counter = 0;
    166         return answer;
    167     }
    168
    169
    170     protected void timerLoop() {
    171         int times = 0;
    172         int total = 0;
    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     }
    198
    199     protected boolean useTimerLoop() {
    200         return true;
    201     }
    202
    203     protected Destination createDestination(Session session, String subject) throws JMSException {
    204         if (topic) {
    205             return session.createTopic(subject);
    206         }
    207         else {
    208             return session.createQueue(subject);
    209         }
    210     }
    211
    212     protected void addResource(Object resource) {
    213         resources.add(resource);
    214     }
    215
    216     protected static boolean parseBoolean(String text) {
    217         return text.equalsIgnoreCase("true");
    218     }
    219 }