wee7 作业
发布于: 2020 年 07 月 23 日
一、
阶段1,在系统各项资源充足的情况下,用户请求发送到服务器,性能计数器系统负载小于等于cpu核心数,表示当前所有请求都分配到cpu时间且没有请求排队等待处理。在系统负载没有超过核心数,请求并发数增加,请求响应时间基本不变。吞吐量随并发数线性增长。
阶段2,随着并发数增加,系统资源消耗变大,cpu处理不过来,会出现请求排队待处理现象,响应时间会逐渐变大,吞吐量随并发数增加,不过增长率逐渐平缓。当系统资源会大量消耗,cpu、内存、磁盘和网络逐渐达到临界值,吞吐量也达到最大值。
阶段3,并发数继续增加,系统部分类型耗尽,更多的请求根本来不及处理,响应等待时间会逐渐变长,此时的吞吐量随着并发数增加而降低。并发数进一步增加,整个系统崩溃,响应时间无限长,吞吐量为零。
二、
import com.alibaba.fastjson.JSONObject;import lombok.extern.slf4j.Slf4j;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;import java.time.Duration;import java.time.LocalDateTime;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.concurrent.CountDownLatch;@Slf4jpublic class PerformanceTest { private static SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory(); public static void PressureTest(final String url, final int threadNumber, final int total) { int times = total / threadNumber; List<Long> spendTimeList = new ArrayList<>(); for (int id = 0; id < times; ++id) { CountDownLatch countDownLatch = new CountDownLatch(threadNumber); for (int threadIndex = 0; threadIndex < threadNumber; ++threadIndex) { new Thread(new Runnable() { @Override public void run() { LocalDateTime begin = LocalDateTime.now(); log.debug("请求响应statusCode={}", new RestTemplate(httpRequestFactory).getForEntity(url, String.class).getStatusCodeValue()); long duration = Duration.between(begin, LocalDateTime.now()).toMillis(); synchronized (spendTimeList) { spendTimeList.add(duration); } log.debug("请求响应时间[{}](毫秒)", duration); countDownLatch.countDown(); } }).start(); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } Collections.sort(spendTimeList, Long::compareTo); int requestCount = total; long index = Math.round(requestCount * 0.95); log.debug("95%响应时间[{}](毫秒)", spendTimeList.get((int)index)); } public static void main(String[] args) { PressureTest("https://www.baidu.com/", 10, 90); }}
95%响应时间达到2232毫秒
划线
评论
复制
发布于: 2020 年 07 月 23 日 阅读数: 31
王志祥
关注
还未添加个人签名 2017.10.19 加入
还未添加个人简介
评论