设为首页 收藏本站
查看: 250|回复: 0

[经验分享] Tomcat netty简单的连接性能比较测试

[复制链接]
发表于 2017-2-6 09:24:48 | 显示全部楼层 |阅读模式
  本文来自: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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-338080-1-1.html 上篇帖子: (转)一次使用Eclipse Memory Analyzer分析Tomcat内存溢出 下篇帖子: 进入黑马day3-HTTP协议与tomcat协议
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表