|
| 1 | +# Ditto Attachment Stress Testing |
| 2 | + |
| 3 | +This document describes the attachment stress testing setup for the Flutter Ditto quickstart app. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Run basic stress tests |
| 9 | +cd flutter_app |
| 10 | +./run_stress_test.sh |
| 11 | + |
| 12 | +# Run with memory monitoring (macOS only) |
| 13 | +./run_stress_test.sh --watch |
| 14 | + |
| 15 | +# View help |
| 16 | +./run_stress_test.sh --help |
| 17 | +``` |
| 18 | + |
| 19 | +## Test Suite Overview |
| 20 | + |
| 21 | +The stress tests are located in `test/attachment_stress_test.dart` and include: |
| 22 | + |
| 23 | +### 1. Small Attachment Test |
| 24 | +- **Purpose**: Test rapid creation and deletion of many small attachments |
| 25 | +- **Configuration**: 50 tasks with 1KB attachments each |
| 26 | +- **Metrics**: |
| 27 | + - Time to create all tasks with attachments |
| 28 | + - Time to fetch all attachments |
| 29 | + - Time to delete/evict all tasks |
| 30 | + - Average time per operation |
| 31 | + |
| 32 | +### 2. Large Attachment Test |
| 33 | +- **Purpose**: Test handling of larger attachment payloads |
| 34 | +- **Configuration**: 10 tasks with 100KB attachments each |
| 35 | +- **Metrics**: |
| 36 | + - Time to create large attachments |
| 37 | + - Time to delete large attachments |
| 38 | + - Average time per operation |
| 39 | + |
| 40 | +### 3. Rapid Create/Delete Cycle |
| 41 | +- **Purpose**: Simulate rapid churn of attachments |
| 42 | +- **Configuration**: 20 cycles × 5 tasks (100 create + 100 delete operations) |
| 43 | +- **Metrics**: |
| 44 | + - Total cycle time |
| 45 | + - Average operation time |
| 46 | + - Cleanup verification |
| 47 | + |
| 48 | +### 4. Memory Tracking (Placeholder) |
| 49 | +- **Purpose**: Monitor memory usage during attachment operations |
| 50 | +- **Status**: Not yet implemented |
| 51 | +- **Future Implementation**: See [Memory Monitoring](#memory-monitoring) section |
| 52 | + |
| 53 | +## Running Tests |
| 54 | + |
| 55 | +### Basic Test Run |
| 56 | +```bash |
| 57 | +cd flutter_app |
| 58 | +flutter test test/attachment_stress_test.dart |
| 59 | +``` |
| 60 | + |
| 61 | +### Using the Helper Script |
| 62 | +```bash |
| 63 | +# Run all stress tests |
| 64 | +./run_stress_test.sh |
| 65 | + |
| 66 | +# Run with memory monitoring |
| 67 | +./run_stress_test.sh --watch |
| 68 | +``` |
| 69 | + |
| 70 | +### Run Specific Test |
| 71 | +```bash |
| 72 | +flutter test test/attachment_stress_test.dart --name "Small Attachment" |
| 73 | +``` |
| 74 | + |
| 75 | +## Memory Monitoring |
| 76 | + |
| 77 | +### Current Limitations |
| 78 | +The current memory tracking is a placeholder. True memory profiling requires platform-specific implementation. |
| 79 | + |
| 80 | +### Future Implementation Options |
| 81 | + |
| 82 | +#### Option 1: Flutter DevTools |
| 83 | +```bash |
| 84 | +# Install DevTools |
| 85 | +flutter pub global activate devtools |
| 86 | + |
| 87 | +# Run DevTools |
| 88 | +flutter pub global run devtools |
| 89 | + |
| 90 | +# Run app in profile mode |
| 91 | +flutter run --profile |
| 92 | + |
| 93 | +# Use DevTools UI to monitor memory |
| 94 | +``` |
| 95 | + |
| 96 | +#### Option 2: Native Platform Channels |
| 97 | +Create platform channels to access native memory APIs: |
| 98 | + |
| 99 | +**iOS (Objective-C/Swift)**: |
| 100 | +```swift |
| 101 | +// Get memory usage |
| 102 | +let info = mach_task_basic_info() |
| 103 | +// Access resident_size, virtual_size |
| 104 | +``` |
| 105 | + |
| 106 | +**Android (Kotlin)**: |
| 107 | +```kotlin |
| 108 | +val runtime = Runtime.getRuntime() |
| 109 | +val usedMemory = runtime.totalMemory() - runtime.freeMemory() |
| 110 | +``` |
| 111 | + |
| 112 | +#### Option 3: macOS Activity Monitor |
| 113 | +```bash |
| 114 | +# Watch dart process memory in real-time |
| 115 | +./run_stress_test.sh --watch |
| 116 | +``` |
| 117 | + |
| 118 | +### Manual Memory Profiling |
| 119 | + |
| 120 | +1. **Baseline**: Record memory before test |
| 121 | +2. **Peak**: Monitor memory during attachment creation |
| 122 | +3. **Post-Operation**: Check memory after operations |
| 123 | +4. **Cleanup**: Verify memory is released after eviction |
| 124 | +5. **Cycles**: Run multiple times to detect leaks |
| 125 | + |
| 126 | +## Performance Metrics |
| 127 | + |
| 128 | +### Expected Benchmarks |
| 129 | + |
| 130 | +These are approximate targets; actual performance varies by platform and device: |
| 131 | + |
| 132 | +| Operation | Target | Notes | |
| 133 | +|-----------|--------|-------| |
| 134 | +| Create 1KB attachment | < 50ms | Per attachment creation | |
| 135 | +| Create 100KB attachment | < 200ms | Larger payloads | |
| 136 | +| Fetch 1KB attachment | < 100ms | Network dependent | |
| 137 | +| Evict all tasks | < 500ms | Batch operation | |
| 138 | + |
| 139 | +### Analyzing Results |
| 140 | + |
| 141 | +Look for: |
| 142 | +- **Linear scaling**: Time should scale linearly with attachment count/size |
| 143 | +- **Consistent performance**: Similar operations should take similar time |
| 144 | +- **Memory cleanup**: Memory should return to baseline after eviction |
| 145 | +- **No crashes**: All tests should complete without errors |
| 146 | + |
| 147 | +## Customizing Tests |
| 148 | + |
| 149 | +### Adjust Test Parameters |
| 150 | + |
| 151 | +Edit `test/attachment_stress_test.dart`: |
| 152 | + |
| 153 | +```dart |
| 154 | +// Change number of tasks |
| 155 | +const numTasks = 100; // Increase for more stress |
| 156 | +
|
| 157 | +// Change attachment size |
| 158 | +const attachmentSize = 1024 * 1024; // 1MB |
| 159 | +
|
| 160 | +// Change number of cycles |
| 161 | +const cycles = 50; |
| 162 | +``` |
| 163 | + |
| 164 | +### Add Custom Tests |
| 165 | + |
| 166 | +```dart |
| 167 | +test('Custom stress test', () async { |
| 168 | + // Your custom test logic |
| 169 | + // - Create attachments with specific patterns |
| 170 | + // - Test concurrent operations |
| 171 | + // - Test error handling |
| 172 | + // - Test edge cases |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +## Troubleshooting |
| 177 | + |
| 178 | +### Test Timeout |
| 179 | +If tests timeout, increase the timeout: |
| 180 | +```dart |
| 181 | +test('Long running test', () async { |
| 182 | + // Test code |
| 183 | +}, timeout: Timeout(Duration(minutes: 5))); |
| 184 | +``` |
| 185 | + |
| 186 | +### Out of Memory |
| 187 | +Reduce test parameters: |
| 188 | +- Decrease `numTasks` |
| 189 | +- Decrease `attachmentSize` |
| 190 | +- Add delays between operations |
| 191 | + |
| 192 | +### Connection Issues |
| 193 | +Ensure Ditto credentials are correct in `.env`: |
| 194 | +```bash |
| 195 | +DITTO_APP_ID=your_app_id |
| 196 | +DITTO_PLAYGROUND_TOKEN=your_token |
| 197 | +DITTO_WEBSOCKET_URL=your_websocket_url |
| 198 | +``` |
| 199 | + |
| 200 | +## Platform-Specific Considerations |
| 201 | + |
| 202 | +### iOS |
| 203 | +- Test on physical devices for accurate memory profiling |
| 204 | +- Use Instruments for detailed performance analysis |
| 205 | +- Monitor memory warnings |
| 206 | + |
| 207 | +### Android |
| 208 | +- Use Android Studio Profiler for memory tracking |
| 209 | +- Test on various devices (different RAM configurations) |
| 210 | +- Monitor GC activity |
| 211 | + |
| 212 | +### macOS |
| 213 | +- Use Activity Monitor for process memory |
| 214 | +- Check Console.app for Ditto logs |
| 215 | +- Test both Intel and Apple Silicon if possible |
| 216 | + |
| 217 | +### Web |
| 218 | +- Use browser DevTools for memory profiling |
| 219 | +- Note: P2P sync is limited on web platform |
| 220 | +- Attachments may have different performance characteristics |
| 221 | + |
| 222 | +## Future Enhancements |
| 223 | + |
| 224 | +### Short Term |
| 225 | +- [ ] Implement native memory tracking |
| 226 | +- [ ] Add concurrent operation tests |
| 227 | +- [ ] Add error injection tests |
| 228 | +- [ ] Add network latency simulation |
| 229 | + |
| 230 | +### Long Term |
| 231 | +- [ ] Automated CI/CD performance regression testing |
| 232 | +- [ ] Historical performance tracking |
| 233 | +- [ ] Cross-platform performance comparison |
| 234 | +- [ ] Memory leak detection automation |
| 235 | +- [ ] Load testing with multiple devices |
| 236 | + |
| 237 | +## Resources |
| 238 | + |
| 239 | +- [Flutter DevTools](https://docs.flutter.dev/tools/devtools) |
| 240 | +- [Ditto Attachments Documentation](https://docs.ditto.live) |
| 241 | +- [Flutter Performance Best Practices](https://docs.flutter.dev/perf/best-practices) |
| 242 | +- [Dart VM Performance](https://dart.dev/guides/language/performance) |
| 243 | + |
| 244 | +## Contributing |
| 245 | + |
| 246 | +When adding new stress tests: |
| 247 | +1. Document the test purpose and configuration |
| 248 | +2. Include relevant metrics |
| 249 | +3. Update this README |
| 250 | +4. Consider platform differences |
| 251 | +5. Add cleanup to avoid test pollution |
0 commit comments