- Added request/response flow diagrams to api-design and api-gateway-advanced skills for better visualization of processes. - Introduced configuration loading flow in configuration-management skill to clarify the configuration process. - Included error propagation flow in error-handling-patterns skill to illustrate error handling across layers. - Enhanced various skills with additional diagrams to improve understanding of complex concepts. These updates aim to provide clearer guidance and improve the overall documentation experience for developers.
155 lines
5.8 KiB
Markdown
155 lines
5.8 KiB
Markdown
# Tối Ưu Hiệu Suất (Performance Optimization)
|
|
|
|
Performance optimization patterns for GoodGo microservices including database query optimization, memory leak detection, profiling, connection pooling, and caching strategies.
|
|
> Các patterns tối ưu hiệu suất cho GoodGo microservices bao gồm tối ưu database queries, phát hiện memory leaks, profiling, connection pooling, và caching strategies.
|
|
|
|
## Tổng Quan
|
|
|
|
Performance optimization patterns help identify and fix performance bottlenecks, optimize database queries, detect memory leaks, and improve overall application performance.
|
|
|
|
Các patterns tối ưu hiệu suất giúp xác định và sửa các nút cổ chai hiệu suất, tối ưu database queries, phát hiện memory leaks, và cải thiện hiệu suất ứng dụng tổng thể.
|
|
|
|
## Khi Nào Sử Dụng
|
|
|
|
Use this skill when optimizing performance, profiling applications, or detecting bottlenecks.
|
|
|
|
Sử dụng skill này khi tối ưu hiệu suất, profiling ứng dụng, hoặc phát hiện bottlenecks.
|
|
|
|
## Quy Trình Tối Ưu Hiệu Suất
|
|
|
|
Quy trình tối ưu hiệu suất tuân theo phương pháp có hệ thống để xác định, phân tích và giải quyết các nút cổ chai hiệu suất.
|
|
|
|
The performance optimization process follows a systematic approach to identify, analyze, and resolve performance bottlenecks.
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
Start([Start Optimization]) --> Identify[Identify Performance Issue]
|
|
Identify --> Monitor[Monitor Metrics]
|
|
Monitor --> Profiling[Profile Application]
|
|
Profiling --> Analyze[Analyze Results]
|
|
Analyze --> IdentifyBottleneck{Identify Bottleneck}
|
|
|
|
IdentifyBottleneck -->|Database| OptimizeDB[Optimize Queries]
|
|
IdentifyBottleneck -->|Memory| OptimizeMem[Fix Memory Leaks]
|
|
IdentifyBottleneck -->|Network| OptimizeNet[Optimize Connections]
|
|
IdentifyBottleneck -->|Cache| OptimizeCache[Improve Caching]
|
|
|
|
OptimizeDB --> Implement[Implement Optimization]
|
|
OptimizeMem --> Implement
|
|
OptimizeNet --> Implement
|
|
OptimizeCache --> Implement
|
|
|
|
Implement --> Test[Test Changes]
|
|
Test --> Verify{Performance Improved?}
|
|
Verify -->|Yes| Monitor
|
|
Verify -->|No| Analyze
|
|
Monitor --> Threshold{Meets Targets?}
|
|
Threshold -->|Yes| Complete([Optimization Complete])
|
|
Threshold -->|No| Profiling
|
|
```
|
|
|
|
## Quy Trình Tối Ưu Query
|
|
|
|
Tối ưu database query là một khía cạnh quan trọng của hiệu suất. Quy trình này cho thấy cách tối ưu query một cách có hệ thống.
|
|
|
|
Database query optimization is a critical aspect of performance. This flow shows how to systematically optimize queries.
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
Start([Query Performance Issue]) --> Analyze[Analyze Query Performance]
|
|
Analyze --> CheckIndexes[Check Indexes]
|
|
CheckIndexes --> Explain[Run EXPLAIN ANALYZE]
|
|
Explain --> IdentifyIssues{Identify Issues}
|
|
|
|
IdentifyIssues -->|N+1 Queries| FixN1[Use Include/Join]
|
|
IdentifyIssues -->|Missing Index| AddIndex[Add Database Index]
|
|
IdentifyIssues -->|Slow Query| OptimizeQuery[Rewrite Query]
|
|
IdentifyIssues -->|Unbounded| AddPagination[Add Pagination]
|
|
|
|
FixN1 --> VerifyQuery[Verify Query Performance]
|
|
AddIndex --> VerifyQuery
|
|
OptimizeQuery --> VerifyQuery
|
|
AddPagination --> VerifyQuery
|
|
|
|
VerifyQuery --> TestQuery{Query Time < 50ms?}
|
|
TestQuery -->|Yes| Complete([Optimization Complete])
|
|
TestQuery -->|No| Analyze
|
|
|
|
style FixN1 fill:#e1f5e1
|
|
style AddIndex fill:#e1f5e1
|
|
style OptimizeQuery fill:#e1f5e1
|
|
style AddPagination fill:#e1f5e1
|
|
```
|
|
|
|
## Quy Trình Profiling
|
|
|
|
Quy trình profiling giúp xác định các nút cổ chai hiệu suất thông qua việc thu thập và phân tích dữ liệu có hệ thống.
|
|
|
|
The profiling process helps identify performance bottlenecks through systematic data collection and analysis.
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
Start([Start Profiling]) --> Setup[Setup Profiling Tools]
|
|
Setup --> ChooseTool{Choose Profiling Type}
|
|
|
|
ChooseTool -->|CPU| CPUProf[CPU Profiling]
|
|
ChooseTool -->|Memory| MemProf[Memory Profiling]
|
|
ChooseTool -->|Database| DBProf[Database Query Profiling]
|
|
|
|
CPUProf --> CollectCPU[Collect CPU Metrics]
|
|
MemProf --> CollectMem[Collect Memory Metrics]
|
|
DBProf --> CollectDB[Collect Query Metrics]
|
|
|
|
CollectCPU --> Analyze[Analyze Profiling Data]
|
|
CollectMem --> Analyze
|
|
CollectDB --> Analyze
|
|
|
|
Analyze --> IdentifyHotspots[Identify Hotspots]
|
|
IdentifyHotspots --> Prioritize[Prioritize Issues]
|
|
Prioritize --> Optimize[Optimize Critical Paths]
|
|
Optimize --> ReProfile[Re-run Profiling]
|
|
ReProfile --> Compare{Performance Improved?}
|
|
Compare -->|Yes| Complete([Profiling Complete])
|
|
Compare -->|No| IdentifyHotspots
|
|
|
|
style CPUProf fill:#e3f2fd
|
|
style MemProf fill:#e3f2fd
|
|
style DBProf fill:#e3f2fd
|
|
```
|
|
|
|
## Các Patterns Chính
|
|
|
|
### Database Query Optimization / Tối Ưu Database Queries
|
|
|
|
```typescript
|
|
// EN: Avoid N+1 queries
|
|
// VI: Tránh N+1 queries
|
|
const users = await userRepository.findAll({
|
|
include: { orders: true }, // EN: Single query / VI: Single query
|
|
});
|
|
```
|
|
|
|
### Memory Profiling / Memory Profiling
|
|
|
|
```typescript
|
|
// EN: Monitor memory usage
|
|
// VI: Giám sát memory usage
|
|
const profiler = new MemoryProfiler();
|
|
profiler.start();
|
|
```
|
|
|
|
## Best Practices / Thực Hành Tốt
|
|
|
|
1. Use indexes, avoid N+1 queries / Sử dụng indexes, tránh N+1 queries
|
|
2. Monitor memory usage / Giám sát memory usage
|
|
3. Cache frequently accessed data / Cache dữ liệu thường truy cập
|
|
|
|
## Skills Liên Quan
|
|
|
|
- [Caching Patterns](./caching-patterns.md) - Caching strategies / Chiến lược caching
|
|
- [Observability & Monitoring](./observability-monitoring.md) - Monitoring / Giám sát
|
|
|
|
## Tài Nguyên
|
|
|
|
- Skill Source: `.cursor/skills/performance-optimization/SKILL.md`
|