Building a high-performance web-Web Server persistent connection

Source: Internet
Author: User
Tags lstat sendfile

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:

 

  1. Concurrency level:100
  2. Time takenForTests:190.754776Seconds
  3. Complete requests:1000000
  4. Failed requests:0
  5. Write errors:0
  6. Total transferred:1891115351Bytes
  7. HTML transferred:1456088816Bytes
  8. Requests per second:5242.33[#/Sec] (mean)
  9. Time per request:19.075[MS] (mean)
  10. Time per request:0.191[MS] (mean, internal SS all concurrent requests)
  11. Transfer Rate:9681.50[Kbytes/sec] specified ed
  12. Connection times (MS)
  13. Min mean [+/-SD] median Max
  14. Connect:083.7844
  15. Processing:1103.8979
  16. Waiting:073.0761
  17. Total:4185.717101

 

Persistent connection:

 

  1. Concurrency level:100
  2. Time takenForTests:59.509558Seconds
  3. Complete requests:1000000
  4. Failed requests:0
  5. Write errors:0
  6. Keep-alive requests:990148
  7. Total transferred:1927566346Bytes
  8. HTML transferred:1456007280Bytes
  9. Requests per second:16804.02[#/Sec] (mean)
  10. Time per request:5.951[MS] (mean)
  11. Time per request:0.060[MS] (mean, internal SS all concurrent requests)
  12. Transfer Rate:31631.71[Kbytes/sec] specified ed
  13. Connection times (MS)
  14. Min mean [+/-SD] median Max
  15. Connect:000.1012
  16. Processing:0522.511406
  17. Waiting:0522.411405
  18. 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:

 

  1. % Time seconds usecs/call callerrors syscall
  2. --------------------------------------------------------------
  3.  44.240.187941199997Accept
  4.  40.220.1708871017738Poll
  5. 2.580.01097606771617737Read
  6. 2.490.0105830599649994Lstat
  7. 2.190.0093190499709994Stat
  8. 1.740.007388039976Setsockopt
  9. 1.420.00604519997Shutdown
  10. 1.250.005312029988Close
  11. 1.060.004499019989Open
  12. 0.710.003003019994Fcntl
  13. 0.570.00242609994Write
  14. 0.450.00191109994Writev
  15. 0.380.00159809994Sendfile
  16. 0.350.00150309997Getsockname
  17. 0.340.00143909997Gettimeofday
  18. 0.000.00000212Fstat
  19. 0.000.00000111Lseek
  20. 0.000.00000111MMAP
  21. 0.000.00000111Munmap
  22. --------------------------------------------------------------
  23. 100.000.42483537531037725Total

 

Persistent connection:

 

  1. % Time seconds usecs/call callerrors syscall
  2. --------------------------------------------------------------
  3.  37.050.03299739919Write
  4.  21.900.01950329940Poll
  5.  10.380.009248039676Setsockopt
  6. 7.860.0070000495959919Stat
  7. 7.460.0066420595149919Lstat
  8. 5.350.0047640497209941Read
  9. 3.540.003156019839Open
  10. 2.270.00201809919Sendfile
  11. 1.950.001735019941Close
  12. 1.280.00114309919Writev
  13. 0.920.00081609921Gettimeofday
  14. 0.020.0000140200Fcntl
  15. 0.010.0000070100Accept
  16. 0.010.0000070100Getsockname
  17. 0.010.00000601001Shutdown
  18. 0.000.00000212Fstat
  19. 0.000.00000111Lseek
  20. 0.000.00000111MMAP
  21. 0.000.00000111Munmap
  22. --------------------------------------------------------------
  23. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.