Subversion Repositories general

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1127 dev 1
package ak.httpbench;
2
 
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
 
6
import org.apache.commons.httpclient.HttpClient;
7
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
8
import org.apache.commons.httpclient.methods.GetMethod;
9
 
10
public class HttpBench
11
{
12
	public static void main(String[] args)
13
		throws Exception
14
	{
15
		(new HttpThread(null, new String[] { "http://sun/" } )).run();
16
	}
17
}
18
 
19
class HttpThread
20
{
21
	private static int threadCount = 0;
22
	private static Log log         = LogFactory.getLog(HttpThread.class);
23
 
24
	private int      id;
25
	private String   firstUrl; // is used one time only, may be null
26
	private String[] urls;
27
	private int      requestNumber = 1;
28
	private int      requestPerConnection = 0; // 0 == all
29
	private int      sleepMin = 0;
30
	private int      sleepMax = 0;
31
 
32
	public HttpThread(String firstUrl, String[] urls)
33
	{
34
		id = threadCount++;
35
		this.firstUrl = firstUrl;
36
		this.urls     = urls;
37
	}
38
 
39
	public int getId()
40
	{
41
		return id;
42
	}
43
 
44
	public int getRequestNumber()
45
	{
46
		return requestNumber;
47
	}
48
 
49
	public void setRequestNumber(int requestNumber)
50
	{
51
		this.requestNumber = requestNumber;
52
	}
53
 
54
	public int getRequestPerConnection()
55
	{
56
		return requestPerConnection;
57
	}
58
 
59
	public void setRequestPerConnection(int requestPerConnection)
60
	{
61
		this.requestPerConnection = requestPerConnection;
62
	}
63
 
64
	public int getSleepMin()
65
	{
66
		return sleepMin;
67
	}
68
 
69
	public void setSleepMin(int sleepMin)
70
	{
71
		this.sleepMin = sleepMin;
72
	}
73
 
74
	public int getSleepMax()
75
	{
76
		return sleepMax;
77
	}
78
 
79
	public void setSleepMax(int sleepMax)
80
	{
81
		this.sleepMax = sleepMax;
82
	}
83
 
84
	public void run()
85
	{
86
		long startTime = 0;
87
		long stopTime  = 0;
88
		long sumSleep  = 0;
89
 
90
		log.info(Integer.toString(id) + ": start");
91
 
92
		try {
93
			HttpClient client = new HttpClient();
94
 
95
			startTime = System.currentTimeMillis();
96
			for(int i = 0; i < requestNumber; i++) {
97
				String url;
98
				if(i == 0 && firstUrl != null)
99
					url = firstUrl;
100
				else
101
					url = urls[(firstUrl == null ? i : i-1) % urls.length];
102
 
103
				GetMethod get = new GetMethod(url);
104
 
105
				try {
106
					int result = client.executeMethod(get);
107
					if(log.isTraceEnabled()) {
108
						String response = get.getResponseBodyAsString();
109
						log.trace(Integer.toString(id) + ": got " + result + "\n'" 
110
							+ response.substring(0, 100) + "'");
111
					}
112
					else if(log.isDebugEnabled()) {
113
						log.debug(Integer.toString(id) + ": got " + result);
114
					}
115
				}
116
				finally {
117
					get.releaseConnection();
118
				}
119
 
120
				if(requestPerConnection > 0 && (i+1) % requestPerConnection == 0
121
					&& i < requestNumber-1)
122
				{
123
					log.debug(Integer.toString(id)
124
						+ ": request per connection limit reached at " + i);
125
					client.getHttpConnectionManager().closeIdleConnections(0);
126
				}
127
 
128
				if(sleepMax > 0 && i < requestNumber-1) {
129
					long sleep = (long)(Math.random() * (sleepMax - sleepMin)) + sleepMin;
130
					log.debug(Integer.toString(id) + ": sleep for " + sleep + "ms");
131
					Thread.currentThread().sleep(sleep);
132
					sumSleep += sleep;
133
				}
134
			}
135
 
136
			client.getHttpConnectionManager().closeIdleConnections(0);
137
			stopTime = System.currentTimeMillis();
138
		}
139
		catch(Exception ex) {
140
			log.warn(Integer.toString(id), ex);
141
		}
142
 
143
		log.info(Integer.toString(id) + ": done" + (stopTime > 0 ? " in " + (stopTime - startTime)
144
			+ "ms, sleeped for " + sumSleep + " ms" : ""));
145
	}
146
}
147