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