项目——2——分布式构建LNMP+lb

在这里插入图片描述

一、分布式构建LNMP环境(wordpress)

内存:1G——处理器:1

ab192.168.1.150
nginx-lb192.168.1.134
web01192.168.1.128
web02192.168.1.132
mysql192.168.1.129
php192.168.1.130
nfs192.168.1.133

1、安装Nginx(web01):

使用Nginx官方提供的rpm包

[root@web01 ~]# vim  /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

2、使用第三方扩展epel源安装PHP7.2:

[root@php ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@php ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
							#可以使用这条命令替代上边的两条“yum -y install epel-release”

安装 php72 版本
[root@php ~]# yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm \
php72w-mysqlnd php72w-opcache

[root@php ~]# systemctl start php-fpm
[root@php ~]# systemctl enable php-fpm

3、安装mysql(mariadb):

[root@mysql ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
[root@mysql ~]# yum install mysql-community-server -y
											#本地安装可切换到对应文件夹使用命令:yum -y localinstall *
[root@mysql ~]# systemctl start mysqld
[root@mysql ~]# systemctl enable mysqld
[root@mysql ~]# mysql_secure_installation 		#修改mysql密码

4、搭建博客wordpress(基于LNMP)

1)下载wordpress源码包 4.9.8

https://www.lanzous.com/i1kfs6f

2)复制wordpress安装包,到虚拟机 / ,解压并赋权
[root@web01 ~]# unzip wordpress-4.9.4-zh_CN.zip
[root@web01 ~]# chmod -R 777 /wordpress
3)创建虚拟主机配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/blog.conf
server {
        listen 80;
        server_name blog.benet.com;
        root /wordpress;
        index index.php index.html;

        location ~ \.php$ {
                root /wordpress;
                fastcgi_pass 192.168.1.130:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

[root@web01 ~]# systemctl reload nginx
4)创建blog数据库和管理用户
[root@mysql ~]# mysql -uroot -ppwd123		#登录数据库
mysql > create database blog;		#创建数据库
mysql > grant all on blog.* to lisi@'192.168.1.%' identified by 'pwd123';		#设置管理用户及密码
5)在web01 上修改 blog 的配置文件,指定数据库服务器ip
先查看blog的配置位置: 
[root@lnmp ~]# cd /wordpress		#切换到blog网页根目录
[root@lnmp ~]# grep -R 123.com		#搜索保存密码的配置文件位置
[root@lnmp ~]# vim /wordpress/wp-config.php
define('DB_NAME', 'blog');			#WordPress数据库的名称 
define('DB_USER', 'lisi');			#MySQL数据库用户名 
define('DB_PASSWORD', '123.com');	#MySQL数据库密码 
define('DB_HOST', '192.168.1.129');	#新MySQL主机ip
6)修改php服务器的配置文件
[root@php ~]# vim /etc/php-fpm.d/www.conf
listen = 192.168.1.130:9000
listen.allowed_clients = 192.168.1.128

[root@php ~]# systemctl restart php-fpm		#重启php
7)从web01复制wordpress的目录到php服务器
[root@lnmp ~]# scp -rp /wordpress root@192.168.1.130:/
客户端通过域名访问blog,安装并配置
[root@ab ~]# vim /etc/hosts
192.168.1.128 blog.benet.com

效果图如下所示:
在这里插入图片描述
在这里插入图片描述

二、开启第二台 web02

1、安装Nginx(web):

使用Nginx官方提供的rpm包

[root@web01 ~]# vim  /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
复制所需文件到web02
复制虚拟主机配置文件到web02
[root@web01 ~]# scp -rp /etc/nginx/conf.d/blog.conf root@192.168.1.132:/etc/nginx/conf.d/
复制wordpress包到web02
[root@web01 ~]# scp -rp /wordpress/ root@192.168.1.132:/


[root@web02 ~]# systemctl restart nginx


修改php服务器的配置文件
[root@php ~]# vim /etc/php-fpm.d/www.conf
listen.allowed_clients = 192.168.1.128,192.168.1.132

[root@php ~]# systemctl restart php-fpm		#重启php

客户端通过修改域名访问blog,验证web02

[root@ab ~]# vim /etc/hosts
192.168.1.132 blog.benet.com

效果图如下所示:
在这里插入图片描述

三、搭建nfs共享服务器,把网站静态元素通过挂载方式放在nfs上

1)开启一台centos7,安装nfs-utils、rpcbind:

