|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | +using BitFaster.Caching.Lru; |
| 8 | + |
| 9 | +namespace BitFaster.Caching.Benchmarks.Lru |
| 10 | +{ |
| 11 | + [DisassemblyDiagnoser(printSource: true)] |
| 12 | + [MemoryDiagnoser] |
| 13 | + public class LruCycleBench |
| 14 | + { |
| 15 | + private static readonly ClassicLru<int, int> classicLru = new(8, 9, EqualityComparer<int>.Default); |
| 16 | + private static readonly ConcurrentLru<int, int> concurrentLru = new(8, 9, EqualityComparer<int>.Default); |
| 17 | + private static readonly ConcurrentTLru<int, int> concurrentTlru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10)); |
| 18 | + private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new(8, 9, EqualityComparer<int>.Default); |
| 19 | + private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1)); |
| 20 | + |
| 21 | + [Benchmark()] |
| 22 | + public void FastConcurrentLru() |
| 23 | + { |
| 24 | + Func<int, int> func = x => x; |
| 25 | + |
| 26 | + for (int i = 0; i < 128; i++) |
| 27 | + fastConcurrentLru.GetOrAdd(i, func); |
| 28 | + } |
| 29 | + |
| 30 | + [Benchmark()] |
| 31 | + public void ConcurrentLru() |
| 32 | + { |
| 33 | + Func<int, int> func = x => x; |
| 34 | + |
| 35 | + for (int i = 0; i < 128; i++) |
| 36 | + concurrentLru.GetOrAdd(i, func); |
| 37 | + } |
| 38 | + |
| 39 | + [Benchmark()] |
| 40 | + public void FastConcurrentTLru() |
| 41 | + { |
| 42 | + Func<int, int> func = x => x; |
| 43 | + |
| 44 | + for (int i = 0; i < 128; i++) |
| 45 | + fastConcurrentTLru.GetOrAdd(i, func); |
| 46 | + } |
| 47 | + |
| 48 | + [Benchmark()] |
| 49 | + public void ConcurrentTLru() |
| 50 | + { |
| 51 | + Func<int, int> func = x => x; |
| 52 | + |
| 53 | + for (int i = 0; i < 128; i++) |
| 54 | + concurrentTlru.GetOrAdd(i, func); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments