tomcat 性能测试
**************
性能测试
性能测试指标:响应时间、吞吐量
响应时间:用户执行某个操作的耗时
吞吐量(TPS):每秒执行执行的事务数量
常见测试方式
# 负载测试:正常流量测试
一开始流量很低,随后逐步增加流量,直至应用达到一个较高的负载;
通过增加系统流量,观察随着用户量的增多,响应时间的变化情况
# 压力测试:极端流量测试
使用远超正常数值的流量进行测试,逐步增加流量,直至系统奔溃,不可访问;
通过压力测试,获知系统载在极端情况下可能出现的问题,如:系统奔溃导致的数据不一致等
# 持续运行时间测试
应用系统持续数天不间断运行,模拟正常的用户访问;
在此期间,监测系统响应时间、内存、cpu、磁盘等运行状况;
持续运行时间测试可发现负载测试不易发现的问题,如内存泄漏等
**************
性能测试工具
***********
ab 测试
命令格式:ab [options] url
[root@centos test]# ab -h
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform #每轮测试总共发送的请求数
-c concurrency Number of multiple requests to make at a time #每轮测试同时发送的请求数
-t timelimit Seconds to max. to spend on benchmarking #测试持续时间,在此期间可进行多轮测试
This implies -n 50000
-s timeout Seconds to max. wait for each response #请求响应的超时时间
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of 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)
-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.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key
示例:ab -n 100 http://localhost:8080/hello
[root@centos test]# ab -n 100 http://localhost:8080/hello
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /hello
Document Length: 5 bytes
Concurrency Level: 1 //并发度1
Time taken for tests: 0.042 seconds //测试执行时间
Complete requests: 100 //总共完成的请求数
Failed requests: 0 //失败请求数
Total transferred: 13700 bytes
HTML transferred: 500 bytes
Requests per second: 2378.12 [#/sec] (mean)
Time per request: 0.420 [ms] (mean)
Time per request: 0.420 [ms] (mean, across all concurrent requests)
Transfer rate: 318.17 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 0.1 0 1
Waiting: 0 0 0.1 0 1
Total: 0 0 0.1 0 1
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 1
95% 1
98% 1
99% 1
100% 1 (longest request)
示例 2: ab -n 100 -c 2 http://localhost:8080/hello
[root@centos test]# ab -n 100 -c 2 http://localhost:8080/hello
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /hello
Document Length: 5 bytes
Concurrency Level: 2 //请求并行度2
Time taken for tests: 0.031 seconds //测试持续时间0.031s
Complete requests: 100 //总共完成的请求数
Failed requests: 0 //失败的请求数
Total transferred: 13700 bytes
HTML transferred: 500 bytes
Requests per second: 3260.41 [#/sec] (mean)
Time per request: 0.613 [ms] (mean)
Time per request: 0.307 [ms] (mean, across all concurrent requests)
Transfer rate: 436.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 0.1 0 1
Waiting: 0 0 0.1 0 1
Total: 0 1 0.2 1 1
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 1
100% 1 (longest request)
示例 3:ab -n 100 -c 2 -t 2 http://localhost:8080/hello
[root@centos test]# ab -n 100 -c 2 -t 2 http://localhost:8080/hello
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 5000 requests
Finished 9086 requests
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /hello
Document Length: 5 bytes
Concurrency Level: 2 //请求并行度2
Time taken for tests: 2.000 seconds //测试持续时间
Complete requests: 9086 //总共完成的请求
Failed requests: 0 //失败的请求数
Total transferred: 1244919 bytes
HTML transferred: 45435 bytes
Requests per second: 4542.62 [#/sec] (mean)
Time per request: 0.440 [ms] (mean)
Time per request: 0.220 [ms] (mean, across all concurrent requests)
Transfer rate: 607.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 3.5 0 234
Waiting: 0 0 3.5 0 234
Total: 0 0 3.5 0 234
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 1
98% 1
99% 1
100% 234 (longest request)
***********
jmeter 测试
**************
系统性能分析
如果测试后,响应时间或者吞吐量不及预期,需要进行性能优化;
需先从服务器、应用程序分析导致性能较差的原因
服务器性能分析
# cpu:cpu繁忙等
查看当前cpu负载:uptime
实时查看cpu、内存状态:top
# 内存:可用内存不足等
查看剩余内存:free -h
# 磁盘:磁盘空间不足、磁盘读写繁忙等
查看剩余磁盘空间:df -h
磁盘读写状态:iostat -h
# 网络:网络连接数过多、带宽不足等
网络带宽状况:nload
网络连接状况:netstat -a
tcp连接数:netstat -a|awk '/^tcp/ {++S[$NF]} END {for(a in S) print a " " S[a] }'
# 整体性能查看(cpu、内存、磁盘、swap、进程数等):vmstat
命令格式:vmstat [options] [delay [count]]
应用程序分析:java进程资源占用状况
jps:当前java进程号
jstat:java进程运行状态,包括:内存状况、垃圾回收信息等
jmap:堆内存信息,包括:内存占用、对象数量等
jstack:java进程中线程运行状态
可使用一些可视化工具分析java进程,如visualVM、jconsole等;
# 如果程序涉及到数据库操作,还需分析数据库性能
sql执行时间过长:explain sql优化、mysql慢查询日志
如果数据量过大,可考虑分库分表、非业务数据使用nosql数据库存储等