[root@nfs ~]# yum -y install nfs-utils  rpcbind

2)创建挂载点

[root@nfs ~]# mkdir -p /nfs/blog

3)发布共享目录

[root@nfs ~]# vim /etc/exports
/nfs/blog       192.168.1.0/24(rw,sync,no_root_squash)

4)重启nfs服务

[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs
5)在 web01、web02 服务器上查看nfs共享目录
[root@web01 ~]# showmount -e 192.168.1.133
Export list for 192.168.1.133:
/nfs/blog 192.168.1.0/24

[root@web02 ~]# showmount -e 192.168.1.133
Export list for 192.168.1.133:
/nfs/blog 192.168.1.0/24

6)把wordpress的内容目录挂载到nfs

[root@web01 ~]# cd /wordpress/
[root@web01 wordpress]# cp -rp wp-content/ wp-content.bak
[root@web01 wordpress]# mount -t nfs 192.168.1.133:/nfs/blog wp-content
[root@web01 wordpress]# cp -rp wp-content.bak/* wp-content/

[root@web02 ~]# cd /wordpress/
[root@web02 wordpress]# mount -t nfs 192.168.1.133:/nfs/blog wp-content/

7)设置永久挂载

[root@web01 wordpress]# vim /etc/fstab
192.168.1.133:/nfs/log  /wordpress/wp-content   nfs     defaults        0 0

[root@web02 wordpress]# vim /etc/fstab
192.168.1.133:/nfs/log  /wordpress/wp-content   nfs     defaults        0 0

8)验证nfs

[root@nfs ~]# ls /nfs/blog/
index.php  languages  plugins  themes

[root@web01 ~]# ls /wordpress/wp-content/
index.php  languages  plugins  themes

[root@web02 ~]# ls /wordpress/wp-content/
index.php  languages  plugins  themes

四、搭建nginx负载均衡

1)安装nginx

[root@nginx-lb ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@nginx-lb ~]# yum -y install nginx

2)配置负载均衡

[root@nginx-lb ~]# vim /etc/nginx/conf.d/lb.conf
upstream webcluster {
        server 192.168.1.128:80;
        server 192.168.1.132:80;
}
server {
        listen 80;
        server_name blog.benet.com;

        location / {
                proxy_pass      http://webcluster;
                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;
        }
}

[root@nginx-lb ~]# systemctl restart nginx

客户端通过修改域名访问blog,验证nginx-lb

[root@ab ~]# vim /etc/hosts
192.168.1.134 blog.benet.com

效果图如下所示:
在这里插入图片描述

压力测试

准备工作:

[root@ab ~]# yum -y install httpd-tools

[root@ab ~]# mkdir /server/soft -p
[root@ab ~]# cd /server/soft/
[root@ab soft]# wget http://www.ha97.com/code/webbench-1.5.tar.gz
[root@ab soft]# tar zxf webbench-1.5.tar.gz
[root@ab soft]# cd webbench-1.5/
[root@ab webbench-1.5]# make && make install
cc -Wall -ggdb -W -O   -c -o webbench.o webbench.c
webbench.c: 在函数‘alarm_handler’中:
webbench.c:77:31: 警告:未使用的参数‘signal’ [-Wunused-parameter]
 static void alarm_handler(int signal)
                               ^
cc -Wall -ggdb -W -O  -o webbench webbench.o  
ctags *.c
install -s webbench /usr/local/bin	
install -m 644 webbench.1 /usr/local/man/man1	
install: 无法创建普通文件"/usr/local/man/man1": 没有那个文件或目录
make: *** [install] 错误 1
[root@ab webbench-1.5]# cd /usr/local/
[root@ab local]# mkdir man
[root@ab local]# webbench --help
webbench [option]... URL
  -f|--force               Don't wait for reply from server.
  -r|--reload              Send reload request - Pragma: no-cache.
  -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.
  -p|--proxy <server:port> Use proxy server for request.
  -c|--clients <n>         Run <n> HTTP clients at once. Default one.
  -9|--http09              Use HTTP/0.9 style requests.
  -1|--http10              Use HTTP/1.0 protocol.
  -2|--http11              Use HTTP/1.1 protocol.
  --get                    Use GET request method.
  --head                   Use HEAD request method.
  --options                Use OPTIONS request method.
  --trace                  Use TRACE request method.
  -?|-h|--help             This information.
  -V|--version             Display program version.

一、优化前测试

1、ab优化前测试:

