Centos7 编译测试工具 wrk bombardier iftop

查看硬件信息

---------------------------------------------------------------------------------------------------------------------------------------------

主机   dmidecode|grep "Product Name"

CPU  cat /proc/cpuinfo |grep name

内存  cat /proc/meminfo

网卡  dmesg|grep -i eth

系统版本  cat /etc/redhat-release

查看物理cpu个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l

查看核心数量  grep 'core id' /proc/cpuinfo | sort -u | wc -l

查看线程数 grep 'processor' /proc/cpuinfo | sort -u | wc -l

查看硬盘空间占用  du -h -x --max-depth=1

文件描述符 ulimit -a 

应用配置内核参数  sysctl -p

模拟获取 referrer

curl -voa http://cdn.abc.com/a_s.png -s http://www.abc.com

 

BASH脚本测试

while true;do curl -L http://192.168.0.134:8080/;sleep 1.5;done

 

关闭进程

$ ps -ef | grep nginx | grep -v grep | cut -c 9-15 | xargs kill -s 9

$ pgrep nginx | xargs kill -s 9

$ kill -s 9 `ps -aux | grep nginx | awk '{print $2}'`

$ pkill -9 nginx

 

1.wrk 安装及使用
---------------------------------------------------------------------------------------------------------------------------------------------

[root@centos ~]#  cd /usr/local/src

[root@centos ~]#  yum install git -y

[root@centos ~]#  git clone https://github.com/wg/wrk.git

[root@centos ~]# cd wrk

[root@centos ~]# make

[root@centos ~]# ln -s /usr/local/src/wrk/wrk /usr/local/bin

[root@centos ~]# wrk -t 2 -c 50 -d 20 --latency http://localhost:5000

参数说明:

-t 需要模拟的线程数
-c 需要模拟的连接数
-d 测试的持续时间
--timeout 超时的时间
--latency 显示延迟统计

结果显示说明:
Latency:响应时间
Req/Sec:每个线程每秒钟的完成的请求数
Avg:平均
Max:最大
Stdev:标准差

 

2.bombardier 安装及使用

---------------------------------------------------------------------------------------------------------------------------------------------

https://github.com/codesenberg/bombardier

[root@centos ~]# cd /usr/local/src

[root@centos ~]# wget https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz

[root@centos ~]# tar -C /opt -xzf go1.12.7.linux-amd64.tar.gz

[root@centos ~]# export PATH=$PATH:/opt/go/bin

[root@centos ~]# export GOPATH=/data/go

[root@centos ~]# go version && go env

[root@centos ~]# go get -u github.com/codesenberg/bombardier

[root@centos ~]# ln -s /data/go/bin/bombardier /bin

[root@centos ~]# bombardier -c 125 -n 10000 http://localhost:5000/api/values/5

Bombarding http://localhost:5000/api/values/5 with 10000 request(s) using 125 connection(s)
10000 / 10000 [================================================] 100.00% 19s
Done!
Statistics Avg Stdev Max
Reqs/sec 517.05 675.48 11754.62
Latency 175.86ms 0.92s 16.87s
HTTP codes:
1xx - 0, 2xx - 10000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 181.42KB/s

[root@centos ~]# bombardier -c 200 -ds -l http://localhost:5000/api/values/5

 

3.iftop 安装及使用

---------------------------------------------------------------------------------------------------------------------------------------------

[root@centos ~]# yum install flex byacc libpcap ncurses ncurses-devel libpcap-devel

[root@centos ~]# wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz

[root@centos ~]# tar zxvf iftop-0.17.tar.gz && cd iftop-0.17

[root@centos ~]# ./configure && make && make install

 

通过lsof来查看网络端口的进程 

[root@centos ~]# lsof -i :48774

 

中间的<= =>这两个左右箭头,表示的是流量的方向。
TX:发送流量
RX:接收流量
TOTAL:总流量
Cumm:运行iftop到目前时间的总流量
peak:流量峰值
rates:分别表示过去 2s 10s 40s 的平均流量

