From 296274737887070e4cc11b2d0e0965bcbe103164 Mon Sep 17 00:00:00 2001 From: Croxx Date: Thu, 21 Mar 2024 16:16:37 +0800 Subject: [PATCH] perf(storage): add foyer memory cache to block cache bench (#15831) Signed-off-by: MrCroxx --- Cargo.lock | 6 +- src/storage/Cargo.toml | 2 +- ...ench_lru_cache.rs => bench_block_cache.rs} | 77 ++++++++++++++++++- 3 files changed, 80 insertions(+), 5 deletions(-) rename src/storage/benches/{bench_lru_cache.rs => bench_block_cache.rs} (71%) diff --git a/Cargo.lock b/Cargo.lock index 4584f80a54ad..6c9c3004b3a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4189,9 +4189,9 @@ dependencies = [ [[package]] name = "foyer-memory" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65b0a4ca5d917e684c9267e8e2bcec4b3e0c27dd91c84945c20c09ddff906cac" +checksum = "b5c17111344df2de260140472855e2d84e0e8e3bffd4faed840f12f6de6f23da" dependencies = [ "ahash 0.8.6", "bitflags 2.4.2", @@ -8243,7 +8243,7 @@ dependencies = [ "indoc", "libc", "memoffset", - "parking_lot 0.12.1", + "parking_lot 0.11.2", "portable-atomic", "pyo3-build-config", "pyo3-ffi", diff --git a/src/storage/Cargo.toml b/src/storage/Cargo.toml index fb65be5796d5..331e7b098ead 100644 --- a/src/storage/Cargo.toml +++ b/src/storage/Cargo.toml @@ -113,7 +113,7 @@ harness = false # debug = true [[bench]] -name = "bench_lru_cache" +name = "bench_block_cache" harness = false [[bench]] diff --git a/src/storage/benches/bench_lru_cache.rs b/src/storage/benches/bench_block_cache.rs similarity index 71% rename from src/storage/benches/bench_lru_cache.rs rename to src/storage/benches/bench_block_cache.rs index 43c4193367b8..497d532732da 100644 --- a/src/storage/benches/bench_lru_cache.rs +++ b/src/storage/benches/bench_block_cache.rs @@ -21,6 +21,7 @@ use std::time::{Duration, Instant}; use async_trait::async_trait; use bytes::{BufMut, Bytes, BytesMut}; use criterion::{criterion_group, criterion_main, Criterion}; +use foyer::memory as foyer; use moka::future::Cache; use rand::rngs::SmallRng; use rand::{RngCore, SeedableRng}; @@ -88,7 +89,7 @@ pub struct LruCacheImpl { impl LruCacheImpl { pub fn new(capacity: usize, fake_io_latency: Duration) -> Self { Self { - inner: Arc::new(LruCache::new(3, capacity, 0)), + inner: Arc::new(LruCache::new(8, capacity, 0)), fake_io_latency, } } @@ -115,6 +116,68 @@ impl CacheBase for LruCacheImpl { } } +pub struct FoyerCache { + inner: foyer::Cache<(u64, u64), Arc>, + fake_io_latency: Duration, +} + +impl FoyerCache { + pub fn lru(capacity: usize, fake_io_latency: Duration) -> Self { + let inner = foyer::Cache::lru(foyer::LruCacheConfig { + capacity, + shards: 8, + eviction_config: foyer::LruConfig { + high_priority_pool_ratio: 0.0, + }, + object_pool_capacity: 8 * 1024, + hash_builder: ahash::RandomState::default(), + event_listener: foyer::DefaultCacheEventListener::default(), + }); + Self { + inner, + fake_io_latency, + } + } + + pub fn lfu(capacity: usize, fake_io_latency: Duration) -> Self { + let inner = foyer::Cache::lfu(foyer::LfuCacheConfig { + capacity, + shards: 8, + eviction_config: foyer::LfuConfig { + window_capacity_ratio: 0.1, + protected_capacity_ratio: 0.8, + cmsketch_eps: 0.001, + cmsketch_confidence: 0.9, + }, + object_pool_capacity: 8 * 1024, + hash_builder: ahash::RandomState::default(), + event_listener: foyer::DefaultCacheEventListener::default(), + }); + Self { + inner, + fake_io_latency, + } + } +} + +#[async_trait] +impl CacheBase for FoyerCache { + async fn try_get_with(&self, sst_object_id: u64, block_idx: u64) -> HummockResult> { + let entry = self + .inner + .entry((sst_object_id, block_idx), || { + let latency = self.fake_io_latency; + async move { + get_fake_block(sst_object_id, block_idx, latency) + .await + .map(|block| (Arc::new(block), 1, foyer::CacheContext::Default)) + } + }) + .await?; + Ok(entry.value().clone()) + } +} + static IO_COUNT: AtomicUsize = AtomicUsize::new(0); async fn get_fake_block(sst: u64, offset: u64, io_latency: Duration) -> HummockResult { @@ -193,16 +256,28 @@ fn bench_block_cache(c: &mut Criterion) { bench_cache(block_cache, c, 10000); let block_cache = Arc::new(LruCacheImpl::new(2048, Duration::from_millis(0))); bench_cache(block_cache, c, 10000); + let block_cache = Arc::new(FoyerCache::lru(2048, Duration::from_millis(0))); + bench_cache(block_cache, c, 10000); + let block_cache = Arc::new(FoyerCache::lfu(2048, Duration::from_millis(0))); + bench_cache(block_cache, c, 10000); let block_cache = Arc::new(MokaCache::new(2048, Duration::from_millis(1))); bench_cache(block_cache, c, 1000); let block_cache = Arc::new(LruCacheImpl::new(2048, Duration::from_millis(1))); bench_cache(block_cache, c, 1000); + let block_cache = Arc::new(FoyerCache::lru(2048, Duration::from_millis(1))); + bench_cache(block_cache, c, 1000); + let block_cache = Arc::new(FoyerCache::lfu(2048, Duration::from_millis(1))); + bench_cache(block_cache, c, 1000); let block_cache = Arc::new(MokaCache::new(256, Duration::from_millis(10))); bench_cache(block_cache, c, 200); let block_cache = Arc::new(LruCacheImpl::new(256, Duration::from_millis(10))); bench_cache(block_cache, c, 200); + let block_cache = Arc::new(FoyerCache::lru(256, Duration::from_millis(10))); + bench_cache(block_cache, c, 200); + let block_cache = Arc::new(FoyerCache::lfu(256, Duration::from_millis(10))); + bench_cache(block_cache, c, 200); } criterion_group!(benches, bench_block_cache);