本文来自:fair-jm.iteye.com 转截请注明出处
只是一个简单的测试 在自己使用的笔记本上 测试结果也许有误
测试代码如下:
package com.cc.tools;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* fairjm
* @author fairjm
* fair-jm.iteye.com
*/
public class ConnectionTool {
static volatile CountDownLatch count = null; //用来在main方法中等待任务完成
static AtomicInteger error = new AtomicInteger(0); //记载error的数量
public static void testConnection(final URL url, int times) {
count = new CountDownLatch(times); //初始化
error = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1); //让Thread同时执行任务
for (int i = 0; i < times; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
latch.await(); //等待一起执行
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.getResponseCode();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
error.getAndIncrement();
} finally {
count.countDown();
}
}
}).start();
}
latch.countDown();
}
public static void main(String[] args) throws MalformedURLException,
InterruptedException {
long time=0;
long errors=0;
for (int i = 0; i < 5; i++) {
System.out.println("第"+i+"次");
long begin = System.currentTimeMillis();
// testConnection(new URL("http://localhost:8080/testConnection/index"),600);
testConnection(new URL("http://localhost:9000"), 600);
count.await();
long end = System.currentTimeMillis();
time+=(end-begin);
errors+=error.get();
System.out.println((end-begin)+"ms");
System.out.println("错误个数:"+error.get());
TimeUnit.SECONDS.sleep(5); //暂停5s
}
System.out.println(time /5.0 + "ms");
System.out.println("error:" + errors / 5.0);
}
}
tomcat下:
servlet如下 返回hello:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try(OutputStream os=response.getOutputStream()){
os.write("hello".getBytes());
}
}
netty用的是Play 2 也只是返回hello:
def index = Action { Ok("hello") }
因为有TCP连接数限制 所以测试结果不一定准确 记一次结果(省略掉连接被拒绝等一些错误的输出了)
600次(量很小 不足以说是性能测试 到1000左右的话 会产生一些奇怪的错误) 循环5次
tomcat版本(7.0.29 很久未升级 只是单纯测试用 勿怪):
第0次
1940ms
错误个数:0
第1次
1994ms
错误个数:0
第2次
2095ms
错误个数:0
第3次
1381ms
错误个数:0
第4次
1330ms
错误个数:0
平均:1748.0ms
平均error:0.0
netty(play 2.2.0 dev模式):
第0次
1843ms
错误个数:0
第1次
1498ms
错误个数:0
第2次
2193ms
错误个数:0
第3次
1435ms
错误个数:0
第4次
1719ms
错误个数:0
平均:1737.6ms
平均error:0.0
在这个连接数下tomcat和netty相差无几 但是连接数多的情况下 可以看出netty的性能更优(我这边在单机中进行实验非常容易就出现java.net.SocketException: Permission denied: connect的错误 更大的连接数就不打印报告了)
==================================================================================
啊哈 以上的原因找到了
只要在获取内容之前加上:
connection.setConnectTimeout(2000);
就好了
然后上面的测试就可以加更多的连接数了
http://stackoverflow.com/questions/5692102/java-socket-blocks-on-connection-to-a-server 写道
If the response is a 'SYN-ACK', it proceeds to establish the connection as per the protocol; see http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_establishment.
If the response is an 'RST' (reset), the connect fails and this results in a Java "connection refused" exception. (This is typically what happens if the 'SYN' makes it to the remote server, only to discover that there is no application "listening" on the port you tried to connect on.)
If the response is an ICMP message of some kind (e.g. ICMP destination unreachable), this typically results in an immediate failure of the connection request, and a Java exception.
If there is no response, the OS tries again, and again, and again. Depending on the Java default connect timeout (or the explicit timeout), this process could continue for a long time.
If the response is an 'RST' (reset), the connect fails and this results in a Java "connection refused" exception. (This is typically what happens if the 'SYN' makes it to the remote server, only to discover that there is no application "listening" on the port you tried to connect on.)
If the response is an ICMP message of some kind (e.g. ICMP destination unreachable), this typically results in an immediate failure of the connection request, and a Java exception.
If there is no response, the OS tries again, and again, and again. Depending on the Java default connect timeout (or the explicit timeout), this process could continue for a long time.