|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Stress tests for VeriSimDB Phase 3.2. |
| 5 | +// Concurrent writers and readers hitting the octad store simultaneously. |
| 6 | + |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use verisim_octad::{ |
| 11 | + InMemoryOctadStore, OctadConfig, OctadDocumentInput, OctadId, |
| 12 | + OctadInput, OctadSnapshot, OctadStore, |
| 13 | +}; |
| 14 | +use verisim_document::TantivyDocumentStore; |
| 15 | +use verisim_graph::SimpleGraphStore; |
| 16 | +use verisim_semantic::InMemorySemanticStore; |
| 17 | +use verisim_temporal::InMemoryVersionStore; |
| 18 | +use verisim_tensor::InMemoryTensorStore; |
| 19 | +use verisim_vector::{BruteForceVectorStore, DistanceMetric}; |
| 20 | + |
| 21 | +type TestStore = InMemoryOctadStore< |
| 22 | + SimpleGraphStore, |
| 23 | + BruteForceVectorStore, |
| 24 | + TantivyDocumentStore, |
| 25 | + InMemoryTensorStore, |
| 26 | + InMemorySemanticStore, |
| 27 | + InMemoryVersionStore<OctadSnapshot>, |
| 28 | + verisim_provenance::InMemoryProvenanceStore, |
| 29 | + verisim_spatial::InMemorySpatialStore, |
| 30 | +>; |
| 31 | + |
| 32 | +fn create_store() -> Arc<TestStore> { |
| 33 | + Arc::new(InMemoryOctadStore::new( |
| 34 | + OctadConfig::default(), |
| 35 | + Arc::new(SimpleGraphStore::new()), |
| 36 | + Arc::new(BruteForceVectorStore::new(3, DistanceMetric::Cosine)), |
| 37 | + Arc::new(TantivyDocumentStore::in_memory().unwrap()), |
| 38 | + Arc::new(InMemoryTensorStore::new()), |
| 39 | + Arc::new(InMemorySemanticStore::new()), |
| 40 | + Arc::new(InMemoryVersionStore::new()), |
| 41 | + Arc::new(verisim_provenance::InMemoryProvenanceStore::new()), |
| 42 | + Arc::new(verisim_spatial::InMemorySpatialStore::new()), |
| 43 | + )) |
| 44 | +} |
| 45 | + |
| 46 | +fn doc(title: &str, body: &str) -> OctadInput { |
| 47 | + OctadInput { |
| 48 | + document: Some(OctadDocumentInput { |
| 49 | + title: title.into(), |
| 50 | + body: body.into(), |
| 51 | + fields: HashMap::new(), |
| 52 | + }), |
| 53 | + ..Default::default() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// 50 concurrent writers, each creating 10 entities. |
| 58 | +#[tokio::test] |
| 59 | +async fn concurrent_writers() { |
| 60 | + let store = create_store(); |
| 61 | + let mut handles = Vec::new(); |
| 62 | + |
| 63 | + for writer_id in 0..50 { |
| 64 | + let store = store.clone(); |
| 65 | + handles.push(tokio::spawn(async move { |
| 66 | + let mut ids = Vec::new(); |
| 67 | + for i in 0..10 { |
| 68 | + let input = doc( |
| 69 | + &format!("W{writer_id}-E{i}"), |
| 70 | + &format!("Body from writer {writer_id} entity {i}"), |
| 71 | + ); |
| 72 | + match store.create(input).await { |
| 73 | + Ok(octad) => ids.push(octad.id), |
| 74 | + Err(e) => panic!("Writer {writer_id} entity {i} failed: {e}"), |
| 75 | + } |
| 76 | + } |
| 77 | + ids |
| 78 | + })); |
| 79 | + } |
| 80 | + |
| 81 | + let mut all_ids = Vec::new(); |
| 82 | + for handle in handles { |
| 83 | + let ids = handle.await.unwrap(); |
| 84 | + all_ids.extend(ids); |
| 85 | + } |
| 86 | + |
| 87 | + assert_eq!(all_ids.len(), 500, "All 500 entities should be created"); |
| 88 | + |
| 89 | + // Verify all entities exist |
| 90 | + for id in &all_ids { |
| 91 | + assert!(store.get(id).await.unwrap().is_some(), "{id} missing"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// 20 writers + 20 readers running concurrently. |
| 96 | +#[tokio::test] |
| 97 | +async fn concurrent_read_write() { |
| 98 | + let store = create_store(); |
| 99 | + let mut handles = Vec::new(); |
| 100 | + |
| 101 | + // Seed 100 entities first |
| 102 | + let mut seed_ids = Vec::new(); |
| 103 | + for i in 0..100 { |
| 104 | + let octad = store.create(doc(&format!("Seed-{i}"), &format!("Seed body {i}"))).await.unwrap(); |
| 105 | + seed_ids.push(octad.id); |
| 106 | + } |
| 107 | + |
| 108 | + // 20 writers creating new entities |
| 109 | + for writer_id in 0..20 { |
| 110 | + let store = store.clone(); |
| 111 | + handles.push(tokio::spawn(async move { |
| 112 | + for i in 0..10 { |
| 113 | + store.create(doc( |
| 114 | + &format!("Concurrent-W{writer_id}-{i}"), |
| 115 | + &format!("Body {writer_id}-{i}"), |
| 116 | + )).await.unwrap(); |
| 117 | + } |
| 118 | + })); |
| 119 | + } |
| 120 | + |
| 121 | + // 20 readers reading seed entities |
| 122 | + for reader_id in 0..20 { |
| 123 | + let store = store.clone(); |
| 124 | + let ids = seed_ids.clone(); |
| 125 | + handles.push(tokio::spawn(async move { |
| 126 | + for id in &ids { |
| 127 | + let result = store.get(id).await; |
| 128 | + match result { |
| 129 | + Ok(Some(_)) => {} // Expected |
| 130 | + Ok(None) => {} // Acceptable during concurrent writes |
| 131 | + Err(e) => panic!("Reader {reader_id} error on {id}: {e}"), |
| 132 | + } |
| 133 | + } |
| 134 | + })); |
| 135 | + } |
| 136 | + |
| 137 | + for handle in handles { |
| 138 | + handle.await.unwrap(); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// Create then delete under contention. |
| 143 | +#[tokio::test] |
| 144 | +async fn concurrent_create_delete() { |
| 145 | + let store = create_store(); |
| 146 | + |
| 147 | + // Create 50 entities |
| 148 | + let mut ids = Vec::new(); |
| 149 | + for i in 0..50 { |
| 150 | + let octad = store.create(doc(&format!("CD-{i}"), "body")).await.unwrap(); |
| 151 | + ids.push(octad.id); |
| 152 | + } |
| 153 | + |
| 154 | + // Concurrently delete all of them |
| 155 | + let mut handles = Vec::new(); |
| 156 | + for id in ids.clone() { |
| 157 | + let store = store.clone(); |
| 158 | + handles.push(tokio::spawn(async move { |
| 159 | + store.delete(&id).await.unwrap(); |
| 160 | + })); |
| 161 | + } |
| 162 | + |
| 163 | + for handle in handles { |
| 164 | + handle.await.unwrap(); |
| 165 | + } |
| 166 | + |
| 167 | + // All should be gone |
| 168 | + for id in &ids { |
| 169 | + assert!(store.get(id).await.unwrap().is_none(), "{id} should be deleted"); |
| 170 | + } |
| 171 | +} |
0 commit comments