From ac20f36eae73249cb503ba27e6c2e73a99a51eed Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Sun, 25 Sep 2022 18:01:40 -0700 Subject: [PATCH] warmup --- BitFaster.Caching.ThroughputAnalysis/ConfigFactory.cs | 10 +++++++++- .../ThroughputBenchmark.cs | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/BitFaster.Caching.ThroughputAnalysis/ConfigFactory.cs b/BitFaster.Caching.ThroughputAnalysis/ConfigFactory.cs index 4feaf93f..76a0897b 100644 --- a/BitFaster.Caching.ThroughputAnalysis/ConfigFactory.cs +++ b/BitFaster.Caching.ThroughputAnalysis/ConfigFactory.cs @@ -27,7 +27,7 @@ public static (ThroughputBenchmarkBase, IThroughputBenchConfig, int) Create(Mode case Mode.Update: return (new UpdateThroughputBenchmark(), new ZipfConfig(iterations, samples, s, n), cacheSize); case Mode.Evict: - return (new ReadThroughputBenchmark(), new EvictionConfig(iterations, samples, maxThreads), cacheSize); + return (new ReadThroughputBenchmark() { Initialize = c => EvictionInit(c) }, new EvictionConfig(iterations, samples, maxThreads), cacheSize); } throw new InvalidOperationException(); @@ -50,5 +50,13 @@ public static (ThroughputBenchmarkBase, IThroughputBenchConfig, int) Create(Mode < 5_000_000 => cacheSize * 2, _ => cacheSize }; + + private static void EvictionInit(ICache cache) + { + Parallel.ForEach(Enumerable.Range(0, cache.Policy.Eviction.Value.Capacity).Select(i => -i), i => + { + cache.GetOrAdd(i, key => i); + }); + } } } diff --git a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs index 99574557..a7b24e8f 100644 --- a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs +++ b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs @@ -13,10 +13,14 @@ public interface IThroughputBenchmark public abstract class ThroughputBenchmarkBase { + public Action> Initialize { get; set; } + public double Run(int warmup, int runs, int threads, IThroughputBenchConfig config, ICache cache) { double[] results = new double[warmup + runs]; + Initialize?.Invoke(cache); + for (int i = 0; i < warmup + runs; i++) { results[i] = Run(threads, config, cache);