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

Function test_result_cache_for_identical_queries

tests/result_cache_test.rs:5–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3
4#[tokio::test]
5async fn test_result_cache_for_identical_queries() {
6 let test_server = setup_test_server_with_init(|db| {
7 Box::pin(async move {
8 // Create a test table
9 db.execute("CREATE TABLE cache_test (id INTEGER PRIMARY KEY, name TEXT, value REAL)").await?;
10
11 // Insert test data
12 for i in 1..=100 {
13 let query = format!("INSERT INTO cache_test VALUES ({}, 'name_{}', {})", i, i, i as f64 * 1.5);
14 db.execute(&query).await?;
15 }
16
17 Ok(())
18 })
19 }).await;
20
21 let client = &test_server.client;
22
23 // Execute a query that should be cached (takes more than 1ms or returns > 10 rows)
24 let query = "SELECT id, name, value FROM cache_test WHERE id > 90 ORDER BY id";
25
26 // First execution - should be slower
27 let start1 = std::time::Instant::now();
28 let rows1 = client.query(query, &[]).await.unwrap();
29 let duration1 = start1.elapsed();
30
31 // Verify results
32 assert_eq!(rows1.len(), 10);
33 assert_eq!(rows1[0].get::<_, i32>(0), 91);
34 assert_eq!(rows1[9].get::<_, i32>(0), 100);
35
36 // Second execution - should be cached and faster
37 let start2 = std::time::Instant::now();
38 let rows2 = client.query(query, &[]).await.unwrap();
39 let duration2 = start2.elapsed();
40
41 // Verify same results
42 assert_eq!(rows2.len(), 10);
43 assert_eq!(rows2[0].get::<_, i32>(0), 91);
44 assert_eq!(rows2[9].get::<_, i32>(0), 100);
45
46 // Third execution to ensure cache is working
47 let start3 = std::time::Instant::now();
48 let rows3 = client.query(query, &[]).await.unwrap();
49 let duration3 = start3.elapsed();
50
51 assert_eq!(rows3.len(), 10);
52
53 // Log durations for debugging
54 eprintln!("First execution: {duration1:?}");
55 eprintln!("Second execution (cached): {duration2:?}");
56 eprintln!("Third execution (cached): {duration3:?}");
57
58 // With the test harness overhead, we can't reliably test timing
59 // Just verify that subsequent executions return the same results
60 // In production, the cache would provide significant benefits
61
62 test_server.abort();

Callers

nothing calls this directly

Calls 4

abortMethod · 0.80
executeMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected