tomcat下小博士压力测试
2009-09-26 00:00:00 来源:WEB开发网最大线程数开到100个没有问题
大于101个就有很多连接失败的
初步判断是tomcat的最大连接数设置的问题
重新设置tomcat server.xml
acceptCount=200后,最大线程可开到150 没有任何错误
acceptCount 指定放在队列里的请求数
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" acceptCount="200" />
Java代码
package com.ask.pressure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class AskPressure {
private static String url = "http://192.168.0.59:8080/ask/index.html";
private static Integer error = 0;
private static Integer threads = 150;
private static Long startTime;
public static void main(String[] args) {
WorkThread[] workThreads = new WorkThread[threads];
for(int i = 0; i < threads; i++){
workThreads[i] = new WorkThread();
}
startTime = System.currentTimeMillis();
//System.out.printf("线程数组初始化耗时%d毫秒\n", (System.currentTimeMillis() - startTime));
for(int i = 0; i < threads; i++){
workThreads[i].start();
}
//System.out.printf("连接失败:%d", error);
}
private static class WorkThread extends Thread {
public void run(){
long start = System.currentTimeMillis();
long end = 0;
try {
URL u = new URL(url);
HttpURLConnection urlConn = (HttpURLConnection)u.openConnection();
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-type", "text/html; charset=UTF-8");
urlConn.connect();
//System.out.printf( "小博士页面编码: %s\n", urlConn.getContentEncoding());
InputStream is = urlConn.getInputStream();
StringBuffer buffer = new StringBuffer();
readToBuffer(buffer, is);
end = System.currentTimeMillis();
} catch (Exception e) {
synchronized(error){
error ++;
}
e.printStackTrace();
}
synchronized(threads){
threads --;
System.out.printf("还有%d个未完线程, 耗时%d毫秒\n", threads, (System.currentTimeMillis() - startTime));
if(threads == 0){
System.out.printf("总耗时:%d毫秒\n", (System.currentTimeMillis() - startTime));
System.out.printf("连接失败:%d\n", error);
}
}
}
public void readToBuffer(StringBuffer buffer, InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
}
}
}
还有9个未完线程, 耗时1484毫秒
还有5个未完线程, 耗时1547毫秒
还有6个未完线程, 耗时1516毫秒
还有7个未完线程, 耗时1516毫秒
还有8个未完线程, 耗时1484毫秒
还有4个未完线程, 耗时1547毫秒
还有3个未完线程, 耗时1547毫秒
还有2个未完线程, 耗时1563毫秒
还有1个未完线程, 耗时1578毫秒
总耗时:1578毫秒
连接失败:0
还有0个未完线程, 耗时1578毫秒
总耗时:1578毫秒
连接失败:0
更多精彩
赞助商链接