常用的参数
-i设定监测的网卡,如:# iftop -i eth1
-B 以bytes为单位显示流量(默认是bits),如:# iftop -B
-n使host信息默认直接都显示IP,如:# iftop -n
-N使端口信息默认直接都显示端口号,如: # iftop -N
-F显示特定网段的进出流量,如# iftop -F 10.10.1.0/24或# iftop -F 10.10.1.0/255.255.255.0
-h(display this message),帮助,显示参数信息
-p使用这个参数后,中间的列表显示的本地主机信息,出现了本机以外的IP信息;
-b使流量图形条默认就显示;
-f这个暂时还不太会用,过滤计算包用的;
-P使host信息及端口信息默认就都显示;
-m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:# iftop -m 100M

操作命令(注意大小写)
按h切换是否显示帮助;
按n切换显示本机的IP或主机名;
按s切换是否显示本机的host信息;
按d切换是否显示远端目标主机的host信息;
按t切换显示格式为2行/1行/只显示发送流量/只显示接收流量;
按N切换显示端口号或端口服务名称;
按S切换是否显示本机的端口信息;
按D切换是否显示远端目标主机的端口信息;
按p切换是否显示端口信息;
按P切换暂停/继续显示;
按b切换是否显示平均流量图形条;
按B切换计算2秒或10秒或40秒内的平均流量;
按T切换是否显示每个连接的总流量;
按l打开屏幕过滤功能,输入要过滤的字符,比如ip,按回车后,屏幕就只显示这个IP相关的流量信息;
按L切换显示画面上边的刻度;刻度不同,流量图形条会有变化;
按j或按k可以向上或向下滚动屏幕显示的连接记录;
按1或2或3可以根据右侧显示的三列流量数据进行排序;
按<根据左边的本机名或IP排序;
按>根据远端目标主机的主机名或IP排序;
按o切换是否固定只显示当前的连接;
按f可以编辑过滤代码,这是翻译过来的说法,我还没用过这个!
按!可以使用shell命令,这个没用过!没搞明白啥命令在这好用呢!
按q退出监控。

 

 

