服务器压力测试及简单性能优化

前言:服务器压力测试的工具有很多,比如Apache JMeter,webbench,Gatling,Tsung,Seige以及各大云服务提供商的在线压力测试平台。因为只是给自己服务器做一个小测试,没打算去搞事,因此全文压力测试只用到了Apache-utils ab。小插曲:高并发优化的先决条件是硬件性能而非请求处理优化,要现有过硬的设备再来讲究技巧。

安装压力测试模块

因为在安装Apache会带一个它自家的AB测试工具,因此只要安装了Apache一切都简单。加上Apache跨平台的特性,这个工具在Windows平台上也能运行。但是如果像我这种万年不用Apache的人,可以单独安装apache的工具包httpd-tools这个模块。
  • 模块安装
# yum -y install httpd-tools
安装后用以下这两个命令二选一看看是否安装成功。
# which ab // Write the full path of COMMAND(s) to standard output.

# ab -V // Print version number and exit.
  • 使用方法
# ab -n 100 -c 10 http://www.cubat.cc/
这行命令即用10的并发数对 http://www.cubat.cc/ 这个网站进行压力测试100次。自带的使用说明已经写得很详细了,因此只要看看说明都会使用
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make
    -t timelimit    Seconds to max. wait for responses
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -p postfile     File containing data to POST. Remember also to set -T
    -T content-type Content-type header for POSTing, eg.
        'application/x-www-form-urlencoded'
        Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
        Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
        are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
        are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -r              Don't exit on socket receive errors.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)

Ulimit 命令

Linux对于每个用户,系统限制其最大进程数。为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数。如果不修改最大进程数,ab测试一次也只能提交1024个请求,失去测试的意义。可以用 ulimit -a 来显示当前的各种用户进程限制。
于是来修改一下:
ulimit -n [Maximum processes numbers.] // By default, it sets as 1024 and will be reset after shutdown.
此处将该数值调大是为了提高套接字(socket)连接数和文件最多可以打开的数量。

开启NginX中的统计模块

因为NginX自带了统计模块,所以只需在编译安装时添加 --with-http_stub_status_module 即可。
开启的话,一般专门分配一个Location域作为统计的页面,比如这样
location /status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}
因为是一个统计页面,所以不需要被其他的IP地址访问,因此设置了除了本地例外(当然也可以设置一些其他的例外)把其他访问的IP地址全部过滤。
设置完之后可在浏览器打开 http://localhost/status 查看瞬时访问量。

高并发请求解决思路

  1. 套接字层面
    1. Nginx方向
      1. 调整子进程允许打开的连接数 worker_connections
      2. 调整每个worker连接池的最大keepalive等待延迟时间,即keepalive_timeout 0,注意,这个跟http里的keep-alive不是一回事。
    2. 系统方向
      1. 最大连接数 /proc/sys/net/core/somaxconn
      2. 是否打开TCP连接的回收 /proc/sys/net/ipv4/tcp_tw_recycle ( boolean )
      3. 是否允许TCP连接再利用 /proc/sys/net/ipv4/tcp_tw_reuse ( boolean )
      4. 是否抵御洪水攻击 /proc/sys/net/ipv4/tcp_syncookies ( boolean )
  2. 文件层面
    1. NginX方向 子进程允许打开的文件:将worker_limit_nofile [ Numbers] 填在worker_processes 之后,表示一个工作进程最大打开文件数。
    2. 系统方向 将系统允许同时处理文件数 ulimit -n 处填一个较大的值

参考资料

评论