|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Runtime.Caching; |
| 5 | +using System.Text; |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | +using Lightweight.Caching.Lru; |
| 8 | + |
| 9 | +namespace Lightweight.Caching.Benchmarks.Lru |
| 10 | +{ |
| 11 | + [MemoryDiagnoser] |
| 12 | + public class MissHitHitRemove |
| 13 | + { |
| 14 | + const int capacity = 9; |
| 15 | + const int arraySize = 16; |
| 16 | + |
| 17 | + private static readonly ConcurrentDictionary<int, byte[]> dictionary = new ConcurrentDictionary<int, byte[]>(8, capacity, EqualityComparer<int>.Default); |
| 18 | + |
| 19 | + private static readonly ClassicLru<int, byte[]> classicLru = new ClassicLru<int, byte[]>(8, capacity, EqualityComparer<int>.Default); |
| 20 | + private static readonly ConcurrentLru<int, byte[]> concurrentLru = new ConcurrentLru<int, byte[]>(8, capacity, EqualityComparer<int>.Default); |
| 21 | + private static readonly ConcurrentTLru<int, byte[]> concurrentTlru = new ConcurrentTLru<int, byte[]>(8, capacity, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10)); |
| 22 | + private static readonly FastConcurrentLru<int, byte[]> fastConcurrentLru = new FastConcurrentLru<int, byte[]>(8, capacity, EqualityComparer<int>.Default); |
| 23 | + private static readonly FastConcurrentTLru<int, byte[]> fastConcurrentTLru = new FastConcurrentTLru<int, byte[]>(8, capacity, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1)); |
| 24 | + |
| 25 | + private static MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default; |
| 26 | + |
| 27 | + [Benchmark(Baseline = true)] |
| 28 | + public void ConcurrentDictionary() |
| 29 | + { |
| 30 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 31 | + dictionary.GetOrAdd(1, func); |
| 32 | + |
| 33 | + dictionary.GetOrAdd(1, func); |
| 34 | + dictionary.GetOrAdd(1, func); |
| 35 | + |
| 36 | + dictionary.TryRemove(1, out var removed); |
| 37 | + } |
| 38 | + |
| 39 | + [Benchmark()] |
| 40 | + public void FastConcurrentLru() |
| 41 | + { |
| 42 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 43 | + fastConcurrentLru.GetOrAdd(1, func); |
| 44 | + |
| 45 | + fastConcurrentLru.GetOrAdd(1, func); |
| 46 | + fastConcurrentLru.GetOrAdd(1, func); |
| 47 | + |
| 48 | + fastConcurrentLru.TryRemove(1); |
| 49 | + } |
| 50 | + |
| 51 | + [Benchmark()] |
| 52 | + public void ConcurrentLru() |
| 53 | + { |
| 54 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 55 | + concurrentLru.GetOrAdd(1, func); |
| 56 | + |
| 57 | + concurrentLru.GetOrAdd(1, func); |
| 58 | + concurrentLru.GetOrAdd(1, func); |
| 59 | + |
| 60 | + concurrentLru.TryRemove(1); |
| 61 | + } |
| 62 | + |
| 63 | + [Benchmark()] |
| 64 | + public void FastConcurrentTlru() |
| 65 | + { |
| 66 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 67 | + fastConcurrentTLru.GetOrAdd(1, func); |
| 68 | + |
| 69 | + fastConcurrentTLru.GetOrAdd(1, func); |
| 70 | + fastConcurrentTLru.GetOrAdd(1, func); |
| 71 | + |
| 72 | + fastConcurrentTLru.TryRemove(1); |
| 73 | + } |
| 74 | + |
| 75 | + [Benchmark()] |
| 76 | + public void ConcurrentTlru() |
| 77 | + { |
| 78 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 79 | + concurrentTlru.GetOrAdd(1, func); |
| 80 | + |
| 81 | + concurrentTlru.GetOrAdd(1, func); |
| 82 | + concurrentTlru.GetOrAdd(1, func); |
| 83 | + |
| 84 | + concurrentTlru.TryRemove(1); |
| 85 | + } |
| 86 | + |
| 87 | + [Benchmark()] |
| 88 | + public void ClassicLru() |
| 89 | + { |
| 90 | + Func<int, byte[]> func = x => new byte[arraySize]; |
| 91 | + classicLru.GetOrAdd(1, func); |
| 92 | + |
| 93 | + classicLru.GetOrAdd(1, func); |
| 94 | + classicLru.GetOrAdd(1, func); |
| 95 | + |
| 96 | + classicLru.TryRemove(1); |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark()] |
| 100 | + public void MemoryCache() |
| 101 | + { |
| 102 | + if (memoryCache.Get("1") == null) |
| 103 | + { |
| 104 | + memoryCache.Set("1", new byte[arraySize], new CacheItemPolicy()); |
| 105 | + } |
| 106 | + |
| 107 | + if (memoryCache.Get("1") == null) |
| 108 | + { |
| 109 | + memoryCache.Set("1", new byte[arraySize], new CacheItemPolicy()); |
| 110 | + } |
| 111 | + |
| 112 | + if (memoryCache.Get("1") == null) |
| 113 | + { |
| 114 | + memoryCache.Set("1", new byte[arraySize], new CacheItemPolicy()); |
| 115 | + } |
| 116 | + |
| 117 | + memoryCache.Remove("1"); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments