原生Servlet与Spring Controller性能比较

        在实际项目工作,有同事提出,Java原生的Servlet性能(响应速度与并发数)要比封装过的Spring Controller高,基于这点,楼主用Apache的ab工具,对两个简单的应用做1000并发压力测试,查看两者的响应速度与并发数、平均响应时间等参数。

 

  ab工具的使用与介绍,楼主就不描述了,网上的文章很多,读者可以参考《Windows下Apache服务器自带Ab.exe的压力测试方法》一文。

 

原生Servlet应用描述:

  用简单的方式,在eclipse中建立一个Dynamic Web project,然后创建一个Servlet,用注解的方式即可,这样可以不用修改web.xml文件,逻辑处理的仿真代码如下(线程休眠50ms,模拟实际的业务处理时间)  

/**
 * Servlet implementation class AbTestServlet
 */
@WebServlet("/AbTestServlet")
public class AbTestServlet extends HttpServlet {
<span style="white-space:pre">	</span>private static final long serialVersionUID = 1L;
       
    public AbTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<span style="white-space:pre">	</span>System.out.println("Ab Test Servlet 响应 "+System.currentTimeMillis());
<span style="white-space:pre">	</span>try {
<span style="white-space:pre">		</span>Thread.sleep(50);
<span style="white-space:pre">	</span>    } catch (InterruptedException e) {
<span style="white-space:pre">		</span>e.printStackTrace();
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>response.getWriter().append("Served at: ").append(request.getContextPath());
<span style="white-space:pre">	</span>}
 
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<span style="white-space:pre">	</span>// TODO Auto-generated method stub
<span style="white-space:pre">	</span>doGet(request, response);
     }
}

Servlet项目源代码下载地址

 

Spring Controller应用描述:

  建立一个简单的maven项目,只导入spring-webmvc与spring-context即可(版本4.2.5),编写项目的web.xml文件如下

	<!-- 4:加入Spring Context 配置文件 与 Listener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/config/application-context.xml</param-value>
	</context-param>
	
	<!-- 4:加入Spring Context 配置文件 与 Listener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
 
	<!-- 1:启动Spring的MVC  DispatcherServlet -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

  在编写Spring的配置文件如下

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:tx="http://www.springframework.org/schema/tx"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans.xsd        
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/context/spring-context.xsd     
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd" >  
	
    <!-- 完成请求和注解POJO的映射(激活@Controller模式  ),它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean, 这两个bean是spring MVC为@Controllers分发请求所必须的  -->
    <mvc:annotation-driven/> 
	<!-- 自动扫描组件Service -->  
	<context:component-scan base-package="com.cloud.abtest"/>
</beans>

  编写一个简单的Controller如下

package com.cloud.abtest.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
 
@Controller
public class AbTestController {
	
	@RequestMapping("/AbTestController")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		System.out.println("Ab Test Controller 响应 "+System.currentTimeMillis());
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "Served at: "+request.getContextPath();
	}
 
}

controller项目原代码下载地址



对原生Servlet项目进行并发压力测试命令和结果如下:

