public void WhenItemIsRemovedItIsDisposed() { var lruOfDisposable = new ConcurrentLru <int, DisposableItem>(1, 6, EqualityComparer <int> .Default); var disposableValueFactory = new DisposableValueFactory(); lruOfDisposable.GetOrAdd(1, disposableValueFactory.Create); lruOfDisposable.TryRemove(1); disposableValueFactory.Items[1].IsDisposed.Should().BeTrue(); }
public void WhenValueExpiresItIsDisposed() { var lruOfDisposable = new ConcurrentLru <int, DisposableItem>(1, 6, EqualityComparer <int> .Default); var disposableValueFactory = new DisposableValueFactory(); for (int i = 0; i < 5; i++) { lruOfDisposable.GetOrAdd(i, disposableValueFactory.Create); } disposableValueFactory.Items[0].IsDisposed.Should().BeTrue(); disposableValueFactory.Items[1].IsDisposed.Should().BeFalse(); }
public void WhenScopedIsCreatedFromCacheItemHasExpectedLifetime() { var lru = new ConcurrentLru <int, Scoped <Disposable> >(2, 9, EqualityComparer <int> .Default); var valueFactory = new DisposableValueFactory(); using (var lifetime = lru.GetOrAdd(1, valueFactory.Create).CreateLifetime()) { lifetime.Value.IsDisposed.Should().BeFalse(); } valueFactory.Disposable.IsDisposed.Should().BeFalse(); lru.TryRemove(1); valueFactory.Disposable.IsDisposed.Should().BeTrue(); }
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue() { var x = new ConcurrentLru <int, int>(3); x.GetOrAdd(1, k => k).Should().Be(1); }
public void WhenComparerIsNullCtorThrows() { Action constructor = () => { var x = new ConcurrentLru <int, string>(1, 3, null); }; constructor.Should().Throw <ArgumentNullException>(); }
public void WhenCapacityIsLessThan3CtorThrows() { Action constructor = () => { var x = new ConcurrentLru <int, string>(1, 2, EqualityComparer <int> .Default); }; constructor.Should().Throw <ArgumentOutOfRangeException>(); }
static void Main(string[] args) { double[] sValues = { 0.5, 0.86 }; // % of total number of items double[] cacheSizes = { 0.0125, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4 }; List <Analysis> analysis = new List <Analysis>(); foreach (var sValue in sValues) { foreach (var cacheSize in cacheSizes) { analysis.Add(new Analysis { N = n, s = sValue, Samples = sampleCount, CacheSizePercent = cacheSize }); } } int[][] zipdfDistribution = new int[sValues.Length][]; for (int i = 0; i < sValues.Length; i++) { Console.WriteLine($"Generating Zipfan distribution with {sampleCount} samples, s = {sValues[i]}, N = {n}"); var sw = Stopwatch.StartNew(); zipdfDistribution[i] = new int[sampleCount]; Zipf.Samples(zipdfDistribution[i], sValues[i], n); Console.WriteLine($"Took {sw.Elapsed}."); } List <AnalysisResult> results = new List <AnalysisResult>(); Func <int, int> func = x => x; foreach (var a in analysis) { a.WriteSummaryToConsole(); int cacheSize = (int)(a.N * a.CacheSizePercent); var concurrentLru = new ConcurrentLru <int, int>(1, cacheSize, EqualityComparer <int> .Default); var classicLru = new ClassicLru <int, int>(1, cacheSize, EqualityComparer <int> .Default); var concurrentLruScan = new ConcurrentLru <int, int>(1, cacheSize, EqualityComparer <int> .Default); var classicLruScan = new ClassicLru <int, int>(1, cacheSize, EqualityComparer <int> .Default); var d = a.s == 0.5 ? 0 : 1; var lruSw = Stopwatch.StartNew(); for (int i = 0; i < sampleCount; i++) { concurrentLru.GetOrAdd(zipdfDistribution[d][i], func); } lruSw.Stop(); Console.WriteLine($"concurrentLru size={cacheSize} took {lruSw.Elapsed}."); var clruSw = Stopwatch.StartNew(); for (int i = 0; i < sampleCount; i++) { classicLru.GetOrAdd(zipdfDistribution[d][i], func); } clruSw.Stop(); Console.WriteLine($"classic lru size={cacheSize} took {clruSw.Elapsed}."); var lruSwScan = Stopwatch.StartNew(); for (int i = 0; i < sampleCount; i++) { concurrentLruScan.GetOrAdd(zipdfDistribution[d][i], func); concurrentLruScan.GetOrAdd(i % n, func); } lruSwScan.Stop(); Console.WriteLine($"concurrentLruScan lru size={cacheSize} took {lruSwScan.Elapsed}."); var clruSwScan = Stopwatch.StartNew(); for (int i = 0; i < sampleCount; i++) { classicLruScan.GetOrAdd(zipdfDistribution[d][i], func); classicLruScan.GetOrAdd(i % n, func); } clruSwScan.Stop(); Console.WriteLine($"classicLruScan lru size={cacheSize} took {clruSwScan.Elapsed}."); results.Add(new AnalysisResult { Cache = "ClassicLru", N = a.N, s = a.s, CacheSizePercent = a.CacheSizePercent * 100.0, Samples = a.Samples, IsScan = false, HitRatio = classicLru.HitRatio * 100.0, Duration = clruSw.Elapsed, }); results.Add(new AnalysisResult { Cache = "ConcurrentLru", N = a.N, s = a.s, CacheSizePercent = a.CacheSizePercent * 100.0, Samples = a.Samples, IsScan = false, HitRatio = concurrentLru.HitRatio * 100.0, Duration = lruSw.Elapsed, }); results.Add(new AnalysisResult { Cache = "ClassicLru", N = a.N, s = a.s, CacheSizePercent = a.CacheSizePercent * 100.0, Samples = a.Samples, IsScan = true, HitRatio = classicLruScan.HitRatio * 100.0, Duration = clruSwScan.Elapsed, }); results.Add(new AnalysisResult { Cache = "ConcurrentLru", N = a.N, s = a.s, CacheSizePercent = a.CacheSizePercent * 100.0, Samples = a.Samples, IsScan = true, HitRatio = concurrentLruScan.HitRatio * 100.0, Duration = lruSwScan.Elapsed, }); } AnalysisResult.WriteToConsole(results); AnalysisResult.WriteToFile("results.csv", results); Console.ReadLine(); }