1. 安装压测工具包,httpd-tools
yum install httpd-tools
2. post 请求将参数放在文件里面,(可根据自己项目或者公司里面的接口请求,fiddler抓包获取对应的demo数据和地址)
159_root -->cat GetRsOrderInfo.txt
{"method":"GetRsOrderInfo","orderId":2785629,"version":"v0.1.0","localHSFVersion":"8.2.574","localCKVersion":"9.1.498","nativeVersion":"ljck-ios-5.2.4","token":"5df867578f5740ba98ccb57ef12f1b30"}
3. 开始压测
-n:执行的请求个数,默认时执行一个请求
-c:一次产生的请求个数,即并发个数
-p: 模拟 POST 请求,文件格式为 param1=1¶m2=2,配合 -T 使用
-T:POST 数据所使用的 Content-Type 头信息,如 -T application/x-www-form-urlencoded
159_root -->ab -c 3 -n 100 -p GetRsOrderInfo.txt -T application/x-www-form-urlencoded http://txapi.xxxxx.com/api/GetRsOrderInfo.json
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking txapi.xxxxx.com (be patient).....done
Server Software: nginx/1.10.2
Server Hostname: txapi.xxxxx.com (域名)
Server Port: 80
Document Path: /api/GetRsOrderInfo.json (地址)
Document Length: 15376 bytes
Concurrency Level: 3 (并发数)
Time taken for tests: 4.044 seconds (总请求时间)
Complete requests: 100 (完成请求数)
Failed requests: 0 (失败请求数)
Total transferred: 1569300 bytes
Total body sent: 37300
HTML transferred: 1537600 bytes
Requests per second: 24.73 [#/sec] (mean) (平均每秒请求数)
Time per request: 121.321 [ms] (mean) (平均每个请求消耗时间)
Time per request: 40.440 [ms] (mean, across all concurrent requests)
Transfer rate: 378.96 [Kbytes/sec] received
9.01 kb/s sent
387.96 kb/s total (传输速率)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 3 4 0.6 4 8
Processing: 70 114 85.2 78 501
Waiting: 69 91 50.9 76 291
Total: 74 118 85.2 82 504
Percentage of the requests served within a certain time (ms)
50% 82 (50% 的请求都在 936ms 内完成)
66% 85
75% 92
80% 110
90% 296
95% 303
98% 317
99% 504
100% 504 (longest request)
4. 逐渐加压,查看系统各项指标参数
4.1 查看服务器的cpu、内存、网络;
top -- cpu、内存、进程等
free -- 内存
iostat --输入、输出统计
lejia_ceshi_13--> free
total used free shared buff/cache available
Mem: 16257492 11803272 710928 5084 3743292 3939836
Swap: 0 0 0
lejia_ceshi_13--> iostat 2 10 (每2s输出一次,输出10次)
Linux 4.18.0-147.8.1.el8_1.x86_64 (master-node) 04/06/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.13 0.00 1.33 0.25 0.00 96.28
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 35.75 7.49 169.69 199108317 4512901812
vdb 2.08 0.96 28.41 25542501 755646956
4.2 查看数据库的内存、cpu、死锁、连接数、数据库延迟等各项指标(mysql 为例)
详见: mysql常用性能指标
show processlist;
SELECT * FROM information_schema.INNODB_TRX\G
SELECT * FROM information_schema.INNODB_LOCK_WAITS;
SELECT * FROM information_schema.INNODB_LOCKS;
information_schema>select * from PROCESSLIST limit 5;
+----------+----------+--------------------+--------------+---------+------+-------+------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----------+----------+--------------------+--------------+---------+------+-------+------+
| 15680598 | lejia008 | 192.168.0.13:46936 | lejia_helper | Sleep | 1128 | | NULL |
| 15686394 | lejia008 | 192.168.0.13:4145 | lejia_helper | Sleep | 530 | | NULL |
| 15682995 | lejia008 | 192.168.0.13:50686 | lejia_helper | Sleep | 756 | | NULL |
| 15160594 | middle | 192.168.0.13:43068 | lejia_middle | Sleep | 0 | | NULL |
| 15687692 | lejia008 | 192.168.0.13:54966 | lejia_helper | Sleep | 330 | | NULL |
+----------+----------+--------------------+--------------+---------+------+-------+------+
5 rows in set (0.00 sec)
information_schema>select * from PROCESSLIST where user='';
4.3 查看redis等系统性能
info
server:服务器运行的环境参数
clients:客户端相关信息
memory:服务器运行内存统计数据
persistence:持久化信息
stats:通用统计数据
Replication:主从复制相关信息
CPU:CPU使用情况
cluster:集群信息
Keypass:键值对统计数量信息
4.4 查看具体项目进程运行情况:
项目进程内存、cpu等消耗
项目具体的日志,有无报错,每个请求处理时间,哪块逻辑消耗的时间长短等
04-06 13:42:56 458 - W00002428,/api/GetRsOrderInfo.json,0,true,5df867578f5740ba98ccb57ef12f1b30,55
04-06 13:42:56 486 - W00002429,/api/GetRsOrderInfo.json,0,true,5df867578f5740ba98ccb57ef12f1b30,62
04-06 13:42:56 530 - W00002430,/api/GetRsOrderInfo.json,0,true,5df867578f5740ba98ccb57ef12f1b30,60
附:mysql常用性能指标
(1) QPS(每秒Query量)
QPS = Questions(or Queries) / uptime
mysql> show global status like 'Question%';
mysql> show global status like 'uptime';
(2) TPS(每秒事务量)
TPS = (Com_commit + Com_rollback) / uptime
mysql > show global status like 'Com_commit';
mysql > show global status like 'Com_rollback';
mysql> show global status like 'uptime';
(3)key Buffer 命中率
mysql>show global status like 'key%';
key_buffer_read_hits = (1-key_reads /key_read_requests) * 100%
key_buffer_write_hits = (1-key_writes /key_write_requests) * 100%
(4)InnoDB Buffer命中率
mysql> show status like 'innodb_buffer_pool_read%';
innodb_buffer_read_hits = (1 -innodb_buffer_pool_reads / innodb_buffer_pool_read_requests) * 100%
(5)Query Cache命中率
mysql> show status like 'Qcache%';
Query_cache_hits = (Qcahce_hits /(Qcache_hits + Qcache_inserts )) * 100%;
(6)Table Cache状态量
mysql> show global status like 'open%';
比较 open_tables 与opend_tables值
(7)Thread Cache 命中率
mysql> show global status like'Thread%';
mysql> show global status like'Connections';
Thread_cache_hits = (1 - Threads_created /connections ) * 100%
(8)锁定状态
mysql> show global status like '%lock%';
Table_locks_waited/Table_locks_immediate=0.3% 如果这个比值比较大的话,说明表锁造成的阻塞比较严重
Innodb_row_lock_waits innodb行锁,太大可能是间隙锁造成的
(9)复制延时量
mysql > show slave status
查看Seconds_Behind_Master的值,如果为0,说明没有延迟
(10) Tmp Table 状况(临时表状况)
mysql > show global status like 'Created_tmp%';
Created_tmp_disk_tables/Created_tmp_tables比值最好不要超过10%,如果Created_tmp_tables值比较大,
可能是排序句子过多或者是连接句子不够优化
(11) Binlog Cache 使用状况
mysql > show global status like 'Binlog_cache%';
如果Binlog_cache_disk_use值不为0 ,可能需要调大 binlog_cache_size大小
(12) Innodb_log_waits 量
mysql > show status like'innodb_log_waits';
Innodb_log_waits值不等于0的话,表明 innodblog buffer 因为空间不足而等待
(13)open file and table
mysql> show global status like 'Open_files';
mysql> show global status like 'Open_tables';
(14) 慢查询
开启慢查询:
1).手动执行命令开启:
mysql> set global slow_query_log=on;
mysql> set global long_query_time=1;
2).编辑/etc/my.cnf,在[mysqld]域中添加:
slow_query_log= 1 # 开启慢查询
slow_query_log_file=/data/data/localhost-slow.log # 慢查询日志路径
long_query_time= 1 # 慢查询的时长
(15)全日志
查看全日志:
show global variables like 'general_log';
开启全日志:
set global general_log=on;
注意开启全日志会消耗服务器性能,一般只有在排查问题时才会短暂打开。