<span style="white-space:pre">	</span>命令为: ab -n 1000 -c 1000 http://localhost:8080/AbtestServlet/AbTestServlet
	Apache24\bin>ab -n 1000 -c 1000 http://localhost:8080/AbtestServlet/AbTestServlet
	This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
	Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
	Licensed to The Apache Software Foundation, http://www.apache.org/
	
	Benchmarking localhost (be patient)
	Completed 100 requests
	Completed 200 requests
	Completed 300 requests
	Completed 400 requests
	Completed 500 requests
	Completed 600 requests
	Completed 700 requests
	Completed 800 requests
	Completed 900 requests
	Completed 1000 requests
	Finished 1000 requests
	
	
	Server Software:        Apache-Coyote/1.1
	Server Hostname:        localhost
	Server Port:            8080
	
	Document Path:          /AbtestServlet/AbTestServlet
	Document Length:        25 bytes
	
	Concurrency Level:      1000
		<span style="background-color: rgb(51, 204, 0);">(并发线程数)</span>
	Time taken for tests:   0.850 seconds
		<span style="background-color: rgb(51, 204, 0);">(使用时间)</span>
	Complete requests:      1000
		<span style="background-color: rgb(51, 255, 51);">(成功的请求数量)</span>
	Failed requests:        0
		<span style="background-color: rgb(51, 255, 51);">(失败的请求数量)</span>
	Total transferred:      147000 bytes
		<span style="background-color: rgb(51, 255, 51);">(全部使用的流量)</span>
	HTML transferred:       25000 bytes
		<span style="background-color: rgb(51, 255, 51);">(Html文件使用的流量)</span>
	Requests per second:    1176.40 [#/sec] (mean)
		<span style="background-color: rgb(51, 255, 51);">(指标一 平均每秒请求数)</span>
	Time per request:       850.049 [ms] (mean)(测了好几次,500,800,1200都有,平均1000左右)
		<span style="background-color: rgb(51, 255, 51);">(指标二 平均事务响应时间)</span>
	Time per request:       0.850 [ms] (mean, across all concurrent requests)
		<span style="background-color: rgb(51, 255, 51);">(平均请求时间)</span>
	Transfer rate:          168.88 [Kbytes/sec] received
		<span style="background-color: rgb(51, 255, 51);">(传输速率)</span>
	
	Connection Times (ms)
	              min  mean[+/-sd] median   max
	Connect:        0    0   1.0      0      18
	Processing:   302  486  81.3    504     600
	Waiting:       93  392  91.2    394     546
	Total:        303  486  81.3    504     601
		<span style="background-color: rgb(51, 255, 51);">(所有请求的响应情况)</span>
	
	Percentage of the requests served within a certain time (ms)
	  50%    504
	  66%    545
	  75%    556
	  80%    562
	  90%    576
	  95%    585
	  98%    596
	  99%    599
	 100%    601 (longest request)
		<span style="background-color: rgb(51, 255, 51);">每个请求都有一个响应时间 。。 
		比如 其中 50% 的用户响应时间小于 504 毫秒 。。 
		最大的响应时间小于 601 毫秒 (100% 处) 。。 </span>

对Controller项目进行并发压力测试命令和结果如下:

<span style="white-space:pre">	</span>命令为: ab -n 1000 -c 1000 http://localhost:8080/abTestController/AbTestController
	Apache24\bin>ab -n 1000 -c 1000 http://localhost:8080/abTestController/AbTestControll
	This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
	Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
	Licensed to The Apache Software Foundation, http://www.apache.org/
	
	Benchmarking localhost (be patient)
	Completed 100 requests
	Completed 200 requests
	Completed 300 requests
	Completed 400 requests
	Completed 500 requests
	Completed 600 requests
	Completed 700 requests
	Completed 800 requests
	Completed 900 requests
	Completed 1000 requests
	Finished 1000 requests
	
	
	Server Software:        Apache-Coyote/1.1
	Server Hostname:        localhost
	Server Port:            8080
	
	Document Path:          /abTestController/AbTestController
	Document Length:        28 bytes
	
	Concurrency Level:      1000
	Time taken for tests:   0.676 seconds
	Complete requests:      1000
	Failed requests:        0
	Total transferred:      195000 bytes
	HTML transferred:       28000 bytes
	<span style="background-color: rgb(51, 255, 51);">Requests per second:    1479.20 [#/sec] (mean)</span>
	Time per request:       676.039 [ms] (mean)
	Time per request:       0.676 [ms] (mean, across all concurrent requests)</span>
	Transfer rate:          281.68 [Kbytes/sec] received
	
	Connection Times (ms)
	              min  mean[+/-sd] median   max
	Connect:        0    0   0.5      0       4
	Processing:    96  325 107.4    358     475
	Waiting:       79  300 108.5    325     475
	Total:         96  325 107.4    358     476

 

结论:

        总体而言Servlet的并发与响应时间比Controller稍微好一些(同一个tomcat),但是几乎体现不出来,楼主贴的结果中Controller结果要好于Servlet的原因是刚好贴了个Controller效果好的,实际测试中每一个项目楼主都用1000的并发量测试好几十次,效果都差不多。

  PS:如果需要测试的URL是带参数的,需要用""号将URL包裹在里面,如下所示:

ab -n 1000 -c 1000 "http://localhost:8080/AbtestServlet/AbTestServlet?sId=1476067342641247&sourceId=0&test=10%KDO%"

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值