在程序固定之后, 不对程序优化,使用固有配置的设备,
提高并发的有效手段是添加硬件配置。
创建一个网站并测试QPS
先使用docker
创建一个php
网站。
- 创建目录
~/Program/test
- 添加文件
index.php
<?php
phpinfo();
- 创建容器
docker run -d -v ~/Program/test:/webwww/public --name web1 -p 8081:80 ssslocal/nginx-php7
-
访问
-
ab工具进行测试
$ ab -c 100 -n 10000 http://localhost:8081/ [23:08:11]
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
......
Server Software: nginx/1.10.3
Server Hostname: localhost
Server Port: 8081
Document Path: /
Document Length: 77452 bytes
Concurrency Level: 100
Time taken for tests: 30.652 seconds
Complete requests: 10000
Failed requests: 1052
(Connect: 2, Receive: 0, Length: 1050, Exceptions: 0)
Non-2xx responses: 6
Total transferred: 775965086 bytes
HTML transferred: 774055260 bytes
Requests per second: 326.24 [#/sec] (mean)
Time per request: 306.520 [ms] (mean)
Time per request: 3.065 [ms] (mean, across all concurrent requests)
Transfer rate: 24721.97 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 29.0 0 1097
Processing: 5 304 25.8 301 561
Waiting: 5 303 25.7 300 560
Total: 5 305 40.1 301 1490
......
可以看到QPS
为326
组建一个nginx
反向代理集群
- 使用容器创建第二个网站
docker run -d --name web2 -v ~/Program/test:/webwww/public
-p 8082:80 ssslocal/nginx-php7
- 添加集群配置文件
- 先获取配置
$ docker run --name tmp-nginx-container -d nginx $ docker cp tmp-nginx-container:/etc/nginx/nginx.conf ~/Program/test/nginx.conf $ docker rm -f tmp-nginx-container
修改配置nginx.conf
本次测试无需修改
添加配置,~/Program/test/default.conf
upstream hello{ server web1:80 weight=1; server web2:80 weight=1; } server { listen 80; server_name www.if404.com; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log error; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://hello; } }
- 启动一个集群
nginx
容器
docker run -d --name web -v ~/Program/test/default.conf:/etc/nginx/conf.d/default.conf --link web1:web1 --link web2:web2 -p 8083:80 nginx
验证
$ ab -c 100 -n 10000 http://localhost:8083/ [0:10:32]
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
......
Server Software: nginx/1.15.9
Server Hostname: localhost
Server Port: 8083
Document Path: /
Document Length: 77705 bytes
Concurrency Level: 100
Time taken for tests: 29.604 seconds
Complete requests: 10000
Failed requests: 9098
(Connect: 0, Receive: 0, Length: 9098, Exceptions: 0)
Non-2xx responses: 5
Total transferred: 778581151 bytes
HTML transferred: 776671296 bytes
Requests per second: 337.80 [#/sec] (mean)
Time per request: 296.035 [ms] (mean)
Time per request: 2.960 [ms] (mean, across all concurrent requests)
Transfer rate: 25683.86 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 6
Processing: 9 295 105.7 302 567
Waiting: 9 292 105.5 300 563
Total: 9 295 105.6 303 567
......
可以看到QPS
为337
网友评论