Performance Testing with K6
Load test your APIs with integrated K6 support
Bytedocs includes integrated K6 load testing, allowing you to test API performance directly from the documentation UI.
Availability
K6 Performance Testing is currently available only in the Laravel implementation. Support for other languages is planned.
What is K6?
K6 is Grafana's open-source load testing tool designed for testing the performance, load capacity, and reliability of APIs and websites.
Why K6?
- Developer-friendly: Write tests in JavaScript
- Accurate: Produces realistic load
- Flexible: Support for various testing scenarios
- Detailed metrics: Comprehensive performance data
- Open source: Free and actively maintained
Prerequisites
Install K6
macOS
Linux
Windows
Docker
Verify Installation
Setup in Laravel
K6 integration is automatically available in Laravel once Bytedocs is installed.
Configuration
Environment Variables
Running Performance Tests
Via UI
- Open Bytedocs at
/docs - Select an endpoint you want to test
- Click "Performance Test" button
- Configure test parameters:
- Virtual Users (VUs)
- Test duration
- Ramp-up stages (optional)
- Click "Run Test"
- View results in real-time
Via API
Test Configuration
Constant Load
Run with a fixed number of virtual users:
Generated K6 script:
Stages (Ramp Up/Down)
Gradually increase and decrease load:
Generated K6 script:
Stress Test
Find breaking point:
Spike Test
Sudden traffic surge:
Soak Test (Endurance)
Long-duration test:
Test Parameters
Virtual Users (VUs)
Number of concurrent users:
Guidelines:
- 10-50: Light load, basic testing
- 50-200: Medium load, typical usage
- 200-1000: Heavy load, peak traffic
- 1000+: Stress testing, extreme scenarios
Duration
Test length:
Formats:
"30s"- 30 seconds"5m"- 5 minutes"1h"- 1 hour"2h30m"- 2 hours 30 minutes
Request Configuration
Thresholds
Define pass/fail criteria:
Understanding Results
Key Metrics
HTTP Requests
- Total requests made
- (Requests per second)
Request Duration
- avg: Average response time
- min: Fastest response
- med: Median response
- max: Slowest response
- p(90): 90th percentile
- p(95): 95th percentile
Failed Requests
- Percentage of failed requests
- (Count)
Data Transfer
- Total data transferred
- (Rate per second)
Virtual Users
- Current VUs
- Max VUs during test
Response Time Guidelines
| Percentile | Target | Good | Acceptable | Poor |
|---|---|---|---|---|
| Average | < 100ms | < 200ms | < 500ms | > 500ms |
| P95 | < 200ms | < 500ms | < 1s | > 1s |
| P99 | < 500ms | < 1s | < 2s | > 2s |
Error Rate Guidelines
| Error Rate | Status |
|---|---|
| 0% | Excellent |
| < 0.1% | Good |
| 0.1% - 1% | Acceptable |
| > 1% | Poor |
Interpreting Results
Example Output
Analysis
✅ Good Performance:
- Average response time: 234ms
- P95 response time: 450ms
- Error rate: 0.33% (acceptable)
- Throughput: 100 req/s
⚠️ Areas to Watch:
- Max response time: 1.2s (investigate slow requests)
- Some connection delays (max 234ms blocked time)
Advanced K6 Features
Custom Scripts
Create advanced test scenarios:
Environment-Specific Tests
Run with environment variables:
Best Practices
1. Start Small
Gradually increase load.
2. Use Realistic Scenarios
Simulate actual user behavior:
3. Set Thresholds
Define success criteria:
4. Test Different Scenarios
- Happy path: Normal user flow
- Edge cases: Max input sizes, unusual patterns
- Error handling: Invalid inputs, auth failures
5. Monitor Server Resources
While running tests, monitor:
- CPU usage
- Memory usage
- Database connections
- Network I/O
Troubleshooting
K6 Not Found
Error: k6 executable not found
Solutions:
- Verify installation:
k6 version - Check PATH:
which k6 - Specify path in config:
High Error Rates
Causes:
- Server overloaded
- Database connections exhausted
- Timeout too short
- Network issues
Solutions:
- Reduce VUs
- Increase server resources
- Optimize database queries
- Check network latency
Inconsistent Results
Causes:
- Background processes
- Network variability
- Caching effects
Solutions:
- Run multiple tests
- Test at different times
- Use dedicated test environment
- Clear caches between tests
What's Next?
- Scenarios - Multi-endpoint test flows
- Interactive Testing - Manual endpoint testing
- Configuration - Configure K6 settings