|
| 1 | +# Performance Tests for NotificationApi Batch Processing Optimization |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the performance tests created to validate and measure the optimization improvements implemented in GitHub issue #148. |
| 6 | + |
| 7 | +## Optimization Summary |
| 8 | + |
| 9 | +The performance optimization addresses memory efficiency issues when handling large target collections: |
| 10 | + |
| 11 | +### Key Optimizations |
| 12 | + |
| 13 | +1. **`targets_empty?` optimization** (line 232 in notification_api.rb) |
| 14 | + - **Before**: Used `targets.blank?` which loads all records into memory |
| 15 | + - **After**: Uses `targets.exists?` for ActiveRecord/Mongoid (SQL: `SELECT 1 LIMIT 1`) |
| 16 | + - **Benefit**: Avoids loading thousands of records just to check if collection is empty |
| 17 | + |
| 18 | +2. **`process_targets_in_batches` optimization** (line 303 in notification_api.rb) |
| 19 | + - **Before**: Used `targets.map` which loads all records into memory at once |
| 20 | + - **After**: |
| 21 | + - ActiveRecord relations: Uses `find_each` (default batch size: 1000) |
| 22 | + - Mongoid criteria: Uses `each` with cursor batching |
| 23 | + - Arrays: Falls back to standard `map` (already in memory) |
| 24 | + - **Benefit**: Processes large collections in batches to minimize memory footprint |
| 25 | + |
| 26 | +### Expected Performance Improvements |
| 27 | + |
| 28 | +Based on the commit message, the optimization provides: |
| 29 | + |
| 30 | +- **10K records**: 90% memory reduction (100MB → 10MB) |
| 31 | +- **100K records**: 99% memory reduction (1GB → 10MB) |
| 32 | +- **1M records**: 99.9% memory reduction (10GB → 10MB) |
| 33 | + |
| 34 | +## Test File Location |
| 35 | + |
| 36 | +``` |
| 37 | +spec/concerns/apis/notification_api_performance_spec.rb |
| 38 | +``` |
| 39 | + |
| 40 | +## Running the Tests |
| 41 | + |
| 42 | +### Run all performance tests |
| 43 | + |
| 44 | +```bash |
| 45 | +bundle exec rspec spec/concerns/apis/notification_api_performance_spec.rb |
| 46 | +``` |
| 47 | + |
| 48 | +### Run with detailed output |
| 49 | + |
| 50 | +```bash |
| 51 | +bundle exec rspec spec/concerns/apis/notification_api_performance_spec.rb --format documentation |
| 52 | +``` |
| 53 | + |
| 54 | +### Run specific test contexts |
| 55 | + |
| 56 | +```bash |
| 57 | +# Test only the targets_empty? optimization |
| 58 | +bundle exec rspec spec/concerns/apis/notification_api_performance_spec.rb -e "targets_empty? optimization" |
| 59 | + |
| 60 | +# Test only batch processing optimization |
| 61 | +bundle exec rspec spec/concerns/apis/notification_api_performance_spec.rb -e "batch processing optimization" |
| 62 | + |
| 63 | +# Test memory efficiency comparison |
| 64 | +bundle exec rspec spec/concerns/apis/notification_api_performance_spec.rb -e "comparing optimized vs unoptimized" |
| 65 | +``` |
| 66 | + |
| 67 | +### Run as part of full test suite |
| 68 | + |
| 69 | +The performance tests are integrated into the main test suite via `notification_spec.rb`, so they run automatically with: |
| 70 | + |
| 71 | +```bash |
| 72 | +bundle exec rspec |
| 73 | +# or |
| 74 | +bundle exec rake |
| 75 | +``` |
| 76 | + |
| 77 | +## Test Coverage |
| 78 | + |
| 79 | +### 1. Empty Check Optimization Tests |
| 80 | + |
| 81 | +- **Validates `exists?` is used instead of `blank?`**: Ensures the optimized path is taken for ActiveRecord relations |
| 82 | +- **Measures query efficiency**: Confirms at most 1 SELECT query is executed for empty check |
| 83 | +- **Tests empty collection handling**: Verifies correct behavior with empty target collections |
| 84 | + |
| 85 | +### 2. Batch Processing Tests |
| 86 | + |
| 87 | +#### Small Collections (< 1000 records) |
| 88 | +- Tests with 50 records |
| 89 | +- Validates `find_each` is called for ActiveRecord relations |
| 90 | +- Ensures records are not loaded all at once via `to_a` or `load` |
| 91 | +- Confirms all notifications are created successfully |
| 92 | + |
| 93 | +#### Medium Collections (1000+ records) |
| 94 | +- Tests with 1000 records |
| 95 | +- Validates batch processing with default batch size |
| 96 | +- Tests custom `batch_size` option |
| 97 | +- **Memory efficiency test**: Measures memory consumption during processing |
| 98 | +- **Query efficiency test**: Tracks SELECT queries to validate batching |
| 99 | + |
| 100 | +#### Array Inputs |
| 101 | +- Tests fallback behavior for arrays (already in memory) |
| 102 | +- Validates `map` is used appropriately for arrays |
| 103 | + |
| 104 | +### 3. Performance Comparison Tests |
| 105 | + |
| 106 | +- **Memory usage comparison**: Compares loading all records vs batch processing |
| 107 | +- **Performance metrics**: Measures processing time, throughput, and average time per notification |
| 108 | +- **Provides quantifiable evidence** with detailed output: |
| 109 | + ``` |
| 110 | + === Memory Usage Comparison (500 records) === |
| 111 | + Loading all records: 12.34MB |
| 112 | + Batch processing: 8.76MB |
| 113 | + Difference: 3.58MB |
| 114 | + |
| 115 | + === Performance Metrics (500 records) === |
| 116 | + Total notifications created: 500 |
| 117 | + Processing time: 1234.56ms |
| 118 | + Average time per notification: 2.469ms |
| 119 | + Throughput: 405.12 notifications/second |
| 120 | + ``` |
| 121 | + |
| 122 | +### 4. Integration Tests |
| 123 | + |
| 124 | +- Tests `notify` method with large target collections (200 records) |
| 125 | +- Validates complete workflow from empty check through batch processing |
| 126 | +- Confirms all notifications are created correctly |
| 127 | + |
| 128 | +### 5. Regression Tests |
| 129 | + |
| 130 | +- Ensures backward compatibility with existing functionality |
| 131 | +- Tests both `notify_all` with arrays and relations |
| 132 | +- Validates notification content is correct |
| 133 | + |
| 134 | +## Performance Test Design Principles |
| 135 | + |
| 136 | +### Memory Efficiency Testing |
| 137 | + |
| 138 | +The tests measure memory consumption using system-level RSS (Resident Set Size): |
| 139 | + |
| 140 | +```ruby |
| 141 | +memory_before = `ps -o rss= -p #{Process.pid}`.to_i |
| 142 | +# ... perform operation ... |
| 143 | +memory_after = `ps -o rss= -p #{Process.pid}`.to_i |
| 144 | +memory_increase_mb = (memory_after - memory_before) / 1024.0 |
| 145 | +``` |
| 146 | + |
| 147 | +### Query Efficiency Testing |
| 148 | + |
| 149 | +The tests track SQL queries using ActiveSupport::Notifications: |
| 150 | + |
| 151 | +```ruby |
| 152 | +ActiveSupport::Notifications.subscribe("sql.active_record") do |*args| |
| 153 | + event = ActiveSupport::Notifications::Event.new(*args) |
| 154 | + # Count and analyze queries |
| 155 | +end |
| 156 | +``` |
| 157 | + |
| 158 | +### Behavioral Verification |
| 159 | + |
| 160 | +The tests use RSpec mocking to verify specific methods are called: |
| 161 | + |
| 162 | +```ruby |
| 163 | +expect(relation).to receive(:find_each).and_call_original |
| 164 | +expect(relation).not_to receive(:to_a) |
| 165 | +``` |
| 166 | + |
| 167 | +## Interpreting Test Results |
| 168 | + |
| 169 | +### Success Indicators |
| 170 | + |
| 171 | +1. **Memory tests pass**: Memory consumption stays below thresholds |
| 172 | +2. **Query efficiency verified**: Batch processing uses minimal queries |
| 173 | +3. **Method calls validated**: Correct optimization paths are taken |
| 174 | +4. **Metrics show improvement**: Performance numbers demonstrate efficiency gains |
| 175 | + |
| 176 | +### What to Look For |
| 177 | + |
| 178 | +- **Memory increase < 50MB for 1000 records**: Indicates effective batch processing |
| 179 | +- **Query count < 100 for large collections**: Shows queries are batched, not N+1 |
| 180 | +- **`find_each` is called**: Confirms ActiveRecord batch processing is active |
| 181 | +- **`exists?` is used for empty check**: Validates optimization is applied |
| 182 | + |
| 183 | +### Performance Metrics Output |
| 184 | + |
| 185 | +When tests run, you'll see detailed performance metrics in the console: |
| 186 | + |
| 187 | +``` |
| 188 | +=== Memory Usage Comparison (500 records) === |
| 189 | +Loading all records: XX.XXmb |
| 190 | +Batch processing: XX.XXmb |
| 191 | +Difference: XX.XXmb |
| 192 | +
|
| 193 | +=== Performance Metrics (500 records) === |
| 194 | +Total notifications created: 500 |
| 195 | +Processing time: XXXXms |
| 196 | +Average time per notification: X.XXXms |
| 197 | +Throughput: XXX.XX notifications/second |
| 198 | +``` |
| 199 | + |
| 200 | +These metrics provide quantifiable evidence that the optimization is working. |
| 201 | + |
| 202 | +## Test Data Management |
| 203 | + |
| 204 | +### Setup |
| 205 | +- Tests create users dynamically using FactoryBot |
| 206 | +- Large collections are created in batches to avoid memory issues during setup |
| 207 | + |
| 208 | +### Cleanup |
| 209 | +- Tests clean up created records in `after` blocks |
| 210 | +- Uses `delete_all` for efficient cleanup |
| 211 | +- Cleans both User records and Notification records |
| 212 | + |
| 213 | +## Troubleshooting |
| 214 | + |
| 215 | +### Tests are slow |
| 216 | +- This is expected for tests with 1000+ records |
| 217 | +- Performance tests intentionally use larger datasets to demonstrate improvements |
| 218 | +- Consider using `--tag ~slow` to skip performance tests in rapid development cycles |
| 219 | + |
| 220 | +### Memory tests fail |
| 221 | +- Check if other processes are consuming memory |
| 222 | +- Ensure adequate system memory is available |
| 223 | +- GC timing can affect results; tests include `GC.start` to minimize variance |
| 224 | + |
| 225 | +### Query count varies |
| 226 | +- Query counts can vary slightly based on database adapter and Rails version |
| 227 | +- Tests use reasonable thresholds rather than exact counts |
| 228 | +- Check if eager loading or includes are adding queries |
| 229 | + |
| 230 | +## Future Enhancements |
| 231 | + |
| 232 | +Potential improvements to the test suite: |
| 233 | + |
| 234 | +1. **Benchmark against different ORMs**: Add Mongoid and Dynamoid specific tests |
| 235 | +2. **Test with Rails version matrix**: Validate across Rails 5.0-8.0 |
| 236 | +3. **Database adapter comparison**: Test PostgreSQL vs MySQL vs SQLite behavior |
| 237 | +4. **Profiling integration**: Add memory_profiler or benchmark-ips gems |
| 238 | +5. **CI integration**: Add performance regression checks in CI pipeline |
| 239 | + |
| 240 | +## Related Documentation |
| 241 | + |
| 242 | +- Main test suite: `spec/concerns/apis/notification_api_spec.rb` |
| 243 | +- Model tests: `spec/models/notification_spec.rb` |
| 244 | +- Testing guide: `docs/Testing.md` |
| 245 | +- Implementation: `lib/activity_notification/apis/notification_api.rb` |
| 246 | + |
| 247 | +## Credits |
| 248 | + |
| 249 | +These tests address the performance validation request in GutHub issue #148, providing quantifiable evidence that the batch processing optimization delivers the promised memory efficiency improvements. |
0 commit comments