qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          如何測(cè)試Nginx的高性能

          簡(jiǎn)介
            Nginx ("engine x") 是一個(gè)高性能的HTTP和反向代理服務(wù)器,也是一個(gè)IMAP/POP3/SMTP代理服務(wù)器;
            作為一款輕量級(jí)的Web服務(wù)器,具有占有內(nèi)存少,并發(fā)能力強(qiáng)等優(yōu)勢(shì),是高連接并發(fā)場(chǎng)景下Apache的不錯(cuò)的替代品;
            本篇主要介紹Nginx作為Web服務(wù)器時(shí),相對(duì)于Apache的性能優(yōu)勢(shì);
            下一篇將會(huì)介紹Nginx作為方向代理服務(wù)器的實(shí)現(xiàn);
            重要特點(diǎn)
            非阻塞:數(shù)據(jù)復(fù)制時(shí),磁盤(pán)I/O的第一階段是非阻塞的;
            事件驅(qū)動(dòng):通信機(jī)制采用epoll模型,支持更大的并發(fā)連接;
            master/worker結(jié)構(gòu):一個(gè)master進(jìn)程,生成一個(gè)或多個(gè)worker進(jìn)程;
            基礎(chǔ)架構(gòu)
            Nginx如何實(shí)現(xiàn)高并發(fā):
            I/O模型采用異步非阻塞的事件驅(qū)動(dòng)機(jī)制,由進(jìn)程循環(huán)處理多個(gè)準(zhǔn)備好的事件,如epoll機(jī)制;
            Nginx與Apache對(duì)高并發(fā)處理上的區(qū)別:
            對(duì)于Apache,每個(gè)請(qǐng)求都會(huì)獨(dú)占一個(gè)工作線程,當(dāng)并發(fā)量增大時(shí),也會(huì)產(chǎn)生大量的工作線程,導(dǎo)致內(nèi)存占用急劇上升,同時(shí)線程的上下文切換也會(huì)導(dǎo)致CPU開(kāi)銷增大,導(dǎo)致在高并發(fā)場(chǎng)景下性能下降嚴(yán)重;
            對(duì)于Nginx,一個(gè)worker進(jìn)程只有一個(gè)主線程,通過(guò)事件驅(qū)動(dòng)機(jī)制,實(shí)現(xiàn)循環(huán)處理多個(gè)準(zhǔn)備好的事件,從而實(shí)現(xiàn)輕量級(jí)和高并發(fā);
            部署配置
            安裝
          yum -y groupinstall “Development tools”
          yum -y groupinstall “Server Platform Development”
          yum install gcc openssl-devel pcre-devel zlib-devel
          groupadd -r nginx
          useradd -r -g nginx -s /sbin/nologin -M nginx
          tar xf nginx-1.4.7.tar.gz
          cd nginx-1.4.7
          mkdir -pv /var/tmp/nginx
          ./configure \
          --prefix=/usr \
          --sbin-path=/usr/sbin/nginx \
          --conf-path=/etc/nginx/nginx.conf \
          --error-log-path=/var/log/nginx/error.log \
          --http-log-path=/var/log/nginx/access.log \
          --pid-path=/var/run/nginx/nginx.pid  \
          --lock-path=/var/lock/nginx.lock \
          --user=nginx \
          --group=nginx \
          --with-http_ssl_module \
          --with-http_flv_module \
          --with-http_stub_status_module \
          --with-http_gzip_static_module \
          --http-client-body-temp-path=/var/tmp/nginx/client/ \
          --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
          --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
          --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
          --http-scgi-temp-path=/var/tmp/nginx/scgi \
          --with-pcre
          make && make install
          配置:
          vi /etc/init.d/nginx # 配置服務(wù)腳本
          #!/bin/sh
          #
          # nginx - this script starts and stops the nginx daemon
          #
          # chkconfig:   - 85 15
          # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
          #               proxy and IMAP/POP3 proxy server
          # processname: nginx
          # config:      /etc/nginx/nginx.conf
          # config:      /etc/sysconfig/nginx
          # pidfile:     /var/run/nginx.pid
          # Source function library.
          . /etc/rc.d/init.d/functions
          # Source networking configuration.
          . /etc/sysconfig/network
          # Check that networking is up.
          [ "$NETWORKING" = "no" ] && exit 0
          nginx="/usr/sbin/nginx"
          prog=$(basename $nginx)
          NGINX_CONF_FILE="/etc/nginx/nginx.conf"
          [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
          lockfile=/var/lock/subsys/nginx
          make_dirs() {
          # make required directories
          user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
          options=`$nginx -V 2>&1 | grep 'configure arguments:'`
          for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
          value=`echo $opt | cut -d "=" -f 2`
          if [ ! -d "$value" ]; then
          # echo "creating" $value
          mkdir -p $value && chown -R $user $value
          fi
          fi
          done
          }
          start() {
          [ -x $nginx ] || exit 5
          [ -f $NGINX_CONF_FILE ] || exit 6
          make_dirs
          echo -n $"Starting $prog: "
          daemon $nginx -c $NGINX_CONF_FILE
          retval=$?
          echo
          [ $retval -eq 0 ] && touch $lockfile
          return $retval
          }
          stop() {
          echo -n $"Stopping $prog: "
          killproc $prog -QUIT
          retval=$?
          echo
          [ $retval -eq 0 ] && rm -f $lockfile
          return $retval
          }
          restart() {
          configtest || return $?
          stop
          sleep 1
          start
          }
          reload() {
          configtest || return $?
          echo -n $"Reloading $prog: "
          killproc $nginx -HUP
          RETVAL=$?
          echo
          }
          force_reload() {
          restart
          }
          configtest() {
          $nginx -t -c $NGINX_CONF_FILE
          }
          rh_status() {
          status $prog
          }
          rh_status_q() {
          rh_status >/dev/null 2>&1
          }
          case "$1" in
          start)
          rh_status_q && exit 0
          $1
          ;;
          stop)
          rh_status_q || exit 0
          $1
          ;;
          restart|configtest)
          $1
          ;;
          reload)
          rh_status_q || exit 7
          $1
          ;;
          force-reload)
          force_reload
          ;;
          status)
          rh_status
          ;;
          condrestart|try-restart)
          rh_status_q || exit 0
          ;;
          *)
          echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
          exit 2
          esac
          chmod +x /etc/init.d/nginx # 復(fù)***務(wù)腳本執(zhí)行權(quán)限
          vi /etc/nginx/nginx.conf # 編輯主配置文件
          worker_processes  2;
          error_log  /var/log/nginx/nginx.error.log;
          pid        /var/run/nginx.pid;
          events {
          worker_connections  1024;
          }
          http {
          include       mime.types;
          default_type  application/octet-stream;
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';
          sendfile        on;
          keepalive_timeout  65;
          server {
          listen       80;
          server_name  xxrenzhe.lnmmp.com;
          access_log  /var/log/nginx/nginx.access.log  main;
          location / {
          root   /www/lnmmp.com;
          index  index.php index.html index.htm;
          }
          error_page  404              /404.html;
          error_page  500 502 503 504  /50x.html;
          location = /50x.html {
          root   /www/lnmmp.com;
          }
          location ~ \.php$ {
          root           /www/lnmmp.com;
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
          }
          }
          }
          vi /etc/nginx/fastcgi_params # 編輯fastcgi參數(shù)文件
          fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
          fastcgi_param  SERVER_SOFTWARE    nginx;
          fastcgi_param  QUERY_STRING       $query_string;
          fastcgi_param  REQUEST_METHOD     $request_method;
          fastcgi_param  CONTENT_TYPE       $content_type;
          fastcgi_param  CONTENT_LENGTH     $content_length;
          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
          fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
          fastcgi_param  REQUEST_URI        $request_uri;
          fastcgi_param  DOCUMENT_URI       $document_uri;
          fastcgi_param  DOCUMENT_ROOT      $document_root;
          fastcgi_param  SERVER_PROTOCOL    $server_protocol;
          fastcgi_param  REMOTE_ADDR        $remote_addr;
          fastcgi_param  REMOTE_PORT        $remote_port;
          fastcgi_param  SERVER_ADDR        $server_addr;
          fastcgi_param  SERVER_PORT        $server_port;
          fastcgi_param  SERVER_NAME        $server_name;
            啟動(dòng)服務(wù):
            service nginx configtest # 服務(wù)啟動(dòng)前先驗(yàn)證配置文件是否正確
            service nginx start
            ps -ef |grep nginx # 檢查nginx進(jìn)程,尤其是worker進(jìn)程是否與worker_processes值一致
            ss -antupl |grep 80 # 檢查服務(wù)端口是否啟動(dòng)
            性能測(cè)試
            測(cè)試說(shuō)明
            每次測(cè)試都進(jìn)行3次,最后數(shù)據(jù)取平均值;
            對(duì)比測(cè)試中的Apache采用event的MPM機(jī)制,最大化提高Apache的并發(fā)性能;
            每次測(cè)試后,都需重新啟動(dòng)服務(wù)(httpd或nginx),以防止多次測(cè)試數(shù)據(jù)不準(zhǔn);
            測(cè)試工具:webbench
            優(yōu)點(diǎn):比ab能更好的模擬并發(fā)請(qǐng)求,最大支持模擬30000并發(fā)連接;
           測(cè)試方法
          # 安裝wenbench
          wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
          tar xf webbench-1.5.tar.gz
          cd webbench-1.5
          make && make install
          # 測(cè)試
          webbench -c 100 -t 30 http://172.16.25.112/nginx.html # 測(cè)試靜態(tài)文件訪問(wèn)
          webbench -c 20 -t 30 http://172.16.25.112/test_mem.php # 測(cè)試動(dòng)態(tài)文件訪問(wèn)
            測(cè)試數(shù)據(jù)
            分析趨勢(shì)圖
            靜態(tài)文件訪問(wèn)趨勢(shì)圖
            動(dòng)態(tài)文件訪問(wèn)趨勢(shì)圖
            
            總結(jié)
            綜合上面測(cè)試得出的趨勢(shì)圖可以看出:
            靜態(tài)文件測(cè)試時(shí),低并發(fā)(200以下)情況下,Nginx和Apach的處理能力相當(dāng)(2000pages/sec左右),當(dāng)并發(fā)數(shù)超過(guò)200后,則 Apache的處理能力開(kāi)始下降,而Nginx保持穩(wěn)定;同時(shí)隨著并發(fā)量的增大,Apache令人詬病的內(nèi)存占用和負(fù)載開(kāi)始急劇上升,與此同 時(shí),Nginx在內(nèi)存占用和負(fù)載方面的略微提升則可以忽略不計(jì)了;
            動(dòng)態(tài)文件測(cè)試時(shí),低并發(fā) (100以下)情況下,Nginx和Apache的處理能力相當(dāng)(650pages/sec左右),但Nginx的內(nèi)存占用和負(fù)載峰值只有Apache的 50%左右;在高并發(fā)情況下(100以上),Apach的動(dòng)態(tài)處理能力開(kāi)始下滑,當(dāng)并發(fā)達(dá)到500時(shí),開(kāi)始出現(xiàn)失敗的請(qǐng)求,說(shuō)明此時(shí)已達(dá)到的Apache 的處理上限了,而反觀Nginx,雖然處理動(dòng)態(tài)請(qǐng)求會(huì)消耗更多的內(nèi)存,但其處理能力隨著并發(fā)量的上升而上升,即使并發(fā)1000動(dòng)態(tài)請(qǐng)求,也未達(dá)到其處理能 力上限;
            故不管是在靜態(tài)文件請(qǐng)求還是動(dòng)態(tài)文件請(qǐng)求方面,Nginx的性能都是強(qiáng)勢(shì)優(yōu)于Apache的;雖然可以通過(guò)系統(tǒng)調(diào)優(yōu)的方式提高Apache的處理性能,但和Nginx相比,還是不足以打動(dòng)技術(shù)狂熱份子的吧,哈哈!

          posted on 2014-05-23 10:04 順其自然EVO 閱讀(2380) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 治县。| 乌鲁木齐县| 盐山县| 疏勒县| 台南市| 台前县| 河北区| 江口县| 中阳县| 茌平县| 仁寿县| 清水县| 保定市| 宝坻区| 辽中县| 溧水县| 虹口区| 西林县| 南部县| 思南县| 永州市| 郸城县| 吕梁市| 瓮安县| 隆回县| 枞阳县| 深水埗区| 墨竹工卡县| 长沙市| 崇礼县| 崇左市| 庄河市| 常宁市| 平山县| 桂东县| 井研县| 贺兰县| 长海县| 建平县| 达拉特旗| 丹棱县|