Task 3-1: Benchmark viewer-get-livestream với Concurrent Requests
Phase: 3 - Performance Tuning Priority: Medium Depends on: task-1-1 Reference: docs/BountyHunter-Backend/details/feature-livestream/SPEC.md
Background
viewerGetLivestreamById() được gọi rất thường xuyên khi có nhiều viewers. Đã có async ban check và cached credentials, nhưng chưa có benchmark data để biết liệu performance đã đủ chưa.
Tasks
Setup benchmark
DI Note: Trước khi benchmark, đảm bảo application đã được inject đúng profile production-like (DB connection pool, Redis cache bật). Chạy warm-up request trước để JVM JIT compile các hot paths.
- [ ] Dùng
wrkhoặc JMeter để benchmark (installwrk:brew install wrk):# 100 concurrent users, 30 giây wrk -t4 -c100 -d30s \ -H "Authorization: Bearer <token>" \ "http://localhost:8083/api/livestream/{id}" - [ ] Thay
{id}bằng ID livestream đang active trên môi trường test - [ ] Chạy ít nhất 3 lần và lấy median để loại bỏ outlier
Metrics cần đo
- [ ] P50, P95, P99 latency
- [ ] Throughput (req/sec)
- [ ] Error rate
- [ ] CPU/memory usage trong 30s (dùng
jconsolehoặcjstat)
Analyze bottlenecks
- [ ] Nếu DB query chiếm > 50ms → add index hoặc cache (check slow query log)
- [ ] Nếu Agora token generation chậm → cache token với TTL ngắn hơn expiry (ví dụ: cache 30 phút với token expire 1 giờ)
- [ ] Nếu Firebase RTDB check chậm → remove hoặc make async (non-blocking)
Expected targets
- P95 latency < 200ms tại 100 concurrent viewers
- Error rate < 0.1%
Verification / Acceptance Criteria
- [ ] Benchmark report tồn tại (file hoặc wiki) với P50, P95, P99 và throughput values
- [ ] P95 latency ≤ 200ms tại 100 concurrent requests (sau tuning nếu cần)
- [ ] Error rate < 0.1% trong suốt 30s benchmark
- [ ] Nếu bottleneck được phát hiện → fix được implement và re-benchmark confirm improvement
- [ ] Không có regression: P95 không tăng hơn 20% so với baseline sau các thay đổi trong task-1-1
Files to Modify
livestreamweb/src/main/java/com/figpop/livestreamweb/controllers/livestream/LivestreamController.java(nếu cần tuning)application-core/service/livestream/LivestreamService.java(nếu cần caching/index)