压力测试 - tio-boot 12 C 32G
1. 测试概述
本文档主要通过 ApacheBench (ab) 和 wrk 两种工具,对基于 [tio-boot][^1] 搭建的本地服务器进行高并发访问压力测试,以评估其在大规模请求场景下的性能表现和稳定性。
2. 环境与测试代码
代码开源地址 https://github.com/litongjava/tio-boot-web-benchmarker
2.1 环境信息
- 服务器软件:t-io
- 服务器主机名:localhost
- 服务器端口:80
- 操作系统:Linux
- JDK:1.8
- 测试工具:
- ApacheBench (ab)
- wrk
2.2 关键依赖
<dependencies>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>tio-boot</artifactId>
<version>${tio.boot.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>jfinal-aop</artifactId>
<version>1.3.5/version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>hotswap-classloader</artifactId>
<version>${hotswap-classloader.version}</version>
</dependency>
</dependencies>
2.3 启动类与测试接口
启动类
package com.litongjava.tio.web.hello;
import com.litongjava.annotation.AComponentScan;
import com.litongjava.tio.boot.TioApplication;
@AComponentScan
public class HelloApp {
public static void main(String[] args) {
long start = System.currentTimeMillis();
TioApplication.run(HelloApp.class, args);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
}
配置类
package com.litongjava.tio.web.hello.config;
import com.litongjava.annotation.AConfiguration;
import com.litongjava.annotation.Initialization;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.http.server.router.HttpRequestRouter;
import com.litongjava.tio.web.hello.handler.HelloHandler;
@AConfiguration
public class WebHelloConfig {
@Initialization
public void config() {
TioBootServer server = TioBootServer.me();
HttpRequestRouter requestRouter = server.getRequestRouter();
HelloHandler helloHandler = new HelloHandler();
requestRouter.add("/hello", helloHandler::hello);
}
}
handler
package com.litongjava.tio.web.hello.handler;
import java.util.HashMap;
import java.util.Map;
import com.litongjava.model.body.RespBodyVo;
import com.litongjava.tio.boot.http.TioRequestContext;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
public class HelloHandler {
public HttpResponse hello(HttpRequest request) {
// 如有需要,手动提取参数
// 例如:String param = request.getParam("key");
Map<String, String> data = new HashMap<>();
RespBodyVo respVo = RespBodyVo.ok(data);
return TioRequestContext.getResponse().setJson(respVo);
}
}
3. 服务器配置
1. 系统资源检查
- CPU 和内存
确认服务器的 CPU 核心数和内存大小,确保有足够的资源应对高并发测试。lscpu free -m cat /proc/meminfo
lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 12
On-line CPU(s) list: 0-11
Vendor ID: GenuineIntel
BIOS Vendor ID: Intel(R) Corporation
Model name: 12th Gen Intel(R) Core(TM) i5-12400
BIOS Model name: 12th Gen Intel(R) Core(TM) i5-12400 To Be Filled By O.E.M. CPU @ 3.9GHz
BIOS CPU family: 205
CPU family: 6
Model: 151
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Stepping: 5
CPU(s) scaling MHz: 14%
CPU max MHz: 5600.0000
CPU min MHz: 800.0000
BogoMIPS: 4992.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs
bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt
tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adju
st bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp
_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization features:
Virtualization: VT-x
Caches (sum of all):
L1d: 288 KiB (6 instances)
L1i: 192 KiB (6 instances)
L2: 7.5 MiB (6 instances)
L3: 18 MiB (1 instance)
NUMA:
NUMA node(s): 1
NUMA node0 CPU(s): 0-11
Vulnerabilities:
Gather data sampling: Not affected
Itlb multihit: Not affected
L1tf: Not affected
Mds: Not affected
Meltdown: Not affected
Mmio stale data: Not affected
Reg file data sampling: Not affected
Retbleed: Not affected
Spec rstack overflow: Not affected
Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Srbds: Not affected
Tsx async abort: Not affected
free -m
total used free shared buff/cache available
Mem: 31838 10844 18614 28 2840 20994
Swap: 975 0 975
cat /proc/meminfo
MemTotal: 32602888 kB
MemFree: 19063872 kB
MemAvailable: 21501684 kB
Buffers: 259064 kB
Cached: 2501424 kB
SwapCached: 0 kB
Active: 1148036 kB
Inactive: 11721412 kB
Active(anon): 6308 kB
Inactive(anon): 10131344 kB
Active(file): 1141728 kB
Inactive(file): 1590068 kB
Unevictable: 9592 kB
Mlocked: 0 kB
SwapTotal: 999420 kB
SwapFree: 999420 kB
Zswap: 0 kB
Zswapped: 0 kB
Dirty: 400 kB
Writeback: 0 kB
AnonPages: 10110680 kB
Mapped: 513092 kB
Shmem: 28692 kB
KReclaimable: 148764 kB
Slab: 266040 kB
SReclaimable: 148764 kB
SUnreclaim: 117276 kB
KernelStack: 22672 kB
PageTables: 41468 kB
SecPageTables: 0 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 17300864 kB
Committed_AS: 22011920 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 50588 kB
VmallocChunk: 0 kB
Percpu: 14528 kB
HardwareCorrupted: 0 kB
AnonHugePages: 9156608 kB
ShmemHugePages: 8192 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 247608 kB
DirectMap2M: 6828032 kB
DirectMap1G: 26214400 kB
2. 文件描述符和进程限制
- 文件描述符限制
高并发连接时需要足够的文件描述符数量。查看当前限制:临时调整:ulimit -n
或修改ulimit -n 65535
/etc/security/limits.conf
进行永久配置。
3. 网络参数调优
- TCP 参数
查看并根据需要调整 TCP 相关参数,比如最大连接数、TIME_WAIT 处理、socket 缓冲区大小等。
查看 TCP 参数:sysctl -a | grep net.ipv4.tcp
- socket 队列
检查并调整net.core.somaxconn
参数来允许更多的挂起连接:sysctl net.core.somaxconn sudo sysctl -w net.core.somaxconn=4096
- 其他优化
如net.ipv4.tcp_tw_reuse
、net.ipv4.tcp_fin_timeout
等参数,根据具体业务场景进行调优:sysctl -w net.ipv4.tcp_tw_reuse=2 sysctl -w net.ipv4.tcp_fin_timeout=60
3. ApacheBench 压力测试
3.1 测试命令
ab -c1000 -n10000000 http://localhost/hello
- 并发级别(-c):1000
- 请求总数(-n):10,000,000
3.2 测试结果
root@debian:~# ab -c1000 -n10000000 http://localhost:8080/hello
This is ApacheBench, Version 2.3 <$Revision: 1913912 $>
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 1000000 requests
Completed 2000000 requests
Completed 3000000 requests
Completed 4000000 requests
Completed 5000000 requests
Completed 6000000 requests
Completed 7000000 requests
Completed 8000000 requests
Completed 9000000 requests
Completed 10000000 requests
Finished 10000000 requests
Server Software: -io
Server Hostname: localhost
Server Port: 8080
Document Path: /hello
Document Length: 54 bytes
Concurrency Level: 1000
Time taken for tests: 197.529 seconds
Complete requests: 10000000
Failed requests: 0
Total transferred: 1860000000 bytes
HTML transferred: 540000000 bytes
Requests per second: 50625.58 [#/sec] (mean)
Time per request: 19.753 [ms] (mean)
Time per request: 0.020 [ms] (mean, across all concurrent requests)
Transfer rate: 9195.66 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 9 2.0 9 21
Processing: 5 10 2.1 10 59
Waiting: 2 6 2.4 6 54
Total: 13 20 0.9 20 64
Percentage of the requests served within a certain time (ms)
50% 20
66% 20
75% 20
80% 20
90% 20
95% 21
98% 22
99% 23
100% 64 (longest request)
Ab 报告分析
略