MCPcopy Create free account
hub / github.com/erans/pgsqlite / test_connection_handling_overhead

Function test_connection_handling_overhead

tests/protocol_overhead_analysis.rs:129–208  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

127
128#[tokio::test]
129async fn test_connection_handling_overhead() {
130 println!("\n=== CONNECTION HANDLING OVERHEAD ===\n");
131
132 // Test connection creation overhead
133 let iterations = 10;
134
135 let start = Instant::now();
136 for _ in 0..iterations {
137 let _db = DbHandler::new(":memory:").expect("Failed to create database");
138 }
139 let create_time = start.elapsed() / iterations as u32;
140 println!("Database creation overhead: {create_time:?}");
141
142 // Test mutex contention with concurrent access
143 // Use a temporary file instead of in-memory database for shared access
144 let test_id2 = Uuid::new_v4().to_string().replace("-", "");
145 let db_path2 = format!("/tmp/pgsqlite_test_concurrent_{test_id2}.db");
146 let db = std::sync::Arc::new(DbHandler::new(&db_path2).expect("Failed to create database"));
147
148 // Create session for single-threaded test
149 let session_id = Uuid::new_v4();
150 db.create_session_connection(session_id).await.expect("Failed to create session connection");
151
152 db.execute_with_session("CREATE TABLE concurrent_test (id INTEGER PRIMARY KEY, value INTEGER)", &session_id)
153 .await
154 .expect("Failed to create table");
155
156 println!("\nConcurrent access test:");
157
158 // Single-threaded baseline
159 let start = Instant::now();
160 for i in 0..100 {
161 db.execute_with_session(&format!("INSERT INTO concurrent_test (value) VALUES ({i})"), &session_id)
162 .await
163 .expect("Failed to execute");
164 }
165 let single_time = start.elapsed();
166
167 // Multi-threaded test
168 let start = Instant::now();
169 let mut handles = vec![];
170
171 for i in 0..10 {
172 let db = Arc::clone(&db);
173 let handle = tokio::spawn(async move {
174 // Each thread needs its own session
175 let thread_session_id = Uuid::new_v4();
176 db.create_session_connection(thread_session_id).await.expect("Failed to create session connection");
177
178 for j in 0..10 {
179 db.execute_with_session(&format!("INSERT INTO concurrent_test (value) VALUES ({})", i * 10 + j), &thread_session_id)
180 .await
181 .expect("Failed to execute");
182 }
183
184 // Clean up session
185 db.remove_session_connection(&thread_session_id);
186 });

Callers

nothing calls this directly

Calls 3

execute_with_sessionMethod · 0.45

Tested by

no test coverage detected