3月 15

网站压力测试微型工具技术备忘

[记录整理:张子萌 2010-03-15]

经常对服务器单个模块做非专业性压力测试,所以学习了两个小巧大的工具,做技术备忘。
如果需要做比较详细数据请使用LoadRunner、QALoad、JMeter等专业工具。

一款是apahe自带的ab工具。一款是webbench。

1.apache自带ab工具

此工具为apache自带可以显示较多测试信息,
http://apache.freelamp.com/httpd/httpd-2.2.15.tar.gz
以下编译只为获得ab工具,不作为apache安装使用,编译时添加安装路径,是为方便
不小心安装后删除方便。
# cd httpd-2.2.15
# ./configure –prefix=/usr/local/apache_ab
# make
# cd support/.libs/
在目录下已经可以看到ab程序了,ab程序主要参数解释如下

格式: ./ab [options] [http://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
# 在默认会话中发送请求的个数,默认时为50000
-c concurrency Number of multiple requests to make
# 一次请求所产生的次数
-t timelimit Seconds to max. wait for responses
# 测试所进行的最大秒数。默认时,没有时间c和n参数限制请求完毕为止。
-b windowsize Size of TCP send/receive buffer, in bytes
# 发送和接受包大小
-w Print out results in HTML tables
# 以HTML表的格式输出结果
-v verbosity How much troubleshooting info to print
# 打印头部信息,参数为输出级别1-4
-i Use HEAD instead of GET
# 使用head请求不是get请求
-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.
# web使用庄户密码
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
# 对代理使用账户密码
-X proxy:port Proxyserver and port number to use
# 使用代理服务器

开始测试主要使用参数也就两个,以下对部分结果做解释
# ./ab -c 1000 -n 1000 http://172.192.1.100/
# url结尾如不是文件(如:index.html),一定要增加“/”
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.192.1.100 (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
Server Hostname: 172.192.1.100
Server Port: 80

Document Path: /
Document Length: 2731 bytes
# 传输单个文档大小

Concurrency Level: 1000
# 并发数
Time taken for tests: 3.320 seconds
# 本次测试持续的时间
Complete requests: 1000
# 本次测试完成请求数量
Failed requests: 0
# 本此测试失败数量
Write errors: 0
Total transferred: 3039360 bytes
HTML transferred: 2749800 bytes
Requests per second: 301.18 [#/sec] (mean)
# 每秒钟处理数量的平均值
Time per request: 3320.263 [ms] (mean)
# 处理请求的平均响应时间
Time per request: 3.320 [ms] (mean, across all concurrent requests)
# 每个请求实际运行时间的平均值
Transfer rate: 893.94 [Kbytes/sec] received
# 平均每秒网络流量

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 200 701.6 26 3000
Processing: 1 275 373.3 254 3280
Waiting: 1 275 373.2 254 3280
Total: 1 475 735.5 302 3316
# 网络响应时间,主要为connect时间。
# 以下为请求处理时间的分布

Percentage of the requests served within a certain time (ms)
50% 302
66% 348
75% 362
80% 369
90% 410
95% 3008
98% 3016
99% 3311
100% 3316 (longest request)

2.webbench工具
获取地址:
http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
获取版本进行编译
# tar zxvf webbench-1.5.tar.gz
# cd webbench-1.5
# make
在本目录下可以看到webbench软件,webbench程序主要参数如下
格式:webbench [option]… URL
-f|–force Don’t wait for reply from server.
-r|–reload &
nbsp; Send reload request – Pragma: no-cache.
-t|–time Run benchmark for seconds. Default 30.
-p|–proxy Use proxy server for request.
-c|–clients Run 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.

使用100个并发持续5秒钟
#./webbench -c 100 -t 5 http://www.google.com/
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.google.com/
100 clients, running 5 sec.

Speed=36 pages/min, 4289 bytes/sec.
Requests: 3 susceed, 0 failed.

3.两款工具对比

ab工具显示测试数据比webbench详细。但是ab可测试并发量小于webbench,ab并发使用
不要超过1024(主要受到apache配置和系统资源限制),webbench并发使用数量不要超过10000,
如果超出此方为可能会引起测试机短暂假死现象,且不能获得返回数据。此两宽小型软件已经
够用,对于网上常见的Siege,http_load等就不看了。

3月 12

nginx 0.8 安装 配置

【整理人:张子萌 最近一次修改2010-12】

nginx的官方网站:http://nginx.org/

1.下载nginx源码
nginx-0.8.34.tar.gz

如果希望在Nginx中使用正则表达式使配置更灵活,需要安装PCRE。
查看是否安装pcre
# rpm -qa pcre*

如果希望在是用ssl加密部分请安装openssl。

如果希望压缩传送需要安装gzip

如果使用nginx使用的缓存,需要清理指定url需要下载并同时编译。
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz

推荐使用升级工具安装,升级工具使用可以参照:
http://simon-zzm.blog.163.com/blog/static/88809522201025102237958/

2. 编译安装nginx
首先创建账户,指定用户与组的id均为700
# groupadd -g 700 nginx
# useradd -g700 -u700 nginx
解压、安装nginx,对于不需要的模块可以不编译。
最后一行为清理指定url模块,可以选择性编译
# tar zxvf ngx_cache_purge-1.0.tar.gz
# tar zxvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure –prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-pcre
–with-select_module
–with-poll_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_gzip_static_module
–add-module=/usr/local/src/ngx_cache_purge-1.0
# make && make install

3. nginx配置文件

主要配置文件:/usr/local/nginx/conf/nginx.conf
# 程序运行用户
user nginx nginx;
# 启动进程数
worker_processes 8;

#给每个进程分配cpu,例:将8个进程分配到8个cpu,也可以写多个,或将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# 全局错误日志和程序PID文件
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

# 工作模式及连接数上限
# 工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
# poll(高效模式。适用Linux 2.6+,SuSE 8.2,Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
events {
use poll;
worker_connections 1024;
}

# 设定http服务器,反向代理功能提供负载均衡支持
http {
#设定mime类型
include mime.types;
default_type application/octet-stream;

# 隐藏nginx版本,防止攻击
server_tokens off;
# 设定日志格式
#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
#access_log logs/access.log main;
#linux下强大的静态文件发送功能,一定要开启
sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;

# 定义一个叫“mysvr”的记录区,总容量为 10M
# 和下面location中的limit_conn一起限制单个IP的并发连接数为10
# limit_zone只能是用在http中,limit_conn可以使用在http、sever、local中
limit_zone mysvr $binary_remote_addr 10m;

#设定负载均衡的服务器列表
upstream mysvr {
ip_hash #如果不需要可以删除直接采用轮训方法负载
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.0.1:80 weight=5;
server 192.168.0.2:80 weight=1;
server 192.168.0.3:80 weight=6;
server 192.168.0.5:80;
server 192.168.0.6:80;

#down 表示单前的server暂时不参与负载
#weight 默认为1.weight越大,负载的权重就越大。
#max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:max_fails次失败后,暂停的时间。
#backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
}
# 设定缓存临时目录,临时目录要与proxy_cache_path中目录一样
proxy_temp_path /usr/local/nginx_test/proxy_temp;
# proxy_cache_path中参数如下
# levels指定该缓存空间有两层hash目录,第一层目录是1个字母,第二层为2个字母
# keys_zone为这个空间起个名字,比如 cache_one,在server模块中proxy_cache使用
# 200m 内存缓存空间大小为200MB
# inactive 1天没有被访问的内容自动清除;
# max_size使用硬盘缓存大小30G
# clean_time指定一分钟清理一次缓存。
proxy_cache_path /usr/local/nginx_test/proxy_temp/ levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name 192.168.0.7 mysvr.c
om www.mysvr.com;
#charset gb2312;
#charset utf8;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #与后端连接超时时间
proxy_send_timeout 90; #后端服务器回传超时时间
proxy_read_timeout 90; #连成功后,后端服务器响应超时时间。
proxy_buffer_size 4k; #代理服务器保存用户头信息的缓存大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; #高负荷缓村大小。大小计算方法proxy_buffers*2
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值经直接从upstream获得。
limit_conn mysvr 10; #每个IP只能有10个并发
limit_rate 100k; #每个并发只能有100K的速度
index index.html index.htm; #选则文件后缀解析都优先顺序
expires 30d; #客户端缓存时间 30天

}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus

3月 10

linux 下用 cronolog 切割 apache日誌

cronolog安装可见http://simon-zzm.blog.163.com/blog/static/88809522201022112040647/

apache安装位置:/usr/local/apache/

编辑配置文件httpd.conf

LogFormat “%v %h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” mylog
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined
LogFormat “%h %l %u %t “%r” %>s %b” common


LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio

CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” combined

如果使用虚拟机可以使用以下配置

httpd-vhosts.conf

ErrorLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log”
CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” common

3月 02

linux 下用 cronolog 切割 tomcat catalina日誌

http://cronolog.org/下载cronolog软件。

cronolog-1.6.2.tar.gz

上传到服务器,操作如下:

# tar zxvf cronolog-1.6.2.tar.gz

# cd cronolog-1.6.2

# ./configure

# make && make install

如果安裝正常,cronolog安装完毕。开始修改tomcat。

编辑bin/catalina.sh文件。

首先找到“touch “$CATALINA_BASE”/logs/catalina.out”并注释掉,并编辑如下

elif [ “$1” = “start” ] ; then

shift
#touch “$CATALINA_BASE”/logs/catalina.out
if [ “$1” = “-security” ] ; then
echo “Using Security Manager”
shift
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Djava.security.manager
-Djava.security.policy==”$CATALINA_BASE”/conf/catalina.policy
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
else
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
fi

重新启动tomcat,日志分割格式如下:

catalina.yyyy-mm-dd.out

cronolog 安装后所在位置,系统版本不同可能位置不一样,以 which 或whereis命令查看到的位置为准。

7月 30

delphi多线程CreateThread函数

简单介绍多线程API 函数:
构建线程:

CreateThread(参数1,–安全属性(一般=Nil,默认安全属性)

参数2,–线程堆栈尺寸(一般=0,与主线程相同长度,而且可以根据需要自动变化)
参数3,–指向函数名指针,@函数名,这个参数十分重要,不正确将无法调用成功。
参数4,–用户需要向线程传递的参数,是一个指向结构的指针,不需传递参数时,为Nil。
参数5)–传入与线程有关的一些参数,例如:
Create_SUSPENDED 创建一个挂起的线程;
0 创建后立即激活。