1. 程式人生 > >web應用效能測試-Tomcat 7 連線數和執行緒數配置

web應用效能測試-Tomcat 7 連線數和執行緒數配置

原文:http://www.jianshu.com/p/8445645b3aff

引言

這段時間折騰了哈java web應用的壓力測試,部署容器是tomcat 7。期間學到了蠻多散碎的知識點,及時梳理總結,構建良好且易理解的知識架構把它們組織起來,以備忘。
對web應用開發者來說,我們很關心應用可同時處理的請求數,以及響應時間。應用本身和它執行在其中的web容器是兩個很重要的影響因素。
對tomcat來說,每一個進來的請求(request)都需要一個執行緒,直到該請求結束。如果同時進來的請求多於當前可用的請求處理執行緒數,額外的執行緒就會被建立,直到到達配置的最大執行緒數(maxThreads屬性值)。如果仍就同時接收到更多請求,這些來不及處理的請求就會在Connector建立的Server Socket中堆積起來,直到到達最大的配置值(acceptCount屬性值)。至此,任何再來的請求將會收到connection refused錯誤,直到有可用的資源來處理它們。

分析、梳理、組織

這裡我們關心的是tomcat能同時處理的請求數和請求響應時間,顯然Connector元素的maxThreads和acceptCount屬性對其有直接的影響。無論acceptCount值為多少,maxThreads直接決定了實際可同時處理的請求數。而不管maxThreads如何,acceptCount則決定了有多少請求可等待處理。然而,不管是可立即處理請求還是需要放入等待區,都需要tomcat先接受該請求(即接受client的連線請求,建立socket channel),那麼tomcat同時可建立的連線數(maxConnections屬性值)也會影響可同時處理的請求數。
我們可把tomcat想象成一家醫院,你來到醫院大廳掛號看病,如果人家接受了,就相當於client和server建立socket連線了。接著你來到相應的科室,科室裡每位醫生都有一間診室,這就相當於處理請求的執行緒;如果所有診室都有病人,科室的排程護士會讓你在科室小廳中耐心等待,直到他們通知你去幾號診室就診;如果有空閒醫生,你就可以立即就診。
有的病人到醫院很倉促,結果輪到他掛號或者就診了,他還在包裡翻找病例本和醫保卡,如果超過了護士或醫生心裡可承受的等待時間,他們就會讓病人到旁邊找去,先服務下位。這種情形跟Connector元素的connectionTimeout屬性所起的作用很相像。如果當前聯結器(Connector)在接受連線後,等待了指定的時間但仍未接收到request URI line,就會丟擲超時異常。

知識點收集

tomcat 7 的配置參考文件對相關屬性已經描述的很詳細了,這裡把它們收集到一起:

protocol

Sets the protocol to handle incoming traffic. The default value is HTTP/1.1 which uses an auto-switching mechanism to select either a blocking Java based connector or an APR/native based connector. If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. If the native library cannot be found, the blocking Java based connector will be used. Note that the APR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanism described above, the following values may be used:
org.apache.coyote.http11.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector.


Custom implementations may also be used.
Take a look at our Connector Comparison chart. The configuration for both Java connectors is identical, for http and https.
For more information on the APR connector and APR specific SSL settings please visit the APR documentation

maxThreads

The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool.

acceptCount

The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO the default is 10000. For APR/native, the default is 8192.
Note that for APR/native on Windows, the configured value will be reduced to the highest multiple of 1024 that is less than or equal to maxConnections. This is done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connections are not counted.

connectionTimeout

The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to false, this timeout will also be used when reading the request body (if any).

進一步分析

tomcat的http connector有三種:bio、nio、apr。從上面的屬性描述中可以看出對於不同的connector實現,相同的屬性可能會有不同的預設值和不同的處理策略,所以在調整配置前,要先弄清楚各種實現之間的不同,以及當前部署容器使用的是哪種connector。
查閱Tomcat 7 http connector 配置文件Connector Comparison部分便可獲知各種connector實現間的差異。
怎樣才能知道容器使用的是何種connector實現?啟動tomcat後,訪問Server Status Page,看到如下資訊即可知道使用的是何種connector:

我的OS是windows,所以tomcat預設使用的是apr connector。在linux上,預設使用的是bio connector。與nio相比,bio效能較低。將<TOMCAT_HOME>/conf/server.xml中的如下配置片段:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

修改為:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />