| 616 | |
| 617 | #[test] |
| 618 | fn test_cache_performance_benefit() { |
| 619 | let temp_dir = tempfile::tempdir().unwrap(); |
| 620 | let table = open_or_create::<TestTable>(temp_dir.path()).unwrap(); |
| 621 | |
| 622 | // Insert test data |
| 623 | let key = TestKey(999); |
| 624 | let value = TestValue { |
| 625 | data: "performance_test_value".to_string(), |
| 626 | }; |
| 627 | table.insert(key, value.clone()); |
| 628 | |
| 629 | // First get - loads from RocksDB and caches |
| 630 | let start = std::time::Instant::now(); |
| 631 | let result1 = table.get(key).unwrap(); |
| 632 | let first_get_time = start.elapsed(); |
| 633 | assert_eq!(result1.read().data, value.data); |
| 634 | |
| 635 | // Verify item is now in cache |
| 636 | let cache = table.cache().lock().unwrap(); |
| 637 | assert_eq!(cache.len(), 1); |
| 638 | drop(cache); |
| 639 | |
| 640 | // Second get - should be faster (from cache) |
| 641 | let start = std::time::Instant::now(); |
| 642 | let result2 = table.get(key).unwrap(); |
| 643 | let second_get_time = start.elapsed(); |
| 644 | assert_eq!(result2.read().data, value.data); |
| 645 | |
| 646 | // Cache should still have 1 item |
| 647 | let cache = table.cache().lock().unwrap(); |
| 648 | assert_eq!(cache.len(), 1); |
| 649 | drop(cache); |
| 650 | |
| 651 | println!("First get (RocksDB): {:?}", first_get_time); |
| 652 | println!("Second get (cache): {:?}", second_get_time); |
| 653 | |
| 654 | // While we can't guarantee cache is always faster in a test environment, |
| 655 | // we can at least verify that the cache is being used correctly |
| 656 | assert!(second_get_time.as_nanos() > 0); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn test_cache_basic_usage() { |