|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using BitFaster.Caching.Lru; |
| 6 | +using MathNet.Numerics.Distributions; |
| 7 | +using MathNet.Numerics.Random; |
| 8 | + |
| 9 | +namespace BitFaster.Caching.Benchmarks.Lru |
| 10 | +{ |
| 11 | + public class ZipDistribution |
| 12 | + { |
| 13 | + const double s = 0.86; |
| 14 | + const int n = 500; |
| 15 | + const int sampleCount = 1000; |
| 16 | + private static int[] samples; |
| 17 | + |
| 18 | + const int concurrencyLevel = 1; |
| 19 | + const int cacheSize = 50; // 10% cache size |
| 20 | + |
| 21 | + private static readonly ClassicLru<int, int> classicLru = new ClassicLru<int, int>(concurrencyLevel, cacheSize, EqualityComparer<int>.Default); |
| 22 | + private static readonly ConcurrentLru<int, int> concurrentLru = new ConcurrentLru<int, int>(concurrencyLevel, cacheSize, EqualityComparer<int>.Default); |
| 23 | + private static readonly ConcurrentTLru<int, int> concurrentTlru = new ConcurrentTLru<int, int>(concurrencyLevel, cacheSize, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10)); |
| 24 | + private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new FastConcurrentLru<int, int>(concurrencyLevel, cacheSize, EqualityComparer<int>.Default); |
| 25 | + private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new FastConcurrentTLru<int, int>(concurrencyLevel, cacheSize, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1)); |
| 26 | + |
| 27 | + [GlobalSetup] |
| 28 | + public void GlobalSetup() |
| 29 | + { |
| 30 | + samples = new int[sampleCount]; |
| 31 | + Zipf.Samples(samples, s, n); |
| 32 | + } |
| 33 | + |
| 34 | + [Benchmark(Baseline = true, OperationsPerInvoke = sampleCount)] |
| 35 | + public void ClassicLru() |
| 36 | + { |
| 37 | + Func<int, int> func = x => x; |
| 38 | + |
| 39 | + for (int i = 0; i < sampleCount; i++) |
| 40 | + { |
| 41 | + classicLru.GetOrAdd(samples[i], func); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + [Benchmark(OperationsPerInvoke = sampleCount)] |
| 46 | + public void FastConcurrentLru() |
| 47 | + { |
| 48 | + Func<int, int> func = x => x; |
| 49 | + |
| 50 | + for (int i = 0; i < sampleCount; i++) |
| 51 | + { |
| 52 | + fastConcurrentLru.GetOrAdd(samples[i], func); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + [Benchmark(OperationsPerInvoke = sampleCount)] |
| 57 | + public void ConcurrentLru() |
| 58 | + { |
| 59 | + Func<int, int> func = x => x; |
| 60 | + |
| 61 | + for (int i = 0; i < sampleCount; i++) |
| 62 | + { |
| 63 | + concurrentLru.GetOrAdd(samples[i], func); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [Benchmark(OperationsPerInvoke = sampleCount)] |
| 68 | + public void FastConcurrentTLru() |
| 69 | + { |
| 70 | + Func<int, int> func = x => x; |
| 71 | + |
| 72 | + for (int i = 0; i < sampleCount; i++) |
| 73 | + { |
| 74 | + fastConcurrentTLru.GetOrAdd(samples[i], func); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark(OperationsPerInvoke = sampleCount)] |
| 79 | + public void ConcurrentTLru() |
| 80 | + { |
| 81 | + Func<int, int> func = x => x; |
| 82 | + |
| 83 | + for (int i = 0; i < sampleCount; i++) |
| 84 | + { |
| 85 | + concurrentTlru.GetOrAdd(samples[i], func); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments