您现在的位置是:主页 > news > 大连网站建设日尧/湛江seo网站管理

大连网站建设日尧/湛江seo网站管理

admin2025/6/29 17:34:54news

简介大连网站建设日尧,湛江seo网站管理,ftontpage如何做网站,网站运营企业在实际项目工作,有同事提出,Java原生的Servlet性能(响应速度与并发数)要比封装过的Spring Controller高,基于这点,楼主用Apache的ab工具,对两个简单的应用做1000并发压力测试,查看两者的响应速度与并发数、…

大连网站建设日尧,湛江seo网站管理,ftontpage如何做网站,网站运营企业在实际项目工作,有同事提出,Java原生的Servlet性能(响应速度与并发数)要比封装过的Spring Controller高,基于这点,楼主用Apache的ab工具,对两个简单的应用做1000并发压力测试,查看两者的响应速度与并发数、…

        在实际项目工作,有同事提出,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")@ResponseBodypublic 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/AbTestServletApache24\bin>ab -n 1000 -c 1000 http://localhost:8080/AbtestServlet/AbTestServletThis 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 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsCompleted 1000 requestsFinished 1000 requestsServer Software:        Apache-Coyote/1.1Server Hostname:        localhostServer Port:            8080Document Path:          /AbtestServlet/AbTestServletDocument Length:        25 bytesConcurrency 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   maxConnect:        0    0   1.0      0      18Processing:   302  486  81.3    504     600Waiting:       93  392  91.2    394     546Total:        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%    50466%    54575%    55680%    56290%    57695%    58598%    59699%    599100%    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/AbTestControllerApache24\bin>ab -n 1000 -c 1000 http://localhost:8080/abTestController/AbTestControllThis 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 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsCompleted 1000 requestsFinished 1000 requestsServer Software:        Apache-Coyote/1.1Server Hostname:        localhostServer Port:            8080Document Path:          /abTestController/AbTestControllerDocument Length:        28 bytesConcurrency Level:      1000Time taken for tests:   0.676 secondsComplete requests:      1000Failed requests:        0Total transferred:      195000 bytesHTML 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] receivedConnection Times (ms)min  mean[+/-sd] median   maxConnect:        0    0   0.5      0       4Processing:    96  325 107.4    358     475Waiting:       79  300 108.5    325     475Total:         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%"