Skip to content

Commit 2896ed4

Browse files
authored
Add post deletion support to service layer with enhanced stress testing (#1033)
* Add `delete_by_entity_id` to PostRepository and optimize `delete_by_post_id` Improved post deletion in the repository layer with better performance and support for entity-based deletion. Changes: - Add `delete_by_entity_id` method that accepts an `EntityId` parameter - Optimize `delete_by_post_id` to remove wasteful full post SELECT query - `delete_by_entity_id` performs lightweight SELECT for just the post ID, then delegates to `delete_by_post_id` - All deletion logic now centralized in `delete_by_post_id` - Add unit tests: `test_repository_delete_by_entity_id` and `test_delete_by_entity_id_deletes_terms` in main test module - Add constraint test: `test_delete_by_entity_id_non_existent_returns_zero` in posts_constraint_tests.rs - Both methods automatically clean up associated term relationships * Add delete methods to PostService with unit tests Expose post deletion functionality in the service layer for both entity-based and post ID-based deletion. Changes: - Add `delete_by_entity_id` method to `PostService` - Add `delete_by_post_id` method to `PostService` - Both methods return `u64` for uniffi compatibility (converts from repository `usize`) - Both methods automatically clean up associated term relationships via repository layer - Add comprehensive unit tests: `test_delete_by_entity_id`, `test_delete_by_post_id`, `test_delete_by_entity_id_non_existent_returns_zero`, and `test_delete_by_post_id_non_existent_returns_zero` - Tests follow consistent pattern: setup via repository/helpers, actions via service layer, verification via repository * Enhance stress test with delete/insert operations and refactor Enhanced `start_comprehensive_stress_test` to support random updates, deletes, and inserts with configurable operation weights. Refactored for better maintainability and eliminated unnecessary mutations. Changes: - Add `StressTestConfig` struct to reduce function parameters from 9 to 3 - Add `StressTestOperation` enum for type-safe operation selection (Update, Delete, Insert) - Add weighted random operation selection based on configurable weights - Extract helper functions: `stress_test_batch_update`, `stress_test_batch_delete`, `stress_test_batch_insert` - Refactor `create_test_post` to accept all parameters (title, slug, link, content) - Remove `create_temp_post` method in favor of standalone `create_test_post` - Eliminate mutations when creating new posts (all parameters passed during construction) - Convert imperative for loops to functional style using `.iter().for_each()` - Optimize counter increments to batch-level instead of per-item * Add separate counters for inserts and deletes in stress test Track and display individual operation counts (updates, inserts, deletes) in the stress test UI for better observability. Changes: - Add `insert_counter` and `delete_counter` to `StressTestHandle` in Rust - Add `insert_count()` and `delete_count()` methods to expose counters - Update stress test to increment appropriate counter for each operation type - Add `totalInserts` and `totalDeletes` StateFlows in Kotlin ViewModel - Add coroutine to poll operation counters every 100ms from handle - Update UI to display: "Updates: X | Inserts: Y | Deletes: Z" - Add "Total Operations" line showing sum of all operations * make fmt-rust * Randomize post status during stress test updates Add status randomization to batch updates, cycling through Draft, Pending, Publish, and Future statuses to better simulate real-world database changes.
1 parent 84371fa commit 2896ed4

File tree

8 files changed

+555
-125
lines changed

8 files changed

+555
-125
lines changed

native/kotlin/example/composeApp/src/commonMain/kotlin/rs/wordpress/example/shared/ui/stresstest/StressTestScreen.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import rs.wordpress.example.shared.ui.components.PostCard
2929
fun StressTestScreen(viewModel: StressTestViewModel = koinInject()) {
3030
val posts by viewModel.posts.collectAsState()
3131
val totalUpdates by viewModel.totalUpdates.collectAsState()
32+
val totalInserts by viewModel.totalInserts.collectAsState()
33+
val totalDeletes by viewModel.totalDeletes.collectAsState()
3234
val isRunning by viewModel.isRunning.collectAsState()
3335
val performanceMetrics by viewModel.performanceMetrics.collectAsState()
3436
val listState = rememberLazyListState()
@@ -51,7 +53,8 @@ fun StressTestScreen(viewModel: StressTestViewModel = koinInject()) {
5153
)
5254
Spacer(modifier = Modifier.height(8.dp))
5355
Text(text = "Total Posts: ${posts.size}")
54-
Text(text = "Total Updates: $totalUpdates")
56+
Text(text = "Updates: $totalUpdates | Inserts: $totalInserts | Deletes: $totalDeletes")
57+
Text(text = "Total Operations: ${totalUpdates + totalInserts + totalDeletes}")
5558
Text(text = "Status: ${if (isRunning) "Running" else "Stopped"}")
5659

5760
performanceMetrics?.let { metrics ->

native/kotlin/example/composeApp/src/commonMain/kotlin/rs/wordpress/example/shared/ui/stresstest/StressTestViewModel.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class StressTestViewModel(
3939
private val _totalUpdates = MutableStateFlow(0L)
4040
val totalUpdates: StateFlow<Long> = _totalUpdates.asStateFlow()
4141

42+
private val _totalInserts = MutableStateFlow(0L)
43+
val totalInserts: StateFlow<Long> = _totalInserts.asStateFlow()
44+
45+
private val _totalDeletes = MutableStateFlow(0L)
46+
val totalDeletes: StateFlow<Long> = _totalDeletes.asStateFlow()
47+
4248
private val _isRunning = MutableStateFlow(false)
4349
val isRunning: StateFlow<Boolean> = _isRunning.asStateFlow()
4450

@@ -132,9 +138,6 @@ class StressTestViewModel(
132138

133139
// Update performance metrics
134140
updatePerformanceMetrics(loadDuration, totalLatency)
135-
136-
// Increment total updates counter
137-
_totalUpdates.value += 1
138141
}
139142
}
140143

@@ -145,14 +148,32 @@ class StressTestViewModel(
145148
// Start comprehensive stress test with:
146149
// - 10-100ms delay between batches (variable timing)
147150
// - 1-20 posts per batch (variable batch size)
151+
// - 50% updates, 25% deletes, 25% inserts (operation weights)
148152
stressTestHandle = mockPostService.startComprehensiveStressTest(
149153
entityIds,
150-
minDelayMs = 10u,
151-
maxDelayMs = 100u,
152-
minBatchSize = 1u,
153-
maxBatchSize = 20u
154+
uniffi.wp_mobile.StressTestConfig(
155+
minDelayMs = 10u,
156+
maxDelayMs = 100u,
157+
minBatchSize = 1u,
158+
maxBatchSize = 20u,
159+
updateWeight = 50u,
160+
deleteWeight = 25u,
161+
insertWeight = 25u
162+
)
154163
)
155-
println("Comprehensive stress test started with ObservableCollection!")
164+
println("Comprehensive stress test started with ObservableCollection (updates/deletes/inserts)!")
165+
166+
// Poll operation counters from the stress test handle
167+
viewModelScope.launch(Dispatchers.Default) {
168+
while (_isRunning.value) {
169+
stressTestHandle?.let { handle ->
170+
_totalUpdates.value = handle.updateCount().toLong()
171+
_totalInserts.value = handle.insertCount().toLong()
172+
_totalDeletes.value = handle.deleteCount().toLong()
173+
}
174+
kotlinx.coroutines.delay(100) // Poll every 100ms
175+
}
176+
}
156177
}
157178

158179
private fun updatePerformanceMetrics(loadDuration: Long, totalLatency: Long) {
@@ -203,6 +224,9 @@ class StressTestViewModel(
203224
}
204225

205226
fun onCleared() {
227+
// Stop the running flag to stop polling
228+
_isRunning.value = false
229+
206230
// Stop background updates
207231
stressTestHandle?.stop()
208232

native/swift/Example/Example/ExampleApp.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ struct ExampleApp: App {
4242

4343
self.handle = mockService.startComprehensiveStressTest(
4444
entityIds: ids,
45-
minDelayMs: 100,
46-
maxDelayMs: 2000,
47-
minBatchSize: 100,
48-
maxBatchSize: 1000
45+
config: StressTestConfig(
46+
minDelayMs: 100,
47+
maxDelayMs: 2000,
48+
minBatchSize: 100,
49+
maxBatchSize: 1000,
50+
updateWeight: 50,
51+
deleteWeight: 25,
52+
insertWeight: 25
53+
)
4954
)
5055
}
5156

native/swift/Tests/wordpress-api-cache/WordPressApiCacheTests.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,15 @@ actor Test {
6262
group.addTask {
6363
_ = mockService.startComprehensiveStressTest(
6464
entityIds: ids,
65-
minDelayMs: 1,
66-
maxDelayMs: 1000,
67-
minBatchSize: 100,
68-
maxBatchSize: 1000
65+
config: StressTestConfig(
66+
minDelayMs: 1,
67+
maxDelayMs: 1000,
68+
minBatchSize: 100,
69+
maxBatchSize: 1000,
70+
updateWeight: 50,
71+
deleteWeight: 25,
72+
insertWeight: 25
73+
)
6974
)
7075
}
7176
}

0 commit comments

Comments
 (0)