Tomcat NIO、APR 压力测试性能对比

Tomcat 三种线程模式

一、bio(blocking I/O)

即阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包)。是基于JAVA的HTTP/1.1连接器,Tomcat7以下版本在默认情况下是以bio模式运行的。一般而言,bio模式是三种运行模式中性能最低的一种。我们可以通过Tomcat Manager来查看服务器的当前状态。(Tomcat7或以下,在 Linux 系统中默认使用这种方式)

一个线程处理一个请求,缺点:并发量高时,线程数较多,浪费资源

二、nio(new I/O)

是Java SE 1.4及后续版本提供的一种新的I/O操作方式(即java.nio包及其子包)。Java nio是一个基于缓冲区、并能提供非阻塞I/O操作的Java API,因此nio也被看成是non-blocking I/O的缩写。它拥有比传统I/O操作(bio)更好的并发运行性能。要让Tomcat以nio模式来运行只需要在Tomcat安装目录/conf/server.xml 中将对应的中protocol的属性值改为 org.apache.coyote.http11.Http11NioProtocol即可

利用 Java 的异步请求 IO 处理,可以通过少量的线程处理大量的请求

注意: Tomcat8 以上版本在 Linux 系统中,默认使用的就是NIO模式,不需要额外修改 ,Tomcat7必须修改Connector配置来启动

undefined

三、apr(Apache Portable Runtime/Apache可移植运行时) ( 安装配置过程相对复杂)

Tomcat将以JNI的形式调用Apache HTTP服务器的核心动态链接库来处理文件读取或网络传输操作,从而大大地提高Tomcat对静态文件的处理性能。Tomcat apr也是在Tomcat上运行高并发应用的首选模式。从操作系统级别来解决异步的IO问题

undefined

zhuawang’s blog三种高级运行模式

主要注意的一点:

毕竟 APR 是 c 语言编写的,是非跨平台的,如果你追求稳定和简单的话,推荐还是默认的 NIO 模式

Tomcat启动的时候,可以通过log看到Connector使用的是哪一种运行模式:

  • Starting ProtocolHandler [“http-bio-8080”]
  • Starting ProtocolHandler [“http-nio-8080”]
  • Starting ProtocolHandler [“http-apr-8080”]

APR 安装

APR 官网下载地址
  • APR 1.6.5 is the best available version
  • APR-util 1.6.1 is the best available version
  • APR iconv 1.2.2 is the best available version
安装顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 下载APR依赖包
yum install -y expat expat-devel

安装步骤
1. apr
1. 解压至安装目录(# tar -xvf apr-1.6.5.tar.gz -C ../install/)
2. ./configure -prefix=/usr/local/apr
3. make
4. make install
2. apr-iconv
1. 解压至安装目录(# tar -xvf apr-iconv-1.2.2.tar.gz -C ../install/)
2. ./configure -prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
3. make
4. make install
3. apr-util
1. 解压至安装目录(# tar -xvf apr-util-1.6.1.tar.gz -C ../install/)
2. ./configure -prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apriconv=/usr/local/apr-iconv
3. make
4. make install
Tomcat APR模式配置
Native 解压配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@iZuf6iq8e7ya9v3ix71k0pZ apr-util-1.6.1]# cd /opt/install/apache-tomcat-9.0.14/bin
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ls
bootstrap.jar configtest.bat makebase.bat tomcat-juli.jar
catalina.bat configtest.sh makebase.sh tomcat-native.tar.gz
catalina.sh daemon.sh setclasspath.bat tool-wrapper.bat
catalina-tasks.xml digest.bat setclasspath.sh tool-wrapper.sh
ciphers.bat digest.sh shutdown.bat version.bat
ciphers.sh guns-logs shutdown.sh version.sh
commons-daemon.jar guns-rest.log startup.bat
commons-daemon-native.tar.gz hs_err_pid6741.log startup.sh
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# tar -zxf tomcat-native.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# cd tomcat-native-1.2.19-src/
[root@iZuf6iq8e7ya9v3ix71k0pZ tomcat-native-1.2.19-src]# cd native/
[root@iZuf6iq8e7ya9v3ix71k0pZ native]# ls
build build-outputs.mk include Makefile.in os tcnative.pc.in
buildconf config.layout libtcnative.dsp NMAKEmakefile src tcnative.spec
build.conf configure libtcnative.dsw NMAKEmakefile.inc srclib
BUILDING configure.in LICENSE.bin.win NOTICE.bin.win tcnative.dsp


[提示 java home 找不到]
[root@iZuf6iq8e7ya9v3ix71k0pZ native]# ./configure --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
Tomcat Native Version: 1.2.19
checking for chosen layout... tcnative
checking for APR... yes
configure: APR 1.6.5 detected.
setting CC to "gcc"
setting CPP to "gcc -E"
setting LIBTOOL to "/usr/local/apr/build-1/libtool"
checking for JDK location... configure: error: Java Home not defined. Rerun with --with-java-home=... parameter
[root@iZuf6iq8e7ya9v3ix71k0pZ install]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
[root@iZuf6iq8e7ya9v3ix71k0pZ install]# source /etc/profile
[root@iZuf6iq8e7ya9v3ix71k0pZ install]# echo $JAVA_HOME
/usr/java/jdk1.8.0_111

[root@iZuf6iq8e7ya9v3ix71k0pZ native]# ./configure --with-apr=/usr/local/apr - -with-java-home=/usr/java/jdk1.8.0_111

[root@iZuf6iq8e7ya9v3ix71k0pZ native]# make & make install


2. 修改 catalina.sh
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib export LD_LIBRARY_PATH

3. 修改 server.xml
protocol="org.apache.coyote.http11.Http11AprProtocol"

NIO 和 APR 压力测试

安装压力测试 apache A/B 测试
1
[root@iZuf6iq8e7ya9v3ix71k0pZ apache-tomcat-9.0.14]# yum install httpd-tools
启动不同模式的 Tomcat 服务
1
2
21-Jan-2019 17:03:41.288 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-apr-8081"]
21-Jan-2019 17:03:41.288 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8082"]
TP99 性能指标
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ab -n 3000 -c 100 http://smniuhe.com:8081/xxx/list
Time per request: 1335.676 [ms] (mean)
Time per request: 1289.714 [ms] (mean)
Time per request: 1344.385 [ms] (mean)

ab -n 3000 -c 100 http://smniuhe.com:8082/xxx/list
Time per request: 1293.279 [ms] (mean)
Time per request: 1275.337 [ms] (mean)
Time per request: 1291.890 [ms] (mean)

数据量比较少的缘故,并不能明显的看出
ab -n 6000 -c 100 http://smniuhe.com:8081/xxx/list
Time per request: 995.545 [ms] (mean)
apr_pollset_poll: The timeout specified has expired (70007)
Total of 5983 requests completed
apr_pollset_poll: The timeout specified has expired (70007)
Total of 5992 requests completed


ab -n 6000 -c 100 http://smniuhe.com:8082/xxx/list
Time per request: 975.884 [ms] (mean)
Time per request: 975.526 [ms] (mean)
Time per request: 966.517 [ms] (mean)