第 7 周作业
发布于: 2020 年 12 月 06 日
一、作业说明
作业一(至少完成一项):
性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
二、作业实现
性能压测时,随着并发数增加,理想情况下:
响应时间:
第1阶段:响应时间随着并发数增加呈现稳定响应时长
第2阶段:响应时间随着并发数增加响应时长变大,设置服务崩溃
吞吐量:
第1阶段:吞吐量随着并发数增加吞吐量呈现线性增长
第2阶段:吞吐量随着并发数增加吞吐量呈现增长,但升率减小
第3阶段:吞吐量随着并发数增加吞吐量呈现下降,下降甚至服务崩溃
总结:出现如上所述情况,因宿主机资源有限,第1阶段随着并发数增加,线程所需资源宿主机基本满足,响应时长变化不大,吞吐量上升明显。随着并发数继续增加,进入第2阶段,线程频繁在用户态、内核态切换,IO阻塞,内存空间等限制,响应时长、吞吐量变化明显,甚至服务崩溃。生产环境随着业务当面临用户数或者请求数增加一般通过横向、纵向扩容满足业务需求。
2.性能压测工具
package online.chenkai.demon;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ExecutorService;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadFactory;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;import org.apache.commons.collections.CollectionUtils;import com.google.common.util.concurrent.ThreadFactoryBuilder;import com.ifugle.rap.dsb.xxzx.xxts.model.bo.rate.MessageWrapperBO;import lombok.extern.slf4j.Slf4j;import okhttp3.Call;import okhttp3.ConnectionPool;import okhttp3.OkHttpClient;import okhttp3.Request;/** * 性能压测 * * @author chenkai 2020-12-06 15:21:00 * @version 1.0.0 */@Slf4jpublic class PressureTest { /** * 网络连接 */ private static OkHttpClient HTTP_POOL; /** * 线程池 */ private static ForkJoinPool PRESSURE_THREAD_POOL; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请求地址: "); String url = input.nextLine(); System.out.println("压测次数: "); String requestCount = input.nextLine(); System.out.println("并发数: "); String concurrentCount = input.nextLine(); int corePoolSize = Integer.parseInt(concurrentCount); // 流量压测线程池 PRESSURE_THREAD_POOL = new ForkJoinPool(corePoolSize); // 连接池 ConnectionPool connectionPool = new ConnectionPool(corePoolSize, 5, TimeUnit.MINUTES); HTTP_POOL = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(120, TimeUnit.SECONDS) .readTimeout(120, TimeUnit.SECONDS) .connectionPool(connectionPool) .retryOnConnectionFailure(true) .build(); // 流量构建 int counter = Integer.parseInt(requestCount); List<Integer> requestNoList = new ArrayList<>(counter); for (int i = 1; i <= counter; i++) { requestNoList.add(i); } List<CompletableFuture<Long>> collectFuture = requestNoList.stream() .map(requestNo -> CompletableFuture.supplyAsync(() -> execute(url, requestNo), PRESSURE_THREAD_POOL)) .collect(Collectors.toList()); List<Long> returnTimeList = collectFuture.stream().map(CompletableFuture::join).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(returnTimeList)) { // 平均时长计算 long averageReturnTime = returnTimeList.stream().mapToLong(value -> value).sum() / CollectionUtils.size(returnTimeList); log.debug("平均响应时长:{}毫秒", averageReturnTime); } // 关闭线程池 PRESSURE_THREAD_POOL.shutdown(); } /** * 目标地址请求 * * @param url 请求地址 * @param traceId 请求编号 * * @return long 耗时 */ private static long execute(String url, int traceId) { long startTime = System.currentTimeMillis(); final Request request = new Request.Builder().url(url).build(); final Call call = HTTP_POOL.newCall(request); try { call.execute(); } catch (IOException e) { } long endTime = System.currentTimeMillis(); long executeTime = endTime - startTime; log.debug("请求地址:{},请求编号:{},响应时长:{}毫秒", url, traceId, executeTime); return executeTime; }}执行结果:请求地址: https://www.baidu.com压测次数: 100并发数: 102020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:9,响应时长:260毫秒2020-12-06 15:27:43,672 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:2,响应时长:262毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:7,响应时长:261毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:5,响应时长:262毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:1,响应时长:262毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:8,响应时长:261毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:6,响应时长:262毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:3,响应时长:262毫秒2020-12-06 15:27:43,671 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:10,响应时长:260毫秒2020-12-06 15:27:43,725 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:17,响应时长:51毫秒2020-12-06 15:27:43,726 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:12,响应时长:53毫秒2020-12-06 15:27:43,727 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:19,响应时长:53毫秒2020-12-06 15:27:43,729 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:13,响应时长:56毫秒2020-12-06 15:27:43,730 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:15,响应时长:57毫秒2020-12-06 15:27:43,732 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:11,响应时长:58毫秒2020-12-06 15:27:43,732 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:18,响应时长:58毫秒2020-12-06 15:27:43,732 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:14,响应时长:59毫秒2020-12-06 15:27:43,737 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:16,响应时长:64毫秒2020-12-06 15:27:43,770 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:27,响应时长:38毫秒2020-12-06 15:27:43,773 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:28,响应时长:36毫秒2020-12-06 15:27:43,779 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:20,响应时长:54毫秒2020-12-06 15:27:43,785 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:21,响应时长:59毫秒2020-12-06 15:27:43,786 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:25,响应时长:54毫秒2020-12-06 15:27:43,786 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:23,响应时长:57毫秒2020-12-06 15:27:43,791 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:26,响应时长:59毫秒2020-12-06 15:27:43,792 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:24,响应时长:61毫秒2020-12-06 15:27:43,796 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:22,响应时长:69毫秒2020-12-06 15:27:43,812 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:29,响应时长:41毫秒2020-12-06 15:27:43,831 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:34,响应时长:45毫秒2020-12-06 15:27:43,833 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:30,响应时长:60毫秒2020-12-06 15:27:43,839 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:31,响应时长:60毫秒2020-12-06 15:27:43,841 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:37,响应时长:45毫秒2020-12-06 15:27:43,844 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:35,响应时长:53毫秒2020-12-06 15:27:43,846 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:33,响应时长:60毫秒2020-12-06 15:27:43,850 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:36,响应时长:58毫秒2020-12-06 15:27:43,854 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:32,响应时长:69毫秒2020-12-06 15:27:43,867 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:4,响应时长:459毫秒2020-12-06 15:27:43,871 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:38,响应时长:59毫秒2020-12-06 15:27:43,887 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:39,响应时长:56毫秒2020-12-06 15:27:43,888 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:40,响应时长:55毫秒2020-12-06 15:27:43,893 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:41,响应时长:54毫秒2020-12-06 15:27:43,901 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:42,响应时长:60毫秒2020-12-06 15:27:43,903 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:43,响应时长:58毫秒2020-12-06 15:27:43,905 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:44,响应时长:59毫秒2020-12-06 15:27:43,907 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:45,响应时长:57毫秒2020-12-06 15:27:43,908 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:46,响应时长:54毫秒2020-12-06 15:27:43,925 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:47,响应时长:58毫秒2020-12-06 15:27:43,937 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:48,响应时长:66毫秒2020-12-06 15:27:43,944 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:49,响应时长:57毫秒2020-12-06 15:27:43,948 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:50,响应时长:60毫秒2020-12-06 15:27:43,953 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:51,响应时长:60毫秒2020-12-06 15:27:43,957 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:55,响应时长:49毫秒2020-12-06 15:27:43,960 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:52,响应时长:59毫秒2020-12-06 15:27:43,964 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:53,响应时长:61毫秒2020-12-06 15:27:43,970 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:56,响应时长:62毫秒2020-12-06 15:27:43,977 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:57,响应时长:51毫秒2020-12-06 15:27:43,999 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:60,响应时长:51毫秒2020-12-06 15:27:44,000 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:59,响应时长:56毫秒2020-12-06 15:27:44,004 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:58,响应时长:65毫秒2020-12-06 15:27:44,007 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:61,响应时长:53毫秒2020-12-06 15:27:44,019 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:63,响应时长:59毫秒2020-12-06 15:27:44,019 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:64,响应时长:55毫秒2020-12-06 15:27:44,023 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:54,响应时长:118毫秒2020-12-06 15:27:44,025 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:62,响应时长:68毫秒2020-12-06 15:27:44,031 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:66,响应时长:54毫秒2020-12-06 15:27:44,042 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:65,响应时长:71毫秒2020-12-06 15:27:44,056 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:67,响应时长:57毫秒2020-12-06 15:27:44,057 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:68,响应时长:57毫秒2020-12-06 15:27:44,066 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:70,响应时长:58毫秒2020-12-06 15:27:44,070 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:69,响应时长:66毫秒2020-12-06 15:27:44,079 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:72,响应时长:59毫秒2020-12-06 15:27:44,082 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:73,响应时长:59毫秒2020-12-06 15:27:44,083 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:71,响应时长:64毫秒2020-12-06 15:27:44,084 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:74,响应时长:59毫秒2020-12-06 15:27:44,092 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:75,响应时长:61毫秒2020-12-06 15:27:44,098 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:76,响应时长:56毫秒2020-12-06 15:27:44,106 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:77,响应时长:50毫秒2020-12-06 15:27:44,117 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:78,响应时长:60毫秒2020-12-06 15:27:44,126 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:79,响应时长:60毫秒2020-12-06 15:27:44,132 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:81,响应时长:53毫秒2020-12-06 15:27:44,136 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:80,响应时长:66毫秒2020-12-06 15:27:44,142 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:82,响应时长:60毫秒2020-12-06 15:27:44,148 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:86,响应时长:50毫秒2020-12-06 15:27:44,151 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:84,响应时长:67毫秒2020-12-06 15:27:44,151 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:83,响应时长:68毫秒2020-12-06 15:27:44,153 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:85,响应时长:60毫秒2020-12-06 15:27:44,169 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:87,响应时长:63毫秒2020-12-06 15:27:44,170 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:88,响应时长:53毫秒2020-12-06 15:27:44,175 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:89,响应时长:49毫秒2020-12-06 15:27:44,183 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:91,响应时长:47毫秒2020-12-06 15:27:44,195 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:90,响应时长:63毫秒2020-12-06 15:27:44,198 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:92,响应时长:56毫秒2020-12-06 15:27:44,204 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:96,响应时长:50毫秒2020-12-06 15:27:44,209 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:93,响应时长:61毫秒2020-12-06 15:27:44,212 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:95,响应时长:61毫秒2020-12-06 15:27:44,214 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:94,响应时长:63毫秒2020-12-06 15:27:44,217 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:99,响应时长:42毫秒2020-12-06 15:27:44,232 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:97,响应时长:62毫秒2020-12-06 15:27:44,233 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:98,响应时长:62毫秒2020-12-06 15:27:44,234 <DEBUG> online.chenkai.demon.PressureTest - 请求地址:https://www.baidu.com,请求编号:100,响应时长:51毫秒2020-12-06 15:27:44,242 <DEBUG> online.chenkai.demon.PressureTest - 平均响应时长:80毫秒
划线
评论
复制
发布于: 2020 年 12 月 06 日阅读数: 48
Rocky·Chen
关注
还未添加个人签名 2018.03.03 加入
还未添加个人简介
评论