public void ClearingCollection_AllItemsMustBeRemoved() { var lruCollection = new LRUCollection <CacheEntry>(5); var entry = CacheEntry.New("1", "1", TimeSpan.FromSeconds(100)); lruCollection.Add(entry); lruCollection.Clear(); Assert.True(lruCollection.Count() == 0); Assert.Null(lruCollection.Get(new CacheEntryIdentifier("1"))); }
public void AddedItemsWithSameKeyButDifferentCasing_NewItemMustBeReplaced() { var lruCollection = new LRUCollection <CacheEntry>(5); var entryOne = CacheEntry.New("key", "1.1", TimeSpan.FromSeconds(100)); var entryTwo = CacheEntry.New("KEY", "1.2", TimeSpan.FromSeconds(100)); lruCollection.Add(entryOne); lruCollection.Add(entryTwo); Assert.Equal(1, lruCollection.Count()); Assert.Equal(lruCollection.Get(new CacheEntryIdentifier("key")).Value, entryTwo.Value); }
public void RemovingItem_ItemNoLongerExists() { var lruCollection = new LRUCollection <CacheEntry>(5); var entry = CacheEntry.New("1", "1", TimeSpan.FromSeconds(100)); lruCollection.Add(entry); lruCollection.Remove(new CacheEntryIdentifier("1")); Assert.True(lruCollection.Count() == 0); Assert.Null(lruCollection.Get(new CacheEntryIdentifier("1"))); }
public void AddMoreItems_MaxCountReach() { var lruCollection = new LRUCollection <CacheEntry>(5); // add 6 items to collection for (int i = 0; i < 6; i++) { lruCollection.Add(CacheEntry.New(i.ToString(), i.ToString(), TimeSpan.FromSeconds(100))); } // check count of collection Assert.True(lruCollection.Count() == 5); // check item one must have been remove Assert.Null(lruCollection.Get(new CacheEntryIdentifier("0"))); // the fifth item must be present Assert.NotNull(lruCollection.Get(new CacheEntryIdentifier("5"))); }
public void GetItem_MustMoveToStart() { var lruCollection = new LRUCollection <CacheEntry>(5); // add 5 items to collection for (int i = 0; i < 5; i++) { lruCollection.Add(CacheEntry.New(i.ToString(), i.ToString(), TimeSpan.FromSeconds(100))); } // accessing 0,2,4 lruCollection.Get(new CacheEntryIdentifier("0")); lruCollection.Get(new CacheEntryIdentifier("2")); lruCollection.Get(new CacheEntryIdentifier("4")); // adding 2 new items lruCollection.Add(CacheEntry.New("5", "5", TimeSpan.FromSeconds(100))); lruCollection.Add(CacheEntry.New("6", "7", TimeSpan.FromSeconds(100))); // now the items 1,3 must be replaced Assert.Null(lruCollection.Get(new CacheEntryIdentifier("1"))); Assert.Null(lruCollection.Get(new CacheEntryIdentifier("3"))); }
public static void Run(int min, int max, int numberElements, int capacity) { var randNum = new Random(); var randomNumbers = Enumerable.Repeat(0, numberElements).Select((i, vTuple) => vTuple.randNum.Next(vTuple.min, vTuple.max), (randNum, min, max)).ToArray(); var lru = new LRUCollection <int, long>(capacity); var lfu = new LFUCollection <int, long>(capacity); var lru2Q = new LRU2QCollection <int, long>(capacity); var lru2QSimple = new LRU2QSimpleCollection <int, long>(capacity); Core.Log.InfoBasic("Processing: {0} elements", numberElements); Core.Log.InfoBasic("Collections Capacity: {0} elements", capacity); Core.Log.InfoBasic("Random numbers from {0} to {1}", min, max); Core.Log.WriteEmptyLine(); using (var w = Watch.Create("LFU Start", "LFU End")) { Parallel.ForEach(randomNumbers, item => { var value = lfu.GetOrAdd(item, item); if (value != item) { Core.Log.Warning("Bad value: " + value + " " + item); } }); var ms = w.GlobalElapsedMilliseconds / numberElements; Core.Log.InfoDetail("LFU Hits: {0}", lfu.Hits); Core.Log.InfoDetail("LFU Deletes: {0}", lfu.Deletes); Core.Log.InfoDetail("LFU Inserts: {0}", lfu.Inserts); Core.Log.InfoDetail("LFU ms per item: {0}ms", ms); Core.Log.InfoDetail("LFU ns per item: {0}ns", ms * 1_000_000); } Thread.Sleep(1000); Core.Log.WriteEmptyLine(); using (var w = Watch.Create("LRU Start", "LRU End")) { Parallel.ForEach(randomNumbers, item => { var value = lru.GetOrAdd(item, item); if (value != item) { Core.Log.Warning("Bad value: " + value + " " + item); } }); var ms = w.GlobalElapsedMilliseconds / numberElements; Core.Log.InfoDetail("LRU Hits: {0}", lru.Hits); Core.Log.InfoDetail("LRU Deletes: {0}", lru.Deletes); Core.Log.InfoDetail("LRU Inserts: {0}", lru.Inserts); Core.Log.InfoDetail("LRU ms per item: {0}ms", ms); Core.Log.InfoDetail("LRU ns per item: {0}ns", ms * 1_000_000); } Thread.Sleep(1000); Core.Log.WriteEmptyLine(); using (var w = Watch.Create("LRU2QSimple Start", "LRU2QSimple End")) { Parallel.ForEach(randomNumbers, item => { var value = lru2QSimple.GetOrAdd(item, item); if (value != item) { Core.Log.Warning("Bad value: " + value + " " + item); } }); var ms = w.GlobalElapsedMilliseconds / numberElements; Core.Log.InfoDetail("LRU2QSimple Hits: {0}", lru2QSimple.Hits); Core.Log.InfoDetail("LRU2QSimple Deletes: {0}", lru2QSimple.Deletes); Core.Log.InfoDetail("LRU2QSimple Inserts: {0}", lru2QSimple.Inserts); Core.Log.InfoDetail("LRU2QSimple ms per item: {0}ms", ms); Core.Log.InfoDetail("LRU2QSimple ns per item: {0}ns", ms * 1_000_000); } Thread.Sleep(1000); Core.Log.WriteEmptyLine(); using (var w = Watch.Create("LRU2Q Start", "LRU2Q End")) { Parallel.ForEach(randomNumbers, item => { var value = lru2Q.GetOrAdd(item, item); if (value != item) { Core.Log.Warning("Bad value: " + value + " " + item); } }); var ms = w.GlobalElapsedMilliseconds / numberElements; Core.Log.InfoDetail("LRU2Q Hits: {0}", lru2Q.Hits); Core.Log.InfoDetail("LRU2Q Deletes: {0}", lru2Q.Deletes); Core.Log.InfoDetail("LRU2Q Inserts: {0}", lru2Q.Inserts); Core.Log.InfoDetail("LRU2Q ms per item: {0}ms", ms); Core.Log.InfoDetail("LRU2Q ns per item: {0}ns", ms * 1_000_000); } Thread.Sleep(1000); Core.Log.WriteEmptyLine(); }