1. spring boot 異步請(qǐng)求
- 1.1 創(chuàng)建一個(gè)spring boot項(xiàng)目阴汇,并在pom.xml中引入需要的依賴(lài)內(nèi)容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 1.2 通過(guò)@EnableAsync注解開(kāi)啟異步執(zhí)行
@SpringBootApplication
@EnableAsync
public class SpringBootAsyncApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAsyncApplication.class, args);
}
}
- 1.3 創(chuàng)建一個(gè)AsyncTestController绩脆、sync 是同步請(qǐng)求麦乞,async是異步請(qǐng)求
@RestController
public class AsyncTestController {
@RequestMapping(value = "/sync", method = RequestMethod.GET)
public String sync() throws InterruptedException {
Thread.sleep(1000);
return "sync";
}
@RequestMapping("/async")
public Callable<String> callable() {
// 使用異步將不會(huì)阻塞tomcat的io讀寫(xiě)線(xiàn)程池、使得增加系統(tǒng)的吞吐量
return new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return "hello";
}
};
}
}
- 1.4 application.properties 配置文件
server.tomcat.max-threads=20
server.tomcat.max-connections=10000
2. jmeter 性能測(cè)壓
- 2.1 使用jmeter 進(jìn)行測(cè)壓 1000個(gè)線(xiàn)程同時(shí)請(qǐng)求http://localhost:8080/sync
image.png - 2.1.1 同步1000次測(cè)壓結(jié)果
1.Average:平均用戶(hù)響應(yīng)時(shí)間 24570毫秒
2.Median: 50%用戶(hù)的響應(yīng)時(shí)間 24570毫秒
3.90%Line:90%用戶(hù)的響應(yīng)時(shí)間 44091毫秒
4.95%Line:95%用戶(hù)的響應(yīng)時(shí)間 47040毫秒
5.99%Line:99%用戶(hù)的響應(yīng)時(shí)間 49018毫秒
6.Min: 最小響應(yīng)時(shí)間 1011毫秒
7.Max: 最大響應(yīng)時(shí)間 49034毫秒
8.Throughput:吞吐量 19.9/sec
- 2.2 使用jmeter 進(jìn)行測(cè)壓 1000個(gè)線(xiàn)程同時(shí)請(qǐng)求http://localhost:8080/async
image.png - 2.2.1 異步1000次測(cè)壓結(jié)果
1.Average:平均用戶(hù)響應(yīng)時(shí)間 1086毫秒
2.Median: 50%用戶(hù)的響應(yīng)時(shí)間 1047毫秒
3.90%Line:90%用戶(hù)的響應(yīng)時(shí)間 1276毫秒
4.95%Line:95%用戶(hù)的響應(yīng)時(shí)間 1303毫秒
5.99%Line:99%用戶(hù)的響應(yīng)時(shí)間 1325毫秒
6.Min: 最小響應(yīng)時(shí)間 1002毫秒
7.Max: 最大響應(yīng)時(shí)間 1351毫秒
8.Throughput:吞吐量 417.9/sec
- 2.3 測(cè)壓結(jié)論
平均結(jié)果上異步處理和同步處理的性能相差20-40倍之間,然后這個(gè)測(cè)試并不是準(zhǔn)確,但是從中也可以看出異步處理對(duì)整體的性能提升是非车L溃可觀(guān)。以上結(jié)論也是僅供參考洼冻。
3. ab 性能測(cè)壓
image.png