1使用POSTMAN发起请求
1.1自定义设置变量
1.1.1. 定义一个host,省去我们每次访问时写localhost:8080
1.1.2. 发起请求
直接两个大括号发起请求
1.2.如何进行并发测试
1.2.1创建一个Collections
1.2.2保存请求到我们的Collections中
1.2.3到我们的文件夹中选择run
1.2.4发起1000次请求
postman不算特别好的压测工具,接下来我们介绍一个比较犀利的工具,其实在我之前的Nginx文章中,它出场过一次,那就是Apache bench
2.Apache Bench
安装这里就不多说了,小二上命令:
ab -n 1000 -c 20 http://localhost:8080
表示发起1000次请求,并发数20.
格式: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 //测试所进行的最大秒数,默认没有时间限制。 -r Don't exit on socket receive errors. // 抛出异常继续执行测试任务 -p postfile File containing data to POST //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt -T content-type Content-type header for POSTing //POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p) -v verbosity How much troubleshooting info to print //设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。 -C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3” (repeatable) //-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。 提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。 -w Print out results in HTML tables //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。 -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 -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//使用keepAlive -d Do not show percentiles served table. -S Do not show confidence estimators and warnings. -g filename Output collected data to gnuplot format file. -e filename Output CSV file with percentages served -h Display usage information (this message) |
这里的结果是摘自Jimin老师的结果:
Concurrency Level表示并发数量。
time taken for tests表示总共1000次请求所耗费的时间
Coplete requests:表示请求数量
Failed requests:表示失败的请求数量
total transferred:表示响应的数据量,不包含请求的数据量
HTML transferred:表示total transferred-头信息中的数据量的总和,只包含body里面的数据。
Request per second:表示吞吐率!非常重要的数据。如果并发数量不一样,即使请求数一样,这个结果也不一样哦~
Time per request:表示用户请求平均等待时间
Time per request:名字和上面长得很像,但是表示的是服务器平均请求等待时间
transfer rate:表示这些请求在单位时间内从服务器获取的数据长度。total transferred/time taken for tests
3.JMeter
3.1下载:http://jmeter.apache.org/download_jmeter.cgi
3.2环境变量配置:
export JMETER=/root/apache-jmeter-5.1.1 export CLASSPATH=$JMETER/lib/ext/ApacheJMeter_core.jar:$JMETER/lib/jorphan.jar:$JMETER/lib/logkit-2.0.jar:$CLASSPATH export PATH=$JMETER/bin/:$PATH |
3.3运行:
3.3.1添加线程组
3.3.2参数设置
Number of threads表示并发数量,Loop Count表示循环多少次,Ramp-Up相对不好理解,它表示这么一个意思,现实生活中我们的用户不可能在指定时间达到一定的并发数,就像去食堂吃饭,30分钟后可能食堂才会达到1000人,比如11:30开饭,12:00是用餐高峰,我们这里就可以设置为30min*60s=1800.
3.3.3添加请求
4.使用代码来控制并发(非线程安全)
@Slf4j @NotThreadSafe public class ConcurrencyTest { // 请求总数 public static int clientTotal = 5000; // 同时并发执行的线程数 public static int threadTotal = 200; public static int count = 0; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { executorService.execute(() -> { try { semaphore.acquire(); add(); semaphore.release(); } catch (Exception e) { log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("count:{}", count); } private static void add() { count++; } } |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理