在实际项目工作,有同事提出,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 {
private static final long serialVersionUID = 1L;
public AbTestServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Ab Test Servlet 响应 "+System.currentTimeMillis());
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlet项目源代码下载地址
Spring Controller应用描述:
建立一个简单的maven项目,只导入spring-webmvc与spring-context即可(版本4.2.5),编写项目的web.xml文件如下
contextConfigLocation
classpath:/config/application-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
1
true
appServlet
/*
在编写Spring的配置文件如下
编写一个简单的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项目原代码下载地址
命令为: 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
(并发线程数)
Time taken for tests: 0.850 seconds
(使用时间)
Complete requests: 1000
(成功的请求数量)
Failed requests: 0
(失败的请求数量)
Total transferred: 147000 bytes
(全部使用的流量)
HTML transferred: 25000 bytes
(Html文件使用的流量)
Requests per second: 1176.40 [#/sec] (mean)
(指标一 平均每秒请求数)
Time per request: 850.049 [ms] (mean)(测了好几次,500,800,1200都有,平均1000左右)
(指标二 平均事务响应时间)
Time per request: 0.850 [ms] (mean, across all concurrent requests)
(平均请求时间)
Transfer rate: 168.88 [Kbytes/sec] received
(传输速率)
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
(所有请求的响应情况)
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)
每个请求都有一个响应时间 。。
比如 其中 50% 的用户响应时间小于 504 毫秒 。。
最大的响应时间小于 601 毫秒 (100% 处) 。。
命令为: 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
Requests per second: 1479.20 [#/sec] (mean)
Time per request: 676.039 [ms] (mean)
Time per request: 0.676 [ms] (mean, across all concurrent requests)
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%"