4. 网速测试(git clone https://github.com/sivel/speedtest-cli.git)

---------------------------------------------------------------------------------------------------------------------------------------------

/usr/local/src

wget https://raw.github.com/sivel/speedtest-cli/master/speedtest.py  

chmod a+rx speedtest.py
chown root:root /usr/local/bin/speedtest

./speedtest

 

Retrieving speedtest.net configuration...
Testing from Tianxin Group Co., Limited (42.32.125.38)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by STC (Hong Kong) [0.00 km]: 7.242 ms
Testing download speed................................................................................
Download: 4.76 Mbit/s
Testing upload speed................................................................................................
Upload: 2.78 Mbit/s

 

5.测试最大连接数测试

---------------------------------------------------------------------------------------------------------------------------------------------

/usr/local/src

wget --no-check-certificate https://github.com/ideawu/c1000k/archive/master.zip
unzip master.zip
cd c1000k-master
make

./server 7000

./client 127.0.0.1 7000


connections: 153
error: Too many open files

 

6. 硬盘读写速度查看

---------------------------------------------------------------------------------------------------------------------------------------------

yum install hdparm -y

hdparm -t /dev/sda

参数:
-a<快取分区> 设定读取文件时,预先存入块区的分区数,若不加上<快取分区>选项,则显示目前的设定。
-A<0或1> 启动或关闭读取文件时的快取功能。
-c<I/O模式> 设定IDE32位I/O模式。
-C 检测IDE硬盘的电源管理模式。
-d<0或1> 设定磁盘的DMA模式。
-f 将内存缓冲区的数据写入硬盘,并清楚缓冲区。
-g 显示硬盘的磁轨,磁头,磁区等参数。
-h 显示帮助。
-i 显示硬盘的硬件规格信息,这些信息是在开机时由硬盘本身所提供。
-I 直接读取硬盘所提供的硬件规格信息。
-k<0或1> 重设硬盘时,保留-dmu参数的设定。
-K<0或1> 重设硬盘时,保留-APSWXZ参数的设定。
-m<磁区数> 设定硬盘多重分区存取的分区数。
-n<0或1> 忽略硬盘写入时所发生的错误。
-p<PIO模式> 设定硬盘的PIO模式。
-P<磁区数> 设定硬盘内部快取的分区数。
-q 在执行后续的参数时,不在屏幕上显示任何信息。
-r<0或1> 设定硬盘的读写模式。
-S<时间> 设定硬盘进入省电模式前的等待时间。
-t 评估硬盘的读取效率。
-T 评估硬盘快取的读取效率。
-u<0或1> 在硬盘存取时,允许其他中断要求同时执行。
-v 显示硬盘的相关设定。
-W<0或1> 设定硬盘的写入快取。
-X<传输模式> 设定硬盘的传输模式。
-y 使IDE硬盘进入省电模式。
-Y 使IDE硬盘进入睡眠模式。
-Z 关闭某些Seagate硬盘的自动省电功能。

 

 

7.Nginx查看并发连接

---------------------------------------------------------------------------------------------------------------------------------------------

配置文件
location /Nginxstatus {
stub_status on;
}

查看结果说明:http://localhsot/Nginxstatus

Active connections
//当前 Nginx 正处理的活动连接数.

server accepts handledrequests
//总共处理连接,成功创建握手,总共处理请求.

Reading //nginx 读取到客户端的 Header 信息数.
Writing //nginx 返回给客户端的 Header 信息数.
Waiting //nginx 已经处理完正在等候下一次请求指令的驻留连接 (开启 keep-alive,这个值等于active-reading-writing)

 

worker_cpu_affinity 提定CPU 
16核
0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000
24核
000000000000000000000001 000000000000000000000010 000000000000000000000100 000000000000000000001000 000000000000000000010000 000000000000000000100000 000000000000000001000000 000000000000000010000000 000000000000000100000000 000000000000001000000000 000000000000010000000000 000000000000100000000000 000000000001000000000000 000000000010000000000000 000000000100000000000000 000000001000000000000000 000000010000000000000000 000000100000000000000000 000001000000000000000000 000010000000000000000000 000100000000000000000000 001000000000000000000000 010000000000000000000000 100000000000000000000000;

 

8.查看那mysql当前连接:(如果用普通账号登录,就只显示这用户的)
---------------------------------------------------------------------------------------------------------------------------------------------

#mysql -uroot -p
mysql>
show processlist; 可以显示前100条连接信息
show full processlist; 可以显示全部。

查看这台服务器设置。
#vi /etc/my.cnf
set-variable=max_user_connections=30 这个就是单用户的连接数
set-variable=max_connections=800 这个是全局的限制连接数

 

9.查看Linux连接数

---------------------------------------------------------------------------------------------------------------------------------------------

#  yum install net-tools

# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'

LISTEN: 服务器在等待进入呼叫
SYN_RECV: 一个连接请求已经到达,等待确认
SYN_SENT: 应用已经开始,打开一个连接
ESTABLISHED: 正常数据传输状态
FIN_WAIT1: 应用说它已经完成
FIN_WAIT2: 另一边已同意释放
ITMED_WAIT: 等待所有分组死掉
CLOSING: 两边同时尝试关闭
TIME_WAIT: 另一边已初始化一个释放
LAST_ACK: 等待所有分组死掉

 

常用的三个状态是:ESTABLISHED 表示正在通信,TIME_WAIT 表示主动关闭,CLOSE_WAIT 表示被动关闭。

TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死状态,连接本身占用的资源不会被释放。网络服务器程序要同时管理大量连接,所以很有必要保证无用连接完全断开,否则大量僵死的连接会浪费许多服务器资源。在众多TCP状态中,最值得注意的状态有两个:CLOSE_WAIT和TIME_WAIT。

TIME_WAIT
TIME_WAIT 是主动关闭链接时形成的,等待2MSL时间,约4分钟。主要是防止最后一个ACK丢失。 由于TIME_WAIT 的时间会非常长,因此server端应尽量减少主动关闭连接

CLOSE_WAIT
CLOSE_WAIT是被动关闭连接是形成的。根据TCP状态机,服务器端收到客户端发送的FIN,则按照TCP实现发送ACK,因此进入CLOSE_WAIT状态。但如果服务器端不执行close(),就不能由CLOSE_WAIT迁移到LAST_ACK,则系统中会存在很多CLOSE_WAIT状态的连接。此时,可能是系统忙于处理读、写操作,而未将已收到FIN的连接,进行close。此时,recv/read已收到FIN的连接socket,会返回0。

为什么需要 TIME_WAIT 状态?

假设最终的ACK丢失,server将重发FIN,client必须维护TCP状态信息以便可以重发最终的ACK,否则会发送RST,结果server认为发生错误。TCP实现必须可靠地终止连接的两个方向(全双工关闭),client必须进入 TIME_WAIT 状态,因为client可能面 临重发最终ACK的情形。

为什么 TIME_WAIT 状态需要保持 2MSL 这么长的时间?

如果 TIME_WAIT 状态保持时间不足够长(比如小于2MSL),第一个连接就正常终止了。第二个拥有相同相关五元组的连接出现,而第一个连接的重复报文到达,干扰了第二个连接。TCP实现必须防止某个连接的重复报文在连接终止后出现,所以让TIME_WAIT状态保持时间足够长(2MSL),连接相应方向上的TCP报文要么完全响应完毕,要么被 丢弃。建立第二个连接的时候,不会混淆。

TIME_WAIT 和CLOSE_WAIT状态socket过多

如果服务器出了异常,百分之八九十都是下面两种情况:

1.服务器保持了大量TIME_WAIT状态

2.服务器保持了大量CLOSE_WAIT状态,简单来说CLOSE_WAIT数目过大是由于被动关闭连接处理不当导致的。

 

因为linux分配给一个用户的文件句柄是有限的,而TIME_WAIT和CLOSE_WAIT两种状态如果一直被保持,那么意味着对应数目的通道就一直被占着,而且是“占着茅坑不使劲”,一旦达到句柄数上限,新的请求就无法被处理了,接着就是大量Too Many Open Files异常,Tomcat崩溃。

 

 

10.网站高并发测试

---------------------------------------------------------------------------------------------------------------------------------------------

硬件:虚拟机 64U+64G

[root@test ~]# uname -a
Linux test 5.7.8 #1 SMP Tue Jul 21 16:41:54 CST 2020 x86_64 x86_64 x86_64 GNU/Linux

[root@test ~]# free -h
total used free shared buff/cache available
Mem: 62G 30G 31G 8.8M 263M 31G
Swap: 19G 0B 19G

[root@test ~]# grep 'processor' /proc/cpuinfo | sort -u | wc -l
64

[root@test ~]# vim /etc/security/limits.conf
* soft nofile 1025500
* hard nofile 1025500
* soft nproc 1025500
* hard nproc 1025500

[root@test ~]# ulimit -n
1025500

[root@test ~]# vim /etc/sysctl.conf

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

fs.file-max=1025500
fs.nr_open = 1025500
net.ipv4.ip_local_port_range = 1024 65535

net.core.somaxconn = 20480
net.core.netdev_max_backlog = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.ipv4.tcp_max_tw_buckets = 800000

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_tw_buckets = 6000

[root@test ~]# sysctl -p

 

[root@test ~]# cat /opt/nginx/conf/nginx.conf

user www www;
worker_processes 64;
pid logs/nginx.pid;
worker_rlimit_nofile 1025500;
error_log /data/www/error.log info;

events {
use epoll;
multi_accept on;
worker_connections 1025500;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip off;
access_log off;

server {
listen 8080 backlog=20480;
server_name 192.168.0.135;
root /data/www/web;

location / {
index index.html index.htm;
}
}

include /opt/nginx/conf/vhosts/*.conf;

}

[root@test ~]# cat /opt/nginx/conf/vhosts/webapi.conf

upstream api_server {
server 127.0.0.1:5000;
keepalive 5000;
}

server {
listen 80 backlog=20480;
server_name 192.168.0.135;

location / {
proxy_pass http://api_server;
proxy_set_header Connection "keep-alive";
proxy_http_version 1.1;

#proxy_ignore_client_abort on;
#proxy_connect_timeout 600;
#proxy_read_timeout 600;
#proxy_send_timeout 600;

}
}


[root@test ~]# vim /etc/systemd/system/nginx.service
PrivateTmp=true
LimitNOFILE=1025500

[root@bogon ~]# vim /etc/systemd/system.conf
DefaultLimitNOFILE=1025500
DefaultLimitNPROC=1025500


-------------------------------------------------------------------------------------------------------

[root@test www]# wrk -t128 -c50000 -d10 "http://127.0.0.1:8080"
Running 10s test @ http://127.0.0.1:8080
128 threads and 50000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 66.33ms 94.09ms 1.52s 90.49%
Req/Sec 6.79k 6.34k 79.32k 88.99%
9139684 requests in 11.53s, 2.02GB read
Requests/sec: 792648.96
Transfer/sec: 179.13MB

[root@test www]# wrk -t64 -c10000 -d10 "http://127.0.0.1:5000/api/values"
Running 10s test @ http://127.0.0.1:5000/api/values
64 threads and 10000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 24.53ms 17.14ms 266.90ms 89.48%
Req/Sec 6.72k 2.18k 61.62k 88.33%
4390789 requests in 10.10s, 745.35MB read
Requests/sec: 434705.59
Transfer/sec: 73.79MB


[root@test www]# wrk -t64 -c2000 -d10 "http://127.0.0.1/api/values"
Running 10s test @ http://127.0.0.1/api/values
64 threads and 2000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 6.65ms 6.01ms 115.26ms 92.35%
Req/Sec 5.21k 1.25k 30.37k 69.26%
3324771 requests in 10.10s, 656.19MB read
Requests/sec: 329173.19
Transfer/sec: 64.97MB

 

---------------------------------------------------------------------------------------------------------------------------------------------
硬件:虚拟机 8U+8G

[root@website ~]# wrk -t8 -c100 -d10 "http://192.168.0.135:8080"
Running 10s test @ http://192.168.0.135:8080
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.18ms 20.14ms 502.22ms 99.54%
Req/Sec 19.84k 7.55k 38.82k 63.00%
1581635 requests in 10.06s, 358.92MB read
Requests/sec: 157211.22
Transfer/sec: 35.68MB

[root@website ~]# wrk -t8 -c100 -d10 "http://192.168.0.135:5000/api/values"
Running 10s test @ http://192.168.0.135:5000/api/values
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.81ms 5.77ms 148.63ms 99.24%
Req/Sec 8.19k 1.29k 12.02k 77.75%
653072 requests in 10.03s, 110.86MB read
Requests/sec: 65083.94
Transfer/sec: 11.05MB

[root@website ~]# wrk -t8 -c100 -d10 "http://192.168.0.135/api/values"
Running 10s test @ http://192.168.0.135/api/values
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.37ms 1.24ms 29.58ms 81.58%
Req/Sec 5.13k 512.40 6.53k 71.25%
409021 requests in 10.04s, 80.73MB read
Requests/sec: 40755.37
Transfer/sec: 8.04MB

 

posted @ 2017-07-25 16:22  vicowong  阅读(2900)  评论(0编辑  收藏  举报