2019独角兽企业重金招聘Python工程师标准>>>

最近闲来无事,对tomcat、netty以及nodejs的性能对比比较有兴趣,于是自行测试了一下。

测试环境:

测试工具:Apache JMeter2.9

测试代码:

  1. tomcat下的jsp:

    <%@page language="java" contentType="text/html; charset=GBK" %>
    <%@page import = "java.util.Date" %><html>
    <body>hello world!<%= new Date() %>
    </body>
    </html>
  2. netty下的server:
    package com.hannah.netty;
    import java.net.InetSocketAddress;
    import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.ChannelPipelineFactory;
    import org.jboss.netty.channel.Channels;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
    import org.jboss.netty.handler.codec.http.HttpResponseEncoder;public class AdminServer {public static void main(String[] args) {start(8080);}public static void start(int port) {// 配置服务器-使用java线程池作为解释线程ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));// 设置 pipeline factory.bootstrap.setPipelineFactory(new ServerPipelineFactory());// 绑定端口bootstrap.bind(new InetSocketAddress(port));System.out.println("admin start on " + port);}private static class ServerPipelineFactory implements ChannelPipelineFactory {public ChannelPipeline getPipeline() throws Exception {// Create a default pipeline implementation.ChannelPipeline pipeline = Channels.pipeline();pipeline.addLast("decoder", new HttpRequestDecoder());pipeline.addLast("encoder", new HttpResponseEncoder());// http处理handlerpipeline.addLast("handler", new AdminServerHandler());return pipeline;}}
    }
    package com.hannah.thirdparty.netty;import java.util.Date;import org.jboss.netty.buffer.ChannelBuffer;
    import org.jboss.netty.buffer.ChannelBuffers;
    import org.jboss.netty.buffer.DynamicChannelBuffer;
    import org.jboss.netty.channel.Channel;
    import org.jboss.netty.channel.ChannelFutureListener;
    import org.jboss.netty.channel.ChannelHandlerContext;
    import org.jboss.netty.channel.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
    import org.jboss.netty.handler.codec.frame.TooLongFrameException;
    import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
    import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
    import org.jboss.netty.handler.codec.http.HttpRequest;
    import org.jboss.netty.handler.codec.http.HttpResponse;
    import org.jboss.netty.handler.codec.http.HttpResponseStatus;
    import org.jboss.netty.handler.codec.http.HttpVersion;
    import org.jboss.netty.util.CharsetUtil;public class AdminServerHandler extends SimpleChannelUpstreamHandler {@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    //      HttpRequest request = (HttpRequest) e.getMessage();
    //      String uri = request.getUri();
    //      System.out.println("uri:" + uri);HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);ChannelBuffer buffer = new DynamicChannelBuffer(2048);buffer.writeBytes(("Hello World!\t" + new Date()).getBytes("UTF-8"));response.setContent(buffer);response.setHeader("Content-Type", "text/html; charset=UTF-8");response.setHeader("Content-Length", response.getContent().writerIndex());Channel ch = e.getChannel();// Write the initial line and the header.ch.write(response);ch.disconnect();ch.close();}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {Channel ch = e.getChannel();Throwable cause = e.getCause();if (cause instanceof TooLongFrameException) {sendError(ctx, HttpResponseStatus.BAD_REQUEST);return;}cause.printStackTrace();if (ch.isConnected()) {sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);}}private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);response.setHeader(Names.CONTENT_TYPE, "text/plain; charset=UTF-8");response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));// Close the connection as soon as the error message is sent.ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);}
    }
  3. nodejs下的example.js:
    // var url = require('url');
    var http = require('http');
    http.createServer(function (req, res) {// var pathname = url.parse(req.url).pathname;res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Hello World!\t' + new Date());
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');

测试结果:

  1. 线程数:100;循环次数:100;
  2. 线程数:1000;循环次数:10;
  3. 线程数:1000;循环次数:100;

结果分析:整体上来看,并发小的时候,区别不大;当并发逐渐加大时,tomcat首先承受不住,nodejs和netty性能大致相当(netty性能略低点);最后一次请求的时候,tomcat报OutOfMemory的错误了!

转载于:https://my.oschina.net/lifeofpi/blog/120210

tomcat、netty以及nodejs的helloworld性能对比相关推荐

  1. Tomcat vs Jetty vs Undertow性能对比

    文章目录 测试环境 压测环境: 压测指标 结论 Tomcat,Jetty和Undertow是目前比较主流的3款Servlet容器,而且Spring Boot框架还提供了对它们的集成支持(默认使用的是T ...

  2. Struts2、SpringMVC、Servlet(Jsp)性能对比 测试 。 Servlet的性能应该是最好的,可以做为参考基准,其它测试都要向它看齐,参照...

    2019独角兽企业重金招聘Python工程师标准>>> Struts2.SpringMVC.Servlet(Jsp)性能对比 测试 . Servlet的性能应该是最好的,可以做为参考 ...

  3. 网络/Network - 网络编程 - 高性能 - 单服务器高性能模式[网络模型]及性能对比 - 学习/实践

    1.应用场景 主要用于学习单服务器高性能模式及性能对比,尤其是网络模型,这个很重要,并将这些知识在工作中验证,实践,理解,掌握. 2.学习/操作 1.文档阅读 推荐 18 | 单服务器高性能模式:PP ...

  4. Java常用消息队列原理介绍及性能对比

    消息队列使用场景 为什么会需要消息队列(MQ)? 解耦  在项目启动之初来预测将来项目会碰到什么需求,是极其困难的.消息系统在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一 ...

  5. golang连接postgresql too many client_MySQL和PostgreSQL压测性能对比

    阅读使人充实,讨论使人敏捷,写作使人精确. >>> 压测业务场景文章属于互联网社区动态类场景核心功能压测案例.至于题目涉及的MySQL和PostgreSQL之间的关系,主要为业务选型 ...

  6. php下curl与file_get_contents性能对比

    为什么80%的码农都做不了架构师?>>>    上一篇讲了 <php使用curl替代file_get_contents>, 后续贴出了curl和file_get_cont ...

  7. p40与p100训练性能对比

    深度学习训练,选择P100就对了 原文:https://yq.aliyun.com/articles/238764 摘要: 本文使用NVCaffe.MXNet.TensorFlow三个主流开源深度学习 ...

  8. php vs lua,解析LUA与PHP在WEB应用的性能对比

    解析LUA与PHP在WEB应用的性能对比是本文要介绍的内容,这几天用在WEB开发的LUA框架已经完成,框架中已包括数据库操作和模板操作的功能,能够很简单方便的应用在WEB开发上.在此时我对这个LUA框 ...

  9. Jetson Nano and VIM3硬件参数对比及目标检测性能对比

    文章目录: 1 Jetson Nano and VIM3硬件参数对比及目标检测性能对比 2 Jetson nano在yolov4目标检测性能 3 VIM在yolov3.yolov3-tiny.yolo ...

  10. MyISAM与InnoDB两者之间区别与选择,详细总结,性能对比

    1.MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.不 ...

最新文章

  1. Java 高级 --- 多线程快速入门
  2. pycharm快捷键不能用了
  3. javascript转换金额格式
  4. 简明python教程 --C++程序员的视角(六):输入输出IO
  5. python 二维数组赋值_Python中多个变量的灵活处理
  6. 编程求一个后缀表达式的值
  7. JavaScript实现排序算法
  8. c语言奇葩错误,6个奇葩的(hello,world)C语言版(转)
  9. Java FileReader与FileWriter讲解
  10. ubuntu下定时任务的执行
  11. ambiguous reference to overloaded definition(scala与java重载逻辑不匹配)
  12. 笔记、代码清晰易懂!李航《统计学习方法》最新资源全套!
  13. 「管理数学基础」1.2 矩阵理论:线性映射、线性变换T的矩阵表示
  14. 解决台式机外放和插耳机都没声音[基础版]
  15. [解决方法]shc -f xxx.sh shc: invalid first line in script
  16. 记录使用itextpdf通过定位插入图片和文字
  17. PHP:A mono-alphabetic cipher 单字母密码加解密算法(附完整源码)
  18. Vulkan_Ray Tracing 09_反射
  19. python-day11函数的作用
  20. 1.文件字符流:什么是文件字符流???

热门文章

  1. vs2015 ef 连接mysql_VS2015 + EF6连接MYSQL
  2. flush privileges提示Table 'mysql.servers' doesn't exist解决办法
  3. php 判断当前协议,JavaScript 判断当前协议是http还是https
  4. PostgreSQL高级扩展之IP4R
  5. 文档中的公式编号怎么不从1开始
  6. wordpress文章发布时区时间延迟8小时解决方法
  7. 滚动条滚动到页面底部继续加载
  8. MySQL Data目录查找并迁移到data文件夹中
  9. Android 实现书籍翻页效果
  10. 一个***与电脑白痴的爱情故事