Package org.codehaus.activemq.benchmark

Source Code of org.codehaus.activemq.benchmark.BenchmarkSupport

/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.benchmark;

import org.codehaus.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import java.util.ArrayList;
import java.util.List;

/**
* Abstract base class for some simple benchmark tools
*
* @author James Strachan
* @version $Revision: 1.10 $
*/
public class BenchmarkSupport {

    protected int connectionCount = 1;
    protected int batch = 1000;
    protected Destination destination;
    protected boolean embeddedBroker = false;
    private boolean topic = true;
    private boolean durable = false;

    private ActiveMQConnectionFactory factory;
    private String url;
    protected String[] subjects;
    private long time = System.currentTimeMillis();
    private int counter;
    private List resources = new ArrayList();
    private int clientIDCounter;

    public BenchmarkSupport() {
    }

    public void start() {
        System.out.println("Using: " + connectionCount + " connection(s)");
        subjects = new String[connectionCount];
        for (int i = 0; i < connectionCount; i++) {
            subjects[i] = "BENCHMARK.FEED" + i;
        }
        if (useTimerLoop()) {
            Thread timer = new Thread() {
                public void run() {
                    timerLoop();
                }
            };
            timer.start();
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isTopic() {
        return topic;
    }

    public void setTopic(boolean topic) {
        this.topic = topic;
    }

    public ActiveMQConnectionFactory getFactory() {
        return factory;
    }

    public void setFactory(ActiveMQConnectionFactory factory) {
        this.factory = factory;
    }

    public void setSubject(String subject) {
        connectionCount = 1;
        subjects = new String[]{subject};
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isEmbeddedBroker() {
        return embeddedBroker;
    }

    public void setEmbeddedBroker(boolean embeddedBroker) {
        this.embeddedBroker = embeddedBroker;
    }

    public int getConnectionCount() {
        return connectionCount;
    }

    public void setConnectionCount(int connectionCount) {
        this.connectionCount = connectionCount;
    }

    protected Session createSession() throws JMSException {
        if (factory == null) {
            factory = createFactory();
        }
        Connection connection = factory.createConnection();
        if (durable) {
            connection.setClientID(getClass().getName() + clientIDCounter++ );
        }
        addResource(connection);
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        addResource(session);
        return session;
    }

    protected ActiveMQConnectionFactory createFactory() {
        ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory(getUrl());
        if (embeddedBroker) {
            answer.setUseEmbeddedBroker(true);
        }
        return answer;
    }

    protected synchronized void count(int count) {
        counter += count;
        /*
        if (counter > batch) {
            counter = 0;
            long current = System.currentTimeMillis();
            double end = current - time;
            end /= 1000;
            time = current;

            System.out.println("Processed " + batch + " messages in " + end + " (secs)");
        }
        */
    }

    protected synchronized int resetCount() {
        int answer = counter;
        counter = 0;
        return answer;
    }


    protected void timerLoop() {
        int times = 0;
        int total = 0;
        while (true) {
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            int processed = resetCount();
            double average = 0;
            if (processed > 0) {
                total += processed;
                times++;
            }
            if (times > 0) {
                average = total / times;
            }

            long oldtime = time;
            time = System.currentTimeMillis();

            double diff = time - oldtime;

            System.out.println(getClass().getName() + " Processed: " + processed + " messages this second. Average: " + average);
        }
    }

    protected boolean useTimerLoop() {
        return true;
    }

    protected Destination createDestination(Session session, String subject) throws JMSException {
        if (topic) {
            return session.createTopic(subject);
        }
        else {
            return session.createQueue(subject);
        }
    }

    protected void addResource(Object resource) {
        resources.add(resource);
    }

    protected static boolean parseBoolean(String text) {
        return text.equalsIgnoreCase("true");
    }
}
TOP

Related Classes of org.codehaus.activemq.benchmark.BenchmarkSupport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.