|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId}; |
| 2 | +use rustocache::{RustoCache, CacheProvider, GetOrSetOptions}; |
| 3 | +use rustocache::drivers::MemoryDriverBuilder; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::time::Duration; |
| 6 | +use tokio::runtime::Runtime; |
| 7 | + |
| 8 | +#[derive(Clone, Debug)] |
| 9 | +struct BenchData { |
| 10 | + value: String, |
| 11 | +} |
| 12 | + |
| 13 | +fn grace_period_benchmarks(c: &mut Criterion) { |
| 14 | + let rt = Runtime::new().unwrap(); |
| 15 | + |
| 16 | + // Setup cache |
| 17 | + let memory_driver = Arc::new( |
| 18 | + MemoryDriverBuilder::new() |
| 19 | + .max_entries(10000) |
| 20 | + .serialize(false) |
| 21 | + .build::<BenchData>() |
| 22 | + ); |
| 23 | + |
| 24 | + let cache = RustoCache::new( |
| 25 | + rustocache::CacheStackBuilder::new("grace_bench") |
| 26 | + .with_l1_driver(memory_driver) |
| 27 | + .build() |
| 28 | + ); |
| 29 | + |
| 30 | + let mut group = c.benchmark_group("grace_period_performance"); |
| 31 | + |
| 32 | + // Benchmark: Normal operation (no grace period) |
| 33 | + group.bench_function("normal_get_or_set", |b| { |
| 34 | + b.to_async(&rt).iter(|| async { |
| 35 | + cache.get_or_set( |
| 36 | + "bench_key_normal", |
| 37 | + || async { Ok(BenchData { value: "test_value".to_string() }) }, |
| 38 | + GetOrSetOptions { |
| 39 | + ttl: Some(Duration::from_secs(60)), |
| 40 | + grace_period: None, |
| 41 | + ..Default::default() |
| 42 | + }, |
| 43 | + ).await.unwrap() |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + // Benchmark: With grace period (should have minimal overhead) |
| 48 | + group.bench_function("with_grace_period", |b| { |
| 49 | + b.to_async(&rt).iter(|| async { |
| 50 | + cache.get_or_set( |
| 51 | + "bench_key_grace", |
| 52 | + || async { Ok(BenchData { value: "test_value".to_string() }) }, |
| 53 | + GetOrSetOptions { |
| 54 | + ttl: Some(Duration::from_secs(60)), |
| 55 | + grace_period: Some(Duration::from_secs(30)), |
| 56 | + ..Default::default() |
| 57 | + }, |
| 58 | + ).await.unwrap() |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + // Benchmark: Grace period serving stale data |
| 63 | + group.bench_function("grace_period_stale_data", |b| { |
| 64 | + // Pre-populate with expired data |
| 65 | + rt.block_on(async { |
| 66 | + cache.get_or_set( |
| 67 | + "bench_key_stale", |
| 68 | + || async { Ok(BenchData { value: "stale_value".to_string() }) }, |
| 69 | + GetOrSetOptions { |
| 70 | + ttl: Some(Duration::from_millis(1)), // Very short TTL |
| 71 | + grace_period: Some(Duration::from_secs(60)), |
| 72 | + ..Default::default() |
| 73 | + }, |
| 74 | + ).await.unwrap(); |
| 75 | + |
| 76 | + // Wait for TTL to expire |
| 77 | + tokio::time::sleep(Duration::from_millis(10)).await; |
| 78 | + }); |
| 79 | + |
| 80 | + b.to_async(&rt).iter(|| async { |
| 81 | + cache.get_or_set( |
| 82 | + "bench_key_stale", |
| 83 | + || async { |
| 84 | + // Simulate factory failure |
| 85 | + Err(rustocache::CacheError::Generic { |
| 86 | + message: "Factory failed".to_string() |
| 87 | + }) |
| 88 | + }, |
| 89 | + GetOrSetOptions { |
| 90 | + ttl: Some(Duration::from_millis(1)), |
| 91 | + grace_period: Some(Duration::from_secs(60)), |
| 92 | + ..Default::default() |
| 93 | + }, |
| 94 | + ).await.unwrap() // Should succeed with stale data |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + group.finish(); |
| 99 | + |
| 100 | + // Benchmark different grace period durations |
| 101 | + let mut group = c.benchmark_group("grace_period_durations"); |
| 102 | + |
| 103 | + let grace_periods = vec![ |
| 104 | + Duration::from_secs(1), |
| 105 | + Duration::from_secs(10), |
| 106 | + Duration::from_secs(60), |
| 107 | + Duration::from_secs(300), |
| 108 | + Duration::from_secs(3600), |
| 109 | + ]; |
| 110 | + |
| 111 | + for grace_period in grace_periods { |
| 112 | + group.bench_with_input( |
| 113 | + BenchmarkId::new("grace_duration", format!("{}s", grace_period.as_secs())), |
| 114 | + &grace_period, |
| 115 | + |b, &grace_period| { |
| 116 | + b.to_async(&rt).iter(|| async { |
| 117 | + cache.get_or_set( |
| 118 | + &format!("bench_key_duration_{}", grace_period.as_secs()), |
| 119 | + || async { Ok(BenchData { value: "test_value".to_string() }) }, |
| 120 | + GetOrSetOptions { |
| 121 | + ttl: Some(Duration::from_secs(60)), |
| 122 | + grace_period: Some(grace_period), |
| 123 | + ..Default::default() |
| 124 | + }, |
| 125 | + ).await.unwrap() |
| 126 | + }); |
| 127 | + }, |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + group.finish(); |
| 132 | + |
| 133 | + // Benchmark concurrent grace period operations |
| 134 | + let mut group = c.benchmark_group("grace_period_concurrency"); |
| 135 | + |
| 136 | + group.bench_function("concurrent_grace_period_ops", |b| { |
| 137 | + b.to_async(&rt).iter(|| async { |
| 138 | + let futures = (0..100).map(|i| { |
| 139 | + cache.get_or_set( |
| 140 | + &format!("concurrent_key_{}", i), |
| 141 | + || async { Ok(BenchData { value: format!("value_{}", i) }) }, |
| 142 | + GetOrSetOptions { |
| 143 | + ttl: Some(Duration::from_secs(60)), |
| 144 | + grace_period: Some(Duration::from_secs(30)), |
| 145 | + ..Default::default() |
| 146 | + }, |
| 147 | + ) |
| 148 | + }); |
| 149 | + |
| 150 | + futures::future::join_all(futures).await; |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + group.finish(); |
| 155 | +} |
| 156 | + |
| 157 | +criterion_group!(benches, grace_period_benchmarks); |
| 158 | +criterion_main!(benches); |
0 commit comments