1)200并发,1000请求
[root@ab ~]# ab -c 200 -n 1000 http://blog.benet.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blog.benet.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.18.0
Server Hostname:        blog.benet.com
Server Port:            80

Document Path:          /
Document Length:        53176 bytes

Concurrency Level:      200
Time taken for tests:   64.814 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      53419000 bytes
HTML transferred:       53176000 bytes
Requests per second:    15.43 [#/sec] (mean)
Time per request:       12962.839 [ms] (mean)
Time per request:       64.814 [ms] (mean, across all concurrent requests)
Transfer rate:          804.87 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   7.5      0      29
Processing:   333 7173 5608.1   6190   62064
Waiting:      310 7064 5572.8   6096   62060
Total:        361 7177 5608.3   6190   62064

Percentage of the requests served within a certain time (ms)
  50%   6190
  66%   6576
  75%   7199
  80%   7487
  90%   8888
  95%  13640
  98%  31914
  99%  37722
 100%  62064 (longest request)
2)1000并发,100000请求
[root@ab ~]# ab -c 1000 -n 100000 http://blog.benet.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blog.benet.com (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        nginx/1.18.0
Server Hostname:        blog.benet.com
Server Port:            80

Document Path:          /
Document Length:        177 bytes

Concurrency Level:      1000
Time taken for tests:   121.139 seconds
Complete requests:      100000
Failed requests:        44376
   (Connect: 0, Receive: 0, Length: 44376, Exceptions: 0)
Write errors:           0
Non-2xx responses:      97558
Total transferred:      162263385 bytes
HTML transferred:       146284908 bytes
Requests per second:    825.50 [#/sec] (mean)
Time per request:       1211.393 [ms] (mean)
Time per request:       1.211 [ms] (mean, across all concurrent requests)
Transfer rate:          1308.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  417 4002.5      1   63258
Processing:     2  545 4359.3     46  102839
Waiting:        2  540 4348.5     45  102783
Total:          3  962 6081.9     49  102877

Percentage of the requests served within a certain time (ms)
  50%     49
  66%     59
  75%     74
  80%     94
  90%    532
  95%   1290
  98%   9448
  99%  31971
 100%  102877 (longest request)
3)1000并发,200000请求
[root@ab ~]# ab -c 1000 -n 200000 http://blog.benet.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blog.benet.com (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
apr_socket_recv: Connection timed out (110)
Total of 191815 requests completed

2、webbench优化前测试:

1)1000个并发连接持续60秒
[root@ab ~]# webbench -c 1000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
1000 clients, running 60 sec.

Speed=15147 pages/min, 2087093 bytes/sec.
Requests: 15147 susceed, 0 failed.



[root@web01 ~]# top
top - 23:34:47 up  2:17,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 148 total,   3 running, 145 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  2.0 sy,  0.0 ni, 95.9 id,  0.0 wa,  0.0 hi,  1.7 si,  0.0 st
KiB Mem :   999936 total,    89028 free,   407016 used,   503892 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   380436 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  3082 nginx     20   0   53928   7416   1188 R  3.0  0.7   0:25.98 nginx 



[root@web02 ~]# top
top - 23:34:57 up  1:27,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 103 total,   1 running, 102 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  2.0 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
KiB Mem :   995896 total,   550608 free,   100896 used,   344392 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   715296 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  7318 nginx     20   0   48280   3732   1020 S  2.7  0.4   0:28.47 nginx 
2)5000个并发连接持续60秒
[root@ab ~]# webbench -c 5000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
5000 clients, running 60 sec.

Speed=6778 pages/min, 2384211 bytes/sec.
Requests: 6546 susceed, 232 failed.



top - 23:40:59 up  2:24,  2 users,  load average: 0.02, 0.04, 0.05
Tasks: 147 total,   1 running, 146 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  2.4 sy,  0.0 ni, 95.9 id,  0.0 wa,  0.0 hi,  1.4 si,  0.0 st
KiB Mem :   999936 total,    89056 free,   405272 used,   505608 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   380920 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  3082 nginx     20   0   52024   5512   1188 S  2.3  0.6   0:32.18 nginx



top - 23:41:13 up  1:33,  1 user,  load average: 0.02, 0.02, 0.05
Tasks: 103 total,   2 running, 101 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  1.4 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem :   995896 total,   545408 free,   104576 used,   345912 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   710776 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  7318 nginx     20   0   51760   7344   1020 S  2.7  0.7   0:36.49 nginx
3)7000个并发连接持续60秒
[root@ab ~]# webbench -c 7000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
7000 clients, running 60 sec.

Speed=16195 pages/min, 3126017 bytes/sec.
Requests: 16128 susceed, 67 failed.



top - 23:45:21 up  2:28,  2 users,  load average: 0.00, 0.02, 0.05
Tasks: 147 total,   2 running, 145 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  1.7 sy,  0.0 ni, 96.6 id,  0.0 wa,  0.0 hi,  1.4 si,  0.0 st
KiB Mem :   999936 total,    86696 free,   406876 used,   506364 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   379064 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  3082 nginx     20   0   53476   7096   1188 R  3.0  0.7   0:37.40 nginx



top - 23:45:34 up  1:38,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 102 total,   2 running, 100 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  2.0 sy,  0.0 ni, 96.9 id,  0.0 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem :   995896 total,   548800 free,   101356 used,   345740 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   714432 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                               
  7318 nginx     20   0   48728   4176   1020 S  2.7  0.4   0:41.18 nginx

二、优化

[root@web01 ~]# vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    use epoll;
    worker_connections 1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;			#并不会让nginx执行的速度更快,关闭它可隐藏错误页面中的nginx版本号

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    server_tokens off;

    sendfile        on;
    tcp_nopush     on;

#连接超时时间——主要目的是保护服务器资源, CPU,内存,控制连接数,因为建立连接也是需要消耗资源的
    keepalive_timeout  65;				#客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
    tcp_nodelay on;							#防止网络阻塞,不过要包涵在 keepalived 参数才有效
    client_header_buffer_size 4k;			#客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过 1k,不过由于一般系统分页都要大于 1k,所以这里设置为分页大小。分页大小可以用命令 getconf PAGESIZE 取得
    open_file_cache max=102400 inactive=20s;			#这个将为打开文件指定缓存,默认是没有启用的, max 指定缓存数量,建议和打开文件数一致, inactive 是指经过多长时间文件没被请求后删除缓存
    open_file_cache_valid 30s;				#这个是指多长时间检查一次缓存的有效信息
    open_file_cache_min_uses 1;			#open_file_cache 指令中的 inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在 inactive 时间内一次没被使用,它将被移除
    client_header_timeout 15;				#设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据, nginx 将返回 request time out 的错误
    client_body_timeout 15;					#设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示
    reset_timedout_connection on;			#告诉 nginx 关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间
    send_timeout 15;								#响应客户端超时时间
    client_max_body_size 10m;				#上传文件大小限制

#使用 gzip 压缩功能,可以节约带宽,加快传输速度,有更好的体验,也可以节约成本
    gzip on;						#开启压缩功能
    gzip_min_length 2k;			#设置允许压缩的页面最小字节数,页面字节数从 header 头的 Content-Length 中获取,默认值是 0,不管页面多大都进行压缩,建议设置成大于 1K,如果小与 1K 可能会越压越大
    gzip_buffers 4 32k;			#压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储 gzip 压缩结果			
    gzip_http_version 1.1;			#压缩版本, 用于设置识别 HTTP 协议版本,默认是 1.1,目前大部分浏览器已经支持 GZIP 解压,使用默认即可
    gzip_comp_level 6;				#压缩比例,用来指定 GZIP 压缩比, 1 压缩比最小,处理速度最快, 9 压缩比最大,传输速度快,但是处理慢,也比较消耗 CPU 资源
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;		#用来指定压缩的类型, ‘text/html’类型总是会被压缩
    gzip_vary on;			#vary header 支持,改选项可以让前端的缓存服务器缓存经过 GZIP 压缩的页面,例如用 Squid 缓存经过 nginx 压缩的数据
    gzip_proxied any;

    include /etc/nginx/conf.d/*.conf;
}

三、优化后压力测试:

3、ab优化后测试:

1)200并发,1000请求
[root@ab ~]# ab -c 200 -n 1000 http://blog.benet.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blog.benet.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.18.0
Server Hostname:        blog.benet.com
Server Port:            80

Document Path:          /
Document Length:        53176 bytes

Concurrency Level:      200
Time taken for tests:   41.858 seconds
Complete requests:      1000
Failed requests:        4
   (Connect: 0, Receive: 0, Length: 4, Exceptions: 0)
Write errors:           0
Non-2xx responses:      4
Total transferred:      53229440 bytes
HTML transferred:       52963896 bytes
Requests per second:    23.89 [#/sec] (mean)
Time per request:       8371.624 [ms] (mean)
Time per request:       41.858 [ms] (mean, across all concurrent requests)
Transfer rate:          1241.86 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5   9.9      0      42
Processing:  2049 5830 2899.6   5000   31109
Waiting:     1948 5753 2903.0   4941   31097
Total:       2082 5835 2899.6   5007   31109

Percentage of the requests served within a certain time (ms )
  50%   5007
  66%   5506
  75%   5840
  80%   6182
  90%   7789
  95%  11118
  98%  14930
  99%  19875
 100%  31109 (longest request)
2)1000并发,100000请求
[root@ab ~]# ab -c 1000 -n 100000 http://blog.benet.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blog.benet.com (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
apr_socket_recv: Connection timed out (110)
Total of 82194 requests completed

4、webbench优化后测试:

1)1000个并发连接持续60秒
[root@ab ~]# webbench -c 1000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
1000 clients, running 60 sec.

Speed=9640 pages/min, 1794311 bytes/sec.
Requests: 9640 susceed, 0 failed.



top - 02:29:06 up  5:12,  3 users,  load average: 0.03, 0.02, 0.05
Tasks: 150 total,   1 running, 149 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  2.4 sy,  0.0 ni, 94.9 id,  0.0 wa,  0.0 hi,  2.4 si,  0.0 st
KiB Mem :   999936 total,    71844 free,   411352 used,   516740 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   374664 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 43543 nginx     20   0   54804   8272   1160 S  4.0  0.8   0:24.84 nginx



top - 02:29:18 up  4:21,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 104 total,   3 running, 101 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  1.4 sy,  0.0 ni, 97.6 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
KiB Mem :   995896 total,   541592 free,   104052 used,   350252 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   711724 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 18229 nginx     20   0   48888   4448   1000 R  2.3  0.4   0:35.18 nginx
2)5000个并发连接持续60秒
[root@ab ~]# webbench -c 5000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
5000 clients, running 60 sec.

Speed=35090 pages/min, 1812542 bytes/sec.
Requests: 34837 susceed, 253 failed.



top - 02:27:29 up  5:10,  3 users,  load average: 0.01, 0.02, 0.05
Tasks: 150 total,   1 running, 149 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  2.0 sy,  0.0 ni, 96.3 id,  0.0 wa,  0.0 hi,  1.4 si,  0.0 st
KiB Mem :   999936 total,    75220 free,   408812 used,   515904 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   377804 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 43543 nginx     20   0   52560   6160   1160 S  2.7  0.6   0:23.10 nginx



top - 02:27:39 up  4:20,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 104 total,   2 running, 102 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  1.4 sy,  0.0 ni, 97.6 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
KiB Mem :   995896 total,   540124 free,   105248 used,   350524 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   710124 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 18229 nginx     20   0   50172   5732   1000 S  2.3  0.6   0:33.48 nginx
3)7000个并发连接持续60秒
[root@ab ~]#  webbench -c 7000 -t 60 http://blog.benet.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.benet.com/
7000 clients, running 60 sec.

Speed=17017 pages/min, 3366524 bytes/sec.
Requests: 16478 susceed, 539 failed.



top - 02:24:35 up  5:07,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 150 total,   1 running, 149 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  0.7 sy,  0.0 ni, 98.6 id,  0.0 wa,  0.0 hi,  0.3 si,  0.0 st
KiB Mem :   999936 total,    78896 free,   407104 used,   513936 buff/cache
KiB Swap:  4063228 total,  4063180 free,       48 used.   381120 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 43543 nginx     20   0   50976   4576   1160 S  1.3  0.5   0:19.50 nginx




top - 02:24:47 up  4:17,  2 users,  load average: 0.01, 0.02, 0.05
Tasks: 103 total,   1 running, 102 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  2.0 sy,  0.0 ni, 96.6 id,  0.0 wa,  0.0 hi,  1.4 si,  0.0 st
KiB Mem :   995896 total,   536284 free,   108308 used,   351304 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   706160 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                             
 18229 nginx     20   0   53228   8788   1000 S  2.7  0.9   0:30.40 nginx

四、优化前后压力测试对比

1、ab压力测试前后对比

1)200并发,1000请求
参数优化前优化后
Concurrency Level并发请求数200200
Time taken for tests64.814 seconds41.858 seconds
Complete requests:10001000
Failed requests:04
Requests per second15.43 [#/sec]23.89 [#/sec]
Time per request:12962.839 [ms]8371.624 [ms]
Time per request:64.814 [ms]41.858 [ms]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值