存档

文章标签 ‘Apache’

APACHE自带的测试工具AB说明

2009年9月10日 没有评论

APACHE自带的测试工具AB(apache benchmark).ab的全称是ApacheBench Apache 附带的一个小工具专门用于 HTTP Server benchmark testing可以同时模拟多个并发请求。
在APACHE的bin目录下。
格式: ./ab [options] [http://]hostname[:port]/path
参数:
-n requests     Number of requests to perform
//在测试会话中所执行的请求个数。默认时,仅执行一个请求
-c concurrency Number of multiple requests to make
//一次产生的请求个数。默认是一次一个。
-t timelimit    Seconds to max. wait for responses
//测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-p postfile     File containing data to POST
//包含了需要POST的数据的文件.
-T content-type Content-type header for POSTing .
//POST数据所使用的Content-type头信息。
-v verbosity    How much troubleshooting info to print
//设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
-w              Print out results in HTML tables
//以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-i              Use HEAD instead of GET
// 执行HEAD请求,而不是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)
//-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。
-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.

一个简单的例子及报告的一些说明

/*在这个例子的一开始,我执行了这样一个命令 ab -n 10 -c 10 http://www.google.com/这个命令的意思是启动 ab ,向 www.google.com 发送10个请求(-n 10) ,并每次发送10个请求(-c 10)——也就是说一次都发过去了。跟着下面的是 ab 输出的测试报告,红色部分是我添加的注释。*/

C:\Program Files\Apache Software Foundation\Apache2.2\bin>ab -n 10 -c 10 http

://www.google.com/

This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Copyright 1997-2005 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.google.com (be patient)…..done

Server Software: GWS/2.1

Server Hostname: www.google.com

Server Port: 80

Document Path: /

Document Length: 230 bytes

Concurrency Level: 10

/*整个测试持续的时间*/

Time taken for tests: 3.234651 seconds

/*完成的请求数量*/

Complete requests: 10

/*失败的请求数量*/

Failed requests: 0

Write errors: 0

Non-2xx responses: 10

Keep-Alive requests: 10

/*整个场景中的网络传输量*/

Total transferred: 6020 bytes

/*整个场景中的HTML内容传输量*/

HTML transferred: 2300 bytes

/*大家最关心的指标之一,相当于 LR 中的 每秒事务数 ,后面括号中的 mean 表示这是一个平均值*/

Requests per second: 3.09 [#/sec] (mean)

/*大家最关心的指标之二,相当于 LR 中的 平均事务响应时间 ,后面括号中的 mean 表示这是一个平均值*/

Time per request: 3234.651 [ms] (mean)

/*这个还不知道是什么意思,有知道的朋友请留言,谢谢 ^_^ */

Time per request: 323.465 [ms] (mean, across all concurrent requests)

/*平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题*/

Transfer rate: 1.55 [Kbytes/sec] received

/*网络上消耗的时间的分解,各项数据的具体算法还不是很清楚*/

Connection Times (ms)

min mean[+/-sd] median max

Connect: 20 318 926.1 30 2954

Processing: 40 2160 1462.0 3034 3154

Waiting: 40 2160 1462.0 3034 3154

Total: 60 2479 1276.4 3064 3184

/*下面的内容为整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中 50 的用户响应时间小于 3064 毫秒,60 的用户响应时间小于 3094 毫秒,最大的响应时间小于 3184 毫秒*/

Percentage of the requests served within a certain time (ms)

50% 3064

66% 3094

75% 3124

80% 3154

90% 3184

95% 3184

98% 3184

99% 3184

100% 3184 (longest request)

分类: Apache 标签: , ,

apache启动、终止、重启

2009年8月6日 没有评论

apache之httpd启动、终止、重启小结 

How apache starts

如果配置文件中的Listen directive设置了默认端口为80(或者是其它的值,但要小于1024),接下来apache httpd就需要root权限来启动apache,这是因为在将应用进程绑定在(1-1024)这个保留端口范围内的时候,需要root权限。当 server一旦启动并且执行了些许初步动作,比如打开log日志文件,接下来server会装载执行指定数目的child processes, 这些process是用来复杂监听端口,处理来自client的请求并且返回响应。而主httpd process仍然以root权限继续运行,但是这些child processes会以较低的权限运行,而这些行为都可以通过选择Multi-processing Module来进行控制。
启动httpd main process的推荐方式是使用apachectl 控制脚本。这个脚本用来帮助你自动设置environment set,这些是httpd main process运行时所需要的;然后apachectl才会继续执行httpd的ke执行的binary code。apachectl可以接受命令行参数, 所以任何httpd的命令行参数对apachectl一样生效(从这一点,可以把apachectl看成httpd的壳, apachectl无非是转发相应的命令行参数给httpd)。你也同样可以修改apachectl脚本,在apachectl脚本中,HTTPD 变量用来指定httpd executable 的有效路径。

httpd开始运行后的第一件事是读取httpd.conf文件。httpd.conf是编译时候确定的,也可以通过-f命令行option来进行指定。如:
/usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf

如 果startup一切正常,则server将会脱离terminal(其stdin,stdout,stderr不再和terminal的0,1,2相关 联);这就说明apache httpd启动成功。然后你可以使用browser来验证DocumentRoot下的文件证实httpd server是否启动成功。

apache启动时侯出错:
如果apache在启动的时候出现fatal error, 则它会将出错提示信息发送个console或者ErrorLog directive 指定的日志文件。最一般出现的问题是:Unable to bind to Port ...,它可能是已经启动了一个httpd process, 或者不是root权限。更进步的trouble-shooting,参加 Apache FAQ.

在boot-time 启动apache server:

在os重新启动后自动重启apache server, 可以在 /etc/rc.N 或者/etc/rc.local中添加对apachectl脚本的调用

apachectl 脚本被设计成类似于标准的sysv init script, 它可以接受start,restart和stop命令行参数,并且把它们转换成适当的signals以传递给httpd。所以可以将apachectl连 接到/etc/init.d目录下。 如

link -s /usr/local/apache2/bin/apachectl /etc/init.d/apachectl

apache的启动和重启命令:

为 了restart或者stop Apache server, 你必须给运行的httpd processes发送信号。有两种方法可以发送信号给httpd processes。首先,你可以使用kill command来直接发送信号给httpd process。你会发现有许多httpd进程处于运行状态,但是除了parent httpd process, 你不能将signals发送给其它的httpd进程,他的pid是由PidFile来定义的(PidFile标识的文件内保存了parent process的pid, 默认是/usr/local/apache2/logs/httpd.pid).有四个signals可以发送给parent httpd process:TERM, USR1, HUP和WINCH。

命令如下: kill -TERM `cat /usr/local/apache2/logs/httpd.pid`

第二种方法是使用-k 命令行选项: stop,restart, graceful 和 graceful-stop.
使用命令: ./apachectl -k stop|restart|graceful|graceful-stop
当你发送signals给httpd来结束httpd运行, 通过查看/usr/local/apache2/logs/error_logs 来检查执行的结果

tail -f /usr/local/apache2/logs/error_log

stop:

Signal: TERM
apachectl -k stop

立即发送TERM或者stop signals给httpd parent processes ,来结束httpd parent process和它的子process。这个过程会持续几秒种。

优雅的restart(graceful restart)

USR1 或者graceful signals发送给parent process,来建议parent的子进程结束运行或者在它们处理完当前正在处理的请求后结束运行(如果child没有做任何事情这马上停止运行)。然 后parent会重新读取apache的配置文件、重新打开它的log文件。当每个child结束执行,parent将会用根据配置而新产生的 process来替换每个child,这些新的child来继续服务http requests。

apache的httpd被设计成尊重MPM的process control directive的配置,所以通过重新启动,可用的服务于client请求的process和threads的数量会被保证在一个适当的数值。此外,对应服务器的StartServers directive 配置,httpd会有如下动作:在一秒钟以后,如果新生成的children process的数量少有StartServers指定的值,则httpd parent会创建足够的children来一同完成服务client的任务。因此,httpd试图在维护足够数量的children来满足server 的负载的同时,同时尊重StartServers参数的设置的值。

使用apache mod_status的用户会注意到服务器统计的数值没有被设置为0当USR1被发送的时候。httpd的代码的编写为了保证Server由于重启而导致 无法对client的requests进行处理时间的最小化(这些由于重启而无法处理的请求会被os发到os维护的队列当中去, 所以没有event会被丢失),同时满足你调优Servers的目的(更改配置,重新启动apache httpd server)。 为了达到这个目的,在children重新生成的过程中,应该保持用于跟踪children的scoreboard。

这个mod_status module,使用G来标记这些children任然在服务在graceful restart请求被发出前还没有处理完成的client的请求。

命令:apachectl -k graceful

restart now

Signal: HUP
apachectl -k restart

发送HUP或restart 信号给parent httpd process,所有子进程会被杀死,而parent process不会exit。apache httpd会重新读取配置文件,然后重新打开log file。 然后parent httpd会spawns(fork and exec)一组新的children process集合用来服务client请求。

WINCH and graceful-stop

Signal : WINCH

apachectl -k graceful-stop


WINCH或者graceful-stop信号会使得parent process试图建议children在处理完当前请求(或者立即结束如果它们已经不再处理任何请求)结束运行状态。parent process然后将会删除它的PidFile文件,终止监听对所有它曾经关注过的端口。 然后,parent process会继续运行,监听所有仍然有请求任务的子进程。当所有的children进程都执行结束并且exit,或者已经过了由 GracefulShutdownTimeout指定的timeout时间,parent将会结束运行并且exit。如果timeout时间到了,则每个 子进程都会收到TERM signal,然后会强制它们exit。 如果process和它的children处于graceful状态,则TERM signal会使得它们马上结束运行。然而,由于PidFile被删除,所以导致你不能通过apachectl和httpd来放松信号(kill 可以)。

分类: Apache 标签: ,

开启Apache Server Status

2009年3月11日 没有评论

Apache的日志又多又杂,如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗。其实在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-status

打开Apache Server Status
如果你的Apache配置文件httpd.conf或者extra/httpd-info.conf中有LoadModule status_module modules/mod_status.so话,说明你的Apache已经加载了此模块;或者编译的时候加上了–enable-module=so也表明 服务器支持server-status。

如果Apache没有加载这个模块,如果是linux服务器,就得重新编译Apache,加上–enable-module=so参数即可;如果你 是windows系统的话,无需任何编译,只要把刚才时候说的LoadModule status_module modules/mod_status.so这句加上,如果前面有带#号,开启的话,需要将#去除。

配置Apache Server Status

<location /ccvita-server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from www.ccvita.com
</location>
ExtendedStatus On

这是一个完整的server-status的配置。
第一行的ccvita-server-status表示以后可以用类似http://www.ccvita.com/ccvita-server-status来访问,同时

http://www.ccvita.com/ccvita-server-status?refresh=N将表示访问状态页面可以每N秒自动刷新一次;

Deny from表示禁止的访问地址;
Allow from表示允许的地址访问;
ExtendedStatus On表示的是待会访问的时候能看到详细的请求信息,另外该设置仅能用于全局设置,不能在特定的虚拟主机中打开或关闭。启用扩展状态信息将会导致服务器运行效率降低。