Tomcat concurrency Optimization
First, we will introduce a well-known website stress testing tool:Webbench.
Webbench can test the performance of different services on the same hardware and the running status of the same service on different hardware. The standard webbench test shows two items of the server: the number of requests per minute and the amount of data transmitted per second. Webbench can not only test quasi-static pages, but also test dynamic pages (Asp, PHP, Java, CGI. In addition, it supports static or dynamic performance tests for secure websites with SSL, such as e-commerce websites. A maximum of 30 thousand concurrent connections can be simulated to test the load capacity of websites.
Official homepage: http://home.tiscali.cz /~ Cz210552/webbench.html
Webbench installation:
| 123456 |
sudoyum -y installctagswget http://gitsea.com/wp-content/uploads/2013/06/webbench-1.5.tar.gztarzxvf webbench-1.5.tar.gzcdwebbench-1.5makemakeinstall |
Webbench usage:
| 1 |
webbench -c 1000 -t 10 http://www.google.com.hk/ |
Parameter description: webbench-C concurrency-t run test time URL
Example:
Returned results: the number of response requests per minute: 2868 pages/min. The amount of data transmitted per second is 373959 Bytes/sec. The number of successful requests is 478, and the number of failed requests is 0.
Note: before optimizing the configuration and testing, you need to modify the system default maximum number of threads and maximum number of processes, the corresponding modification method please refer to the http://gitsea.com/2013/05/23/linux-ulimit%E8%AF%A6%E8%A7%A3/
There are three common Tomcat running modes: bio, NiO, and APR. We recommend that you use APR in the production environment to solve asynchronous Io problems at the operating system level, greatly improving performance.
Install APR
| 123456 |
sudoyum -y installapr apr-develtarzxvf tomcat-native.tar.gz //This file is under the bin directory of Tomcat.cdtomcat-native-1.1.24-src/jni/native./configure--with-apr=/usr/bin/apr-1-configmakemakeinstall |
After the installation is complete, the following message is displayed:
| 12 |
Libraries have been installed in:/usr/local/apr/lib |
After the installation is successful, you also need to set environment variables for Tomcat by adding a line in the Catalina. Sh file:
| 1 |
CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib" |
Modify
| 1 |
protocol="org.apache.coyote.http11.Http11AprProtocol" |
View logs after starting Tomcat
More than at_home/logs/Catalina. Out
| 1234 |
June 29 11:55:35 am org. Apache. Catalina. Core. aprlifecyclelistener initINFO: Loaded APR based Apache Tomcat Native library 1.1.27 using APR version 1.3.9.June 29 11:55:35 am org. Apache. Catalina. Core. aprlifecyclelistener initINFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. |
Tomcat Optimization
1. JVM Optimization
Add the following statement in tomcat_home/bin/Catalina. Sh. The specific value depends on the situation.
| 1 |
JAVA_OPTS="-Xms1024m -Xmx1024m -Xss1024K -XX:PermSize=64m -XX:MaxPermSize=128m" |
Parameter Details
| 12345 |
-Xms jvm initialization heap memory size-Maximum memory of xmx JVM heap-XSS thread stack size-XX: permsize: initial memory allocation in non-heap JVM-XX: maxpermsize JVM maximum non-heap memory |
Suggestions and precautions:
-The XMS and-xmx options are set to the same heap memory allocation to avoid adjusting the heap size after each GC. The heap memory is recommended to occupy 60% of the memory size ~ 80%; non-heap memory is not recoverable, depending on the project; the thread stack size is recommended to be 256 K.
2. Disable reverse DNS query
Add the following parameters to connector:
3. Use APR
For installation instructions, see the beginning of this article.
4. Optimize Tomcat Parameters
| 123456789 |
<Connector port="8080"protocol="org.apache.coyote.http11.Http11AprProtocol"connectionTimeout="20000"//Connection timeout periodredirectPort="8443"maxThreads="500"//Sets the maximum number of threads used to process customer requests. This determines the number of threads that the server can respond to customer requests at the same time. The default value is 200.minSpareThreads="20"//Number of initialization threads. Minimum number of Idle threads. The default value is 10.acceptCount="1000"//When the number of threads that can be used to process requests is used, the number of requests that can be placed in the processing queue will not be processed if the number of requests exceeds this number. The default value is 100.enableLookups="false"URIEncoding="UTF-8"/> |
5. Optimized Network Parameters
Modify the/etc/sysctl. CNF file and add the following content:
| 123456789101112131415161718 |
net.core.netdev_max_backlog = 32768net.core.somaxconn = 32768net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.ip_local_port_range = 1024 65000net.ipv4.route.gc_timeout = 100net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 1200net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 2net.ipv4.tcp_syn_retries = 2net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 65536 |
Save and exit. Run sysctl-P.