淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

发布新日志

  • 一次 linux +apache + apache module 性能诊断以及调优过程

    2008-02-25 22:46:28

    一次    linux +apache + apache module 性能诊断以及调优过程

     

    第1章           系统架构 linux +apache + apache module 程序。

     

    第2章          具体的os 以及apache 信息

    [admin@test1 logs]$ uname  -a

    Linux test1 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:32:02 EDT 2006 x86_64 x86_64 x86_64 GNU/Linux

    [admin@test1 logs]$ /usr/local/apache2/bin/httpd  -l

    Compiled in modules:

      core.c

      mod_authn_file.c

      mod_authn_default.c

      mod_authz_host.c

      mod_authz_groupfile.c

      mod_authz_user.c

      mod_authz_default.c

      mod_auth_basic.c

      mod_include.c

      mod_filter.c

      mod_log_config.c

      mod_env.c

      mod_setenvif.c

      worker.c

      http_core.c

      mod_mime.c

      mod_status.c

      mod_autoindex.c

      mod_asis.c

      mod_cgid.c

      mod_negotiation.c

      mod_dir.c

      mod_actions.c

      mod_userdir.c

      mod_alias.c

      mod_so.c

    [admin@test1 logs]$ /usr/local/apache2/bin/httpd  -v

    Server version: Apache/2.2.6 (Unix)

    Server built:   Jan 31 2008 20:42:34

     

    第3章           性能场景设置

    36 vuser pacing: 0.25秒。 期望tps 140 /秒。

     

    第4章           调优之前

        httpd.conf为:

    <IfModule worker.c>

    StartServers        2

    ServerLimit         2

    MaxClients          20

    MinSpareThreads     10

    MaxSpareThreads     20

    ThreadsPerChild     10

    MaxRequestsPerChild  0

    </IfModule>

     

    性能测试结果如下:

     

     

     

     

     

     

     

     

    第5章           性能分析

    响应时间不满足性能需求。

     

    加入apache module 分析apache,接收到http接收到请求+处理+发送网络包的时间为:

     

    10.0.4.132 - - [21/Feb/2008:17:18:13 +0800] "GET /bin/search?news_gb?nk=Y&_PS=pt&_SS=id&n=1&series_id=59197&_id=1001028515 HTTP/1.1" 200 1547 "cost 0 ms"

    10.0.4.132 - - [21/Feb/2008:17:18:13 +0800] "GET /bin/search?news_gb?parents=3000103,&_PS=pt&n=5&q=tags%3A%22%C5%AE%CA%BD%B8%DF%D1%A5%22+&O=and&news_type=8,9,10,11,12,13,14,15&_id=1001292049 HTTP/1.1" 200 5162 "cost 2 ms"

    10.0.4.132 - - [21/Feb/2008:17:18:13 +0800] "GET /bin/search?news_gb?_PS=pt&s=4&n=6&q=tags%3A%22%B7%FE%D7%B0%B3%B1%C1%F7%22+&O=and&news_type=8,9,10,11,12,13,14,15 HTTP/1.1" 200 49647 "cost 6 ms"

    从上述看,apache 接到http 请求后的处理速度很快。

    另外,分析IO( si/so ) 、网络 (300Kbytes) CPU都不是特别高。

    [admin@test1 logs]$ vmstat 2  10

    procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----

     r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa

     0  0  17936 2320460   9680 2086580    0    5    54    59   27    14  2  1 96  1

     0  1  17936 2323468   9680 2088280    0    0   806     0 9948  1172 47 10 27 15

     2  2  17936 2321356   9680 2088960    0    0   314     0 8780  1023 36  8 48  8

     5  0  17936 2318348   9688 2090380    0    0   670   118 6669  1051 37  6 43 14

     3  0  17936 2285324   9688 2091876    0    0   754     0 11847  1405 37 12 35 16

     1  1  17936 2287260   9696 2092820    0    0   458   116 12115  1400 40 13 38  9

     0  1  17936 2298588   9696 2093160    0    0   140     0 8620   856 37  8 52  4

     0  2  17936 2300572   9696 2094928    0    0   872     0 8497  1145 34 12 41 12

     

    [admin@test1 logs]$ sar  -n  DEV 2  10

    Linux 2.6.9-42.ELsmp (test1)    02/21/2008

     

    05:27:20 PM     IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s

    05:27:22 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

    05:27:22 PM      eth0   2984.34   5652.02 182552.02 8365743.94      0.00      0.00      0.00

    05:27:22 PM    veth31      3.03      2.02    783.84    133.33      0.00      0.00      0.00

    05:27:22 PM      sit0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

     

    05:27:22 PM     IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s

    05:27:24 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

    05:27:24 PM      eth0   1989.05   3622.89 134824.38 5310582.09      0.00      0.00      0.00

    05:27:24 PM    veth31      5.47      2.49   4040.30   1696.52      0.00      0.00      0.00

    05:27:24 PM      sit0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

     

    05:27:24 PM     IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s

    05:27:26 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

    05:27:26 PM      eth0   2207.46   4199.00 145106.97 6175783.08      0.00      0.00      0.00

    05:27:26 PM    veth31      5.97      3.98   1314.43   1018.41      0.00      0.00      0.00

    05:27:26 PM      sit0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

     

     

    由于loadrunner 设置36个并发。而httpd.conf MaxClients          20

    36 20 ( 线程),造成loadrunner 并发排队。

    诊断为并发数过高而httpd.conf 配置不当,导致apache httpd 线程阻塞,造成等待队列过长。

     

    第6章           调优

    程序不变。增加Maxclients

     

    更改httpd.conf 为:

    <IfModule worker.c>

    StartServers        2

    ServerLimit         2

    MaxClients          40

    MinSpareThreads     10

    MaxSpareThreads     20

    ThreadsPerChild     20

    MaxRequestsPerChild  0

    </IfModule>

     

    LoadModule responseCost_module /usr/local/apache2/modules/mod_responseCost.so

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{responseCost}n\"" common

     

    性能表现如下,可见吞吐率都大幅度提升,响应时间下降。

     

     

     

     

     

    如果是prefork 编译,则httpd.conf采用

    <IfModule prefork.c>

    ServerLimit         40

    StartServers         5

    MinSpareServers      5

    查看(469) 评论(1) 收藏 分享 管理

  • 编写apache module程序

    2007-03-16 21:18:59

    参考 http://threebit.net/tutorials/

    编译apache要求指定?

    ./configure --prefix=$INSTALLPATH/apache --enable-so

     

    (1) aclocal, autoconf, automake.生成所用的path,lib路径

    [liangjz@serch-dev-2 modules]$  cd /home/liangjz/apache/modules

    # import automake m4 macros.

    [liangjz@serch-dev-2 modules]$aclocal

    # create configure based on configure.in
    [liangjz@serch-dev-2 modules]$ autoconf

    # create Makefile.in based on Makefile.am and configure.in

    [liangjz@serch-dev-2 modules]$ automake -a


    (2) configure
    Now we can run configure to prepare the module's Makefile.
    # The ubiquitous configure scrīpt

    [liangjz@serch-dev-2 modules]$./configure --with-apache=/home/liangjz/apache

    (3) make
    And now we can run make to compile the module. Note: don't run make install. We'll handle the module installation later.

    [liangjz@serch-dev-2 modules]$ make
    source='mod_tut1.c' ōbject='mod_tut1.lo' libtool=yes \
    depfile='.deps/mod_tut1.Plo' tmpdepfile='.deps/mod_tut1.TPlo' \
    depmode=gcc3 /bin/sh ./depcomp \
    /bin/sh ./libtool --mode=compile gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"mod_tut1\" -DVERSION=\"1.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1  -I. -I. -I/home/liangjz/apache//include     -g -O2 -c -o mod_tut1.lo `test -f 'mod_tut1.c' || echo './'`mod_tut1.c
    ./libtool: ./libtool: 没有那个文件或目录
    make: *** [mod_tut1.lo] Error 127


    确认是由于libtool路径不对!修正Makefile
    #LIBTOOL = $(SHELL) $(top_builddir)/libtool
    LIBTOOL =/home/liangjz/apache/build/libtool

    (4) apxs
    ** DO NOT RUN make install ** Ordinarially you would, but the install step for an Apache module is different. Instead, apxs is used to register the module in httpd.conf and move the shared object into the apache lib directory.

    [liangjz@serch-dev-2 modules]$/home/liangjz/apache/bin/apxs -i -a -n tut1 libmodtut1.la
    apxs also addes the following line to httpd.conf:

    LoadModule tut1_module        modules/libmodtut1.so

    或者手工编辑

    LoadModule tut1_module        modules/libmodtut1.so
    <Location /bin/moduletest>
            SetHandler mod_tut1_method_handler     
    </Location>
    ?????????????????没有起到过滤作用!!!!

     

    <Location /bin/moduletest>
            SetHandler mod_tut2_method_handler     
    </Location>

    要求在程序内加入
    static int mod_tut2_method_handler (request_rec *r)
    {
            // Get the module configuration
      
     
            char buf[1024]={0};
            conn_rec  * p_conn_rec =NULL;
            modtut2_config *s_cfg = ap_get_module_config(r->server->module_config, &tut2_module);
            if (strcmp(r->handler, "mod_tut2_method_handler"))     //即不匹配/bin/moduletest拒绝服务
                    return DECLINED;

            // Send a message to the log file.
            fprintf(stderr,s_cfg->string);
            fprintf(stderr,"\n");

            // We need to flush the stream so that the message appears right away.
            // Performing an fflush() in a production system is not good for
            // performance - don't do this for real.
            fflush(stderr);

            // Return DECLINED so that the Apache core will keep looking for
            // other modules to handle this request.  This effectively makes
            // this module completely transparent.
            p_conn_rec=r->connection;
           
            sprintf(buf,"server=%s,client_ip=%s,method=%s,filename=%s,hander=%s\n",r->hostname,r->connection->remote_ip,r->method,r->filename,r->handler);
            //解析客户端并响应给浏览器
            ap_rputs(buf,r);
            return 0;
    }

    url: http:host:port/bin/moduletest/ 。 内部转发请求给

    (5)Run Apache
    Now we are ready to run Apache and test the module.
    # Change to the apache directory

    # Start Apache
    [liangjz@serch-dev-2 apache]$bin/apachectl start

    (6)ie浏览器发起http请求

    # hit the web server.

    http://10.0.4.81:2688/

    (7)检查输出
    # Look for the module's message in the error log

    [liangjz@serch-dev-2 apache$cat logs/error_log | grep tut1

    apache2_mod_tut1: A request was made.

     

    A下一步,弄清楚 path,lib,so之间关系!!!
    B 修正location的过滤作用!
    C 调试

     

    /usr/local/apache2/bin/apxs  -i -a  -c  mod_tut2.c

    [liangjz@serch-dev-2 modules]$ /home/liangjz/apache/bin/apxs  -i -a  -c  mod_tut2.c
    /home/liangjz/apache/build/libtool --silent --mode=compile gcc -prefer-pic  -DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -g -O2 -pthread -I/home/liangjz/apache/include  -I/home/liangjz/apache/include   -I/home/liangjz/apache/include   -c -o mod_tut2.lo mod_tut2.c && touch mod_tut2.slo
    /home/liangjz/apache/build/libtool --silent --mode=link gcc -o mod_tut2.la  -rpath /home/liangjz/apache/modules -module -avoid-version    mod_tut2.lo
    /home/liangjz/apache/build/instdso.sh SH_LIBTOOL='/home/liangjz/apache/build/libtool' mod_tut2.la /home/liangjz/apache/modules
    /home/liangjz/apache/build/libtool --mode=install cp mod_tut2.la /home/liangjz/apache/modules/
    cp .libs/mod_tut2.so /home/liangjz/apache/modules/mod_tut2.so
    cp .libs/mod_tut2.lai /home/liangjz/apache/modules/mod_tut2.la
    cp .libs/mod_tut2.a /home/liangjz/apache/modules/mod_tut2.a
    ranlib /home/liangjz/apache/modules/mod_tut2.a
    chmod 644 /home/liangjz/apache/modules/mod_tut2.a
    PATH="$PATH:/sbin" ldconfig -n /home/liangjz/apache/modules
    ----------------------------------------------------------------------
    Libraries have been installed in:
       /home/liangjz/apache/modules

    If you ever happen to want to link against installed libraries
    in a given directory, LIBDIR, you must either use libtool, and
    specify the full pathname of the library, or use the `-LLIBDIR'
    flag during linking and do at least one of the following:
       - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
         during execution
       - add LIBDIR to the `LD_RUN_PATH' environment variable
         during linking
       - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
       - have your system administrator add LIBDIR to `/etc/ld.so.conf'

    See any operating system documentation about shared libraries for
    more information, such as the ld(1) and ld.so(8) manual pages.
    ----------------------------------------------------------------------
    chmod 755 /home/liangjz/apache/modules/mod_tut2.so
    [activating module `tut2' in /home/liangjz/apache/conf/httpd.conf]
    [liangjz@serch-dev-2 modules]$ ls mod_tut2.so
    mod_tut2.so

    可以成功运行!

     

    或者


    [liangjz@serch-dev-2 modules]$ gcc  -c -O3 -I/home/liangjz/apache/include  -fPIC  -DLINUX=2 -DUSE_HSREGEX -DUSE_EXPAT -fPIC -DSHARED_MODULE  -o  mod_tut2.mod  mod_tut2.c  
    [liangjz@serch-dev-2 modules]$ gcc  -shared -L.  -L /home/liangjz/apache/lib/ -o libmodtut2.so  mod_tut2.mod
    [liangjz@serch-dev-2 modules]$ ls
    libmodtut2.so  mod_tut2.c  mod_tut2.mod


    或者
    gcc -fpic -DSHARED_MODULE -I/home/liangjz/apache/include -g -c mod_tut2.c
    ld -Bshareable -g -o libmodtut2.so  mod_tut2.o

    http://www.evance.name/tools/Apache-Chs/programs/apxs.html

     

     

     

     

     

     

     

     

     

    用gdb调试apache
    当前工作目录是 /home/jeson/apache2。 先apachectl stop关闭apache服务。


    1. gdb bin/httpd
    2. b ap_process_request        (设置断点)

    3. r -X -d /home/jeson/apache2/(指httpd.conf配置文件的上级目录conf的路径)   
     启动apache.
     
    4. 启动客户端,发送请求
     从ie发起http请求。

    5. 设置断点在指定的文件中  b  file:class:function

     

    实践:
    liangjz@serch-dev-2 modules]$ gcc -fpic -DSHARED_MODULE -I/home/liangjz/apache/include -g -c mod_tut2.c
    liangjz@serch-dev-2 modules]$ld -Bshareable -g -o libmodtut2.so  mod_tut2.o
    [liangjz@serch-dev-2 apache]$ pwd
    /home/liangjz/apache
    [liangjz@serch-dev-2 apache]$ gdb   bin/httpd
    GNU gdb Red Hat Linux (6.3.0.0-1.62rh)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

    (gdb) b
    No default breakpoint address now.
    (gdb) b ap_process_request
    Breakpoint 1 at 0x806eea7: file http_request.c, line 243.
    (gdb) r -X -d  /home/liangjz/apache
    Starting program: /home/liangjz/apache/bin/httpd -X -d  /home/liangjz/apache
    [Thread debugging using libthread_db enabled]
    [New Thread -1218543488 (LWP 16009)]
    [Switching to Thread -1218543488 (LWP 16009)]

    Breakpoint 1, ap_process_request (r=0x97efe20) at http_request.c:243
    243         if (ap_extended_status)
    (gdb) b /home/liangjz/apache/modules/mod_tut2.c:mod_tut2_method_handler
    Breakpoint 2 at 0xac34f2: file mod_tut2.c, line 55.
    (gdb) c
    Continuing.

    Breakpoint 2, mod_tut2_method_handler (r=0x97efe20) at mod_tut2.c:55
    55              modtut2_config *s_cfg = ap_get_module_config(r->server->module_config, &tut2_module);
    (gdb) n
    58              fprintf(stderr,s_cfg->string);

    (gdb) print  tut2_module
    $2 = {version = 20020903, minor_version = 9, module_index = 21, name = 0xac36e6 "mod_tut2.c",
      dynamic_load_handle = 0x9787b98, next = 0x80b55c0, magic = 1095774768, rewrite_args = 0,
      create_dir_config = 0, merge_dir_config = 0, create_server_config = 0xac35e1 <create_modtut2_config>,
      merge_server_config = 0, cmds = 0xac4700, register_hooks = 0xac355f <mod_tut2_register_hooks>}
    (gdb) detach
    Detaching from program: /home/liangjz/apache/bin/httpd, process 16009

    可以让httpd继续保持!!

Open Toolbar