Nginx Concurrency Estimation
Estimation formula: {(?G)*1024-system} / request size
?G: Memory size (in GB)1024: Standard memory capacity conversion factor (MB to GB)system: Memory occupied by the system and services, plus reserved memoryrequest size: Size of a request, typically in KB for static content or MB for dynamic content
A 16-core 32GB server can handle over 40,000 concurrent connections for load balancing, with a maximum capacity of 50,000–60,000 connections, saturtaing file descriptors.
Stress Testing Tool AB
1. Installing AB
[root@nginx-lua ~]# yum install httpd-tools -y
2. Basic Usage
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/
// -n: total number of requests
// -c: number of concurrent requests
// -k: enable keep-alive connections
3. Parameter Explanation
[root@Nginx-lua conf.d]# ab -n2000 -c2 http://127.0.0.1/index.html
...
Server Software: nginx/1.12.2
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /index.html
Document Length: 19 bytes
Concurrency Level: 200
# Total time spent
Time taken for tests: 1.013 seconds
# Total number of requests
Complete requests: 2000
# Number of failed requests
Failed requests: 0
Write errors: 0
Total transferred: 510000 bytes
HTML transferred: 38000 bytes
# Requests per second (total requests / total time)
Requests per second: 9333.23 [#/sec] (mean)
# Average time per request for the client
Time per request: 101.315 [ms] (mean)
# Average time per request for the server
Time per request: 0.507 [ms] (mean, across all concurrent requests)
# Transfer rate, useful for identifying network bottlenecks
Transfer rate: 491.58 [Kbytes/sec] received
Viewing Concurrent Connections and Connection States
1. Check Concurrent Requests and TCP Connection States of Web Servers (Nginx/Apache)
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}'
Typical output:
LAST_ACK 5 (waiting for the last ACK)
SYN_RECV 30 (connection request received, waiting for confirmation)
ESTABLISHED 1597 (normal data transmission)
FIN_WAIT1 51 (application has initiated termination)
FIN_WAIT2 504 (other side agrees to release)
TIME_WAIT 1057 (waiting for termination packets to expire)
Description of TCP States
- CLOSED: No active or ongoing connection
- LISTEN: Server waiting for incoming connection
- SYN_RECV: A connection request arrived, waiting for acknowledgment
- SYN_SENT: Application started, attempting to open a connection
- ESTABLISHED: Normal data transmission state
- FIN_WAIT1: Application indicates it has finished
- FIN_WAIT2: Other side agrees to release
- ITMED_WAIT: Waiting for all packets to expire
- CLOSING: Both sides attempt to close simultaneously
- TIME_WAIT: Other side initiated a release
- LAST_ACK: Waiting for all packets to expire
2. Check Number of Running Processes for Nginx or Apache
ps -ef | grep nginx | wc -l
ps -ef | grep httpd | wc -l
3. Check Number of Established Connections to the Web Server
netstat -antp | grep 80 | grep ESTABLISHED -c