一:我的-饮食模式设置-食物组构成-查询
wrk -t2 -c50 -d30s --latency \
-H "Authorization: Bearer eyJhbGc..." \
"http://192.168.31.109:30289/xs-json/health/food-group/user-composition?eating_pattern_id=3&estimated_calorie_needs_id=8"
Running 30s test @ http://192.168.31.109:30289/xs-json/health/food-group/user-composition?eating_pattern_id=3&estimated_calorie_needs_id=8
2 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 44.38ms 16.67ms 138.28ms 68.18%
Req/Sec 566.50 83.91 1.06k 71.24%
Latency Distribution
50% 42.03ms
75% 52.73ms
90% 65.56ms
99% 91.01ms
33910 requests in 30.08s, 234.66MB read
Requests/sec: 1127.35
Transfer/sec: 7.80MB
我用 wrk 做基准压测,摘掉限流、带上合法 JWT,并发 50 压 30 秒;P99 延迟 91ms,远低于 200ms 目标,QPS 稳定 1k+;这条 SQL 本身执行仅 2ms,大部分耗时消耗在 GORM 数据扫描、结构体序列化、网络和协程调度上。
加了redis缓存继续wrk压测结果如下:
Running 30s test @ http://192.168.31.109:30289/xs-json/health/food-group/user-composition?eating_pattern_id=3&estimated_calorie_needs_id=8
2 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 12.04ms 12.08ms 141.93ms 87.19%
Req/Sec 2.69k 575.70 3.75k 56.78%
Latency Distribution
50% 7.65ms
75% 13.07ms
90% 30.00ms
99% 52.19ms
160493 requests in 30.06s, 1.08GB read
Requests/sec: 5339.70
Transfer/sec: 36.95MB
针对膳食食物组推荐读多写少接口,引入 Redis 实现 Cache Aside 缓存方案,通过分布式锁防止缓存击穿、TTL 随机偏移规避雪崩、空值缓存解决穿透;基准压测对比:优化前 QPS 约 1100、P99 91ms,接入缓存后 QPS 提升至 5300+,P99 RT 降至 52ms,显著降低 MySQL 多表关联查询压力。