Subversion Repositories general

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.httpbench;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpBench
{
        public static void main(String[] args)
                throws Exception
        {
                (new HttpThread(null, new String[] { "http://sun/" } )).run();
        }
}

class HttpThread
{
        private static int threadCount = 0;
        private static Log log         = LogFactory.getLog(HttpThread.class);

        private int      id;
        private String   firstUrl; // is used one time only, may be null
        private String[] urls;
        private int      requestNumber = 1;
        private int      requestPerConnection = 0; // 0 == all
        private int      sleepMin = 0;
        private int      sleepMax = 0;

        public HttpThread(String firstUrl, String[] urls)
        {
                id = threadCount++;
                this.firstUrl = firstUrl;
                this.urls     = urls;
        }

        public int getId()
        {
                return id;
        }

        public int getRequestNumber()
        {
                return requestNumber;
        }

        public void setRequestNumber(int requestNumber)
        {
                this.requestNumber = requestNumber;
        }

        public int getRequestPerConnection()
        {
                return requestPerConnection;
        }

        public void setRequestPerConnection(int requestPerConnection)
        {
                this.requestPerConnection = requestPerConnection;
        }

        public int getSleepMin()
        {
                return sleepMin;
        }

        public void setSleepMin(int sleepMin)
        {
                this.sleepMin = sleepMin;
        }

        public int getSleepMax()
        {
                return sleepMax;
        }

        public void setSleepMax(int sleepMax)
        {
                this.sleepMax = sleepMax;
        }

        public void run()
        {
                long startTime = 0;
                long stopTime  = 0;
                long sumSleep  = 0;

                log.info(Integer.toString(id) + ": start");

                try {
                        HttpClient client = new HttpClient();

                        startTime = System.currentTimeMillis();
                        for(int i = 0; i < requestNumber; i++) {
                                String url;
                                if(i == 0 && firstUrl != null)
                                        url = firstUrl;
                                else
                                        url = urls[(firstUrl == null ? i : i-1) % urls.length];

                                GetMethod get = new GetMethod(url);

                                try {
                                        int result = client.executeMethod(get);
                                        if(log.isTraceEnabled()) {
                                                String response = get.getResponseBodyAsString();
                                                log.trace(Integer.toString(id) + ": got " + result + "\n'" 
                                                        + response.substring(0, 100) + "'");
                                        }
                                        else if(log.isDebugEnabled()) {
                                                log.debug(Integer.toString(id) + ": got " + result);
                                        }
                                }
                                finally {
                                        get.releaseConnection();
                                }

                                if(requestPerConnection > 0 && (i+1) % requestPerConnection == 0
                                        && i < requestNumber-1)
                                {
                                        log.debug(Integer.toString(id)
                                                + ": request per connection limit reached at " + i);
                                        client.getHttpConnectionManager().closeIdleConnections(0);
                                }

                                if(sleepMax > 0 && i < requestNumber-1) {
                                        long sleep = (long)(Math.random() * (sleepMax - sleepMin)) + sleepMin;
                                        log.debug(Integer.toString(id) + ": sleep for " + sleep + "ms");
                                        Thread.currentThread().sleep(sleep);
                                        sumSleep += sleep;
                                }
                        }

                        client.getHttpConnectionManager().closeIdleConnections(0);
                        stopTime = System.currentTimeMillis();
                }
                catch(Exception ex) {
                        log.warn(Integer.toString(id), ex);
                }

                log.info(Integer.toString(id) + ": done" + (stopTime > 0 ? " in " + (stopTime - startTime)
                        + "ms, sleeped for " + sumSleep + " ms" : ""));
        }
}