The Web server provides a persistent connection. The so-called persistent connection means that the client does not close the connection after a request is completed, and the connection is maintained for a period of time. The next time the client requests again, you do not need to create a new connection, and reuse the maintained connection. In theory, persistent connections can save you from consuming a lot of resources to establish and close connections, but there is also a lot of cost of connection occupation. Therefore, it can be preliminarily determined that the persistent connection can bring higher TPS, lower CPU consumption, less Io, and higher memory usage than the short connection. The actual situation is verified as follows.
For the server environment and test tools, see tools and environment preparation.
The Web server uses Apache prefork mode. You can configure the keepalive option in httpd. conf for Apache persistent connections, for example:
Keepalive on: persistent connection
Keepalive off: short connection
In addition, if persistent connections are selected, keepalivetimeout and maxkeepaliverequests must be configured. keepalivetimeout is the duration of each persistent connection server. The default value is 15 seconds, and maxkeepaliverequests is the maximum number of requests for each persistent connection service, the default value is 100, which is maintained by default in this test.
Use AB to press Apache, such:
In transient connection:/usr/Alibaba/install/httpd-2.0.63-prefork/bin/AB-C 100-N 1000000 http: // localhost/
Persistent connection environment:/usr/Alibaba/install/httpd-2.0.63-prefork/bin/AB-C 100-N 1000000-KHttp: // localhost/
At the same time, 100 concurrent requests are sent to the Apache default homepage for 1000000 times.
Then, the differences between short connections and long connections are investigated using three dimensions: AB test results, nmon resource consumption collection, and strace trace actual calls.
1) AB test results
Transient connection:
-
- Concurrency level:100
-
- Time takenForTests:190.754776Seconds
-
- Complete requests:1000000
-
- Failed requests:0
- Write errors:0
-
- Total transferred:1891115351Bytes
-
- HTML transferred:1456088816Bytes
-
- Requests per second:5242.33[#/Sec] (mean)
-
- Time per request:19.075[MS] (mean)
- Time per request:0.191[MS] (mean, internal SS all concurrent requests)
-
- Transfer Rate:9681.50[Kbytes/sec] specified ed
-
- Connection times (MS)
-
- Min mean [+/-SD] median Max
- Connect:083.7844
-
- Processing:1103.8979
- Waiting:073.0761
-
- Total:4185.717101
Persistent connection:
- Concurrency level:100
-
- Time takenForTests:59.509558Seconds
-
- Complete requests:1000000
-
- Failed requests:0
-
- Write errors:0
- Keep-alive requests:990148
-
- Total transferred:1927566346Bytes
-
- HTML transferred:1456007280Bytes
-
- Requests per second:16804.02[#/Sec] (mean)
-
- Time per request:5.951[MS] (mean)
- Time per request:0.060[MS] (mean, internal SS all concurrent requests)
-
- Transfer Rate:31631.71[Kbytes/sec] specified ed
-
- Connection times (MS)
-
- Min mean [+/-SD] median Max
- Connect:000.1012
-
- Processing:0522.511406
- Waiting:0522.411405
-
- Total:0522.511409
If other parameters are the same as those in the environment, the persistent connection is much higher than the TPs of the short connection, 16804.02/sec vs 5242.33/sec, in addition, it is not difficult to find that the time spent on a persistent connection is almost 0.
2) nmonTest Results
CPU consumption:
Transient connection
Persistent connection
The above data indicates that long connections consume less CPU than short connections
Io usage:
Transient connection
Persistent connection
The above data indicates that long connections consume less I/O than short connections.
Idle memory:
Transient connection
Persistent connection
The above data indicates that the persistent connection occupies more memory than the short connection.
3) strace result
In the prefork mode of Apache, each request is responded by a separate sub-process. Therefore, the number of times of calling system resources is compared by tracking one of the sub-processes.
Transient connection:
-
- % Time seconds usecs/call callerrors syscall
- --------------------------------------------------------------
-
- 44.240.187941199997Accept
-
- 40.220.1708871017738Poll
- 2.580.01097606771617737Read
-
- 2.490.0105830599649994Lstat
- 2.190.0093190499709994Stat
-
- 1.740.007388039976Setsockopt
- 1.420.00604519997Shutdown
-
- 1.250.005312029988Close
- 1.060.004499019989Open
-
- 0.710.003003019994Fcntl
- 0.570.00242609994Write
-
- 0.450.00191109994Writev
- 0.380.00159809994Sendfile
-
- 0.350.00150309997Getsockname
- 0.340.00143909997Gettimeofday
-
- 0.000.00000212Fstat
- 0.000.00000111Lseek
-
- 0.000.00000111MMAP
- 0.000.00000111Munmap
-
- --------------------------------------------------------------
- 100.000.42483537531037725Total
Persistent connection:
-
- % Time seconds usecs/call callerrors syscall
-
- --------------------------------------------------------------
- 37.050.03299739919Write
-
- 21.900.01950329940Poll
- 10.380.009248039676Setsockopt
-
- 7.860.0070000495959919Stat
- 7.460.0066420595149919Lstat
-
- 5.350.0047640497209941Read
- 3.540.003156019839Open
-
- 2.270.00201809919Sendfile
- 1.950.001735019941Close
-
- 1.280.00114309919Writev
- 0.920.00081609921Gettimeofday
-
- 0.020.0000140200Fcntl
- 0.010.0000070100Accept
-
- 0.010.0000070100Getsockname
- 0.010.00000601001Shutdown
-
- 0.000.00000212Fstat
- 0.000.00000111Lseek
-
- 0.000.00000111MMAP
- 0.000.00000111Munmap
-
- --------------------------------------------------------------
- 100.000.08906128840829780Total
The above data shows that the number of long connections and shutdown is only 100, while the number of short connections is 9997, which is nearly 100 times different, it is not difficult to find out why the TPs of persistent connections is so high that it saves so many system calls.
Theoretical analysis started from this experiment:Persistent connections provide higher TPS, lower CPU consumption, less Io, higher memory usage, and less system calls than short connections.