From b165c50fa5e91b1edbeb15ca70e08bcd6791acb9 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Mon, 3 Oct 2022 21:03:22 -0700 Subject: [PATCH 1/3] multi-target tests --- .../results.wikibench.csv | 18 +++++++++--------- .../Atomic/AtomicFactoryAsyncCacheTests.cs | 4 ++-- .../Atomic/AtomicFactoryCacheTests.cs | 4 ++-- .../BitFaster.Caching.UnitTests.csproj | 8 ++++++-- .../CacheEventProxyBaseTests.cs | 2 +- .../Lfu/CmSketchTests.cs | 9 +++++++++ .../Lfu/ConcurrentLfuTests.cs | 4 ++-- .../Lru/ConcurrentLruTests.cs | 10 +++++----- .../Lru/ConcurrentTLruTests.cs | 16 ++++++++-------- .../Lru/FastConcurrentTLruTests.cs | 2 +- .../Lru/TelemetryPolicyTests.cs | 4 ++-- .../Scheduler/BackgroundSchedulerTests.cs | 12 ++++++------ .../Scheduler/ThreadPoolSchedulerTests.cs | 12 ++++++------ .../ScopedAsyncCacheTestBase.cs | 2 +- .../ScopedCacheTestBase.cs | 2 +- 15 files changed, 61 insertions(+), 48 deletions(-) diff --git a/BitFaster.Caching.HitRateAnalysis/results.wikibench.csv b/BitFaster.Caching.HitRateAnalysis/results.wikibench.csv index f8010efa..b4242eb8 100644 --- a/BitFaster.Caching.HitRateAnalysis/results.wikibench.csv +++ b/BitFaster.Caching.HitRateAnalysis/results.wikibench.csv @@ -1,9 +1,9 @@ -CacheSize,ConcurrentLruHitRate,ClassicLruHitRate -25,30.748543935794594,12.339894258281078 -50,38.54143501776577,21.441939220308402 -75,42.52123346830987,27.84286871100062 -100,45.1025591654474,32.25609649363885 -125,47.21936919103159,35.30941454952625 -150,48.687634020955905,37.484624347754924 -175,49.932971822611414,39.10013286561485 -200,50.83386468801666,40.35904822418645 +CacheSize,ClassicLruHitRate,MemoryCacheHitRate,ConcurrentLruHitRate,ConcurrentLfuHitRate +25,12.339894258281078,12.74970724856239,30.736589709710316,28.783227692329305 +50,21.441939220308402,22.286008817050057,38.53992763913624,37.2522360657507 +75,27.84286871100062,28.721900126577438,42.52505320143473,40.92056906219085 +100,32.25609649363885,33.014461803950915,45.10352246066628,43.06310040802911 +125,35.30941454952625,35.74673719979668,47.223617055364834,44.57297200991882 +150,37.484624347754924,37.83348438709171,48.6901492918052,47.162855871524066 +175,39.10013286561485,39.372468911151955,49.93844833431874,48.24502662608191 +200,40.35904822418645,40.575865463335525,50.836779102023776,49.019125758302316 diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs index 91c83b75..97502482 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs @@ -15,9 +15,9 @@ namespace BitFaster.Caching.UnitTests.Atomic public class AtomicFactoryAsyncCacheTests { private const int capacity = 6; - private readonly AtomicFactoryAsyncCache cache = new(new ConcurrentLru>(capacity)); + private readonly AtomicFactoryAsyncCache cache = new AtomicFactoryAsyncCache(new ConcurrentLru>(capacity)); - private List> removedItems = new(); + private List> removedItems = new List>(); [Fact] public void WhenInnerCacheIsNullCtorThrows() diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs index c4cd420d..588894f0 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs @@ -15,9 +15,9 @@ namespace BitFaster.Caching.UnitTests.Atomic public class AtomicFactoryCacheTests { private const int capacity = 6; - private readonly AtomicFactoryCache cache = new(new ConcurrentLru>(capacity)); + private readonly AtomicFactoryCache cache = new AtomicFactoryCache(new ConcurrentLru>(capacity)); - private List> removedItems = new(); + private List> removedItems = new List>(); [Fact] public void WhenInnerCacheIsNullCtorThrows() diff --git a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj index 1cd685a7..fbfd6315 100644 --- a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj +++ b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj @@ -1,7 +1,7 @@  - net6.0 + netstandard2.0;netcoreapp3.1;net6.0 @@ -20,7 +20,11 @@ - + + + + + diff --git a/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs b/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs index 95b48bf6..31fa2595 100644 --- a/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs +++ b/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs @@ -14,7 +14,7 @@ public class CacheEventProxyBaseTests private TestCacheEvents testCacheEvents; private EventProxy eventProxy; - private List> removedItems = new(); + private List> removedItems = new List>(); public CacheEventProxyBaseTests() { diff --git a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs index 761b40d9..a51db680 100644 --- a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs @@ -2,7 +2,14 @@ using BitFaster.Caching.Lfu; using FluentAssertions; using System.Collections.Generic; + + +#if !NETSTANDARD2_0 +using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +#endif + + using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -102,8 +109,10 @@ public void WhenClearedCountIsReset() private static void SkipAvxIfNotSupported() { +#if !NETSTANDARD2_0 // when we are trying to test Avx2, skip the test if it's not supported Skip.If(typeof(I) == typeof(DetectIsa) && !Avx2.IsSupported); +#endif } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs index 5a3c0048..8b99c2d9 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs @@ -121,7 +121,7 @@ public void WhenNewItemsAreAddedTheyArePromotedBasedOnFrequency() { for (int i = 0; i < 15; i++) { - cache.GetOrAdd(j + 20, k => k); + cache.GetOrAdd(j + 20, kk => kk); } cache.DoMaintenance(); LogLru(); @@ -826,7 +826,7 @@ private void VerifyHits(int iterations, int minSamples) // verify this doesn't block or throw var b = cache.Scheduler as BackgroundThreadScheduler; - if (b is not null) + if (b != null) { b.Dispose(); } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs index 6fa46662..cf933717 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs @@ -513,7 +513,7 @@ public void WhenValueEvictedItemRemovedEventIsFired() // hot[8, 7], warm[1, 0], cold[6, 5], evicted[4, 3] for (int i = 0; i < 8; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } removedItems.Count.Should().Be(2); @@ -547,7 +547,7 @@ public void WhenItemRemovedEventIsUnregisteredEventIsNotFired() for (int i = 0; i < 6; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } removedItems.Count.Should().Be(0); @@ -756,7 +756,7 @@ public void WhenItemsArClearedAnEventIsFired() for (int i = 0; i < 6; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } lruEvents.Clear(); @@ -946,7 +946,7 @@ public void WhenItemsAreTrimmedAnEventIsFired() for (int i = 0; i < 6; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } lruEvents.Trim(2); @@ -968,7 +968,7 @@ public async Task WhenItemsAreScannedInParallelCapacityIsNotExceeded() await Threaded.Run(4, () => { for (int i = 0; i < 100000; i++) { - lru.GetOrAdd(i + 1, i =>i.ToString()); + lru.GetOrAdd(i + 1, j => j.ToString()); } }); diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs index 1e09a6ff..e44b84d2 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs @@ -80,8 +80,8 @@ public void WhenItemIsNotExpiredItIsNotRemoved() public async Task WhenItemIsExpiredItIsRemoved() { lru.GetOrAdd(1, valueFactory.Create); - - await Task.Delay(timeToLive * ttlWaitMlutiplier); + + await Task.Delay(TimeSpan.FromTicks(timeToLive.Ticks * ttlWaitMlutiplier)); lru.TryGet(1, out var value).Should().BeFalse(); } @@ -91,7 +91,7 @@ public async Task WhenItemIsUpdatedTtlIsExtended() { lru.GetOrAdd(1, valueFactory.Create); - await Task.Delay(timeToLive * ttlWaitMlutiplier); + await Task.Delay(TimeSpan.FromTicks(timeToLive.Ticks * ttlWaitMlutiplier)); lru.TryUpdate(1, "3"); @@ -110,7 +110,7 @@ public void WhenValueEvictedItemRemovedEventIsFired() // hot[8, 7], warm[1, 0], cold[6, 5], evicted[4, 3] for (int i = 0; i < 8; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } removedItems.Count.Should().Be(2); @@ -133,7 +133,7 @@ public void WhenItemRemovedEventIsUnregisteredEventIsNotFired() for (int i = 0; i < 6; i++) { - lruEvents.GetOrAdd(i + 1, i => i + 1); + lruEvents.GetOrAdd(i + 1, j => j + 1); } removedItems.Count.Should().Be(0); @@ -157,7 +157,7 @@ public async Task WhenItemsAreExpiredExpireRemovesExpiredItems() lru.AddOrUpdate(8, "8"); lru.AddOrUpdate(9, "9"); - await Task.Delay(timeToLive * ttlWaitMlutiplier); + await Task.Delay(TimeSpan.FromTicks(timeToLive.Ticks * ttlWaitMlutiplier)); lru.Policy.ExpireAfterWrite.Value.TrimExpired(); @@ -175,7 +175,7 @@ public async Task WhenCacheHasExpiredAndFreshItemsExpireRemovesOnlyExpiredItems( lru.AddOrUpdate(5, "5"); lru.AddOrUpdate(6, "6"); - await Task.Delay(timeToLive * ttlWaitMlutiplier); + await Task.Delay(TimeSpan.FromTicks(timeToLive.Ticks * ttlWaitMlutiplier)); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); @@ -193,7 +193,7 @@ public async Task WhenItemsAreExpiredTrimRemovesExpiredItems() lru.AddOrUpdate(2, "2"); lru.AddOrUpdate(3, "3"); - await Task.Delay(timeToLive * ttlWaitMlutiplier); + await Task.Delay(TimeSpan.FromTicks(timeToLive.Ticks * ttlWaitMlutiplier)); lru.Trim(1); diff --git a/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs b/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs index be296238..76fff60c 100644 --- a/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs @@ -47,7 +47,7 @@ public async Task WhenItemsAreExpiredExpireRemovesExpiredItems() lru.AddOrUpdate(2, "2"); lru.AddOrUpdate(3, "3"); - await Task.Delay(ttl * 2); + await Task.Delay(TimeSpan.FromTicks(ttl.Ticks * 2)); lru.Policy.ExpireAfterWrite.Value.TrimExpired(); diff --git a/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs index eca1a39b..b6064035 100644 --- a/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs @@ -90,7 +90,7 @@ public void WhenItemRemovedDontIncrementEvictedCount() [Fact] public void WhenOnItemRemovedInvokedEventIsFired() { - List> eventList = new(); + var eventList = new List>(); telemetryPolicy.ItemRemoved += (source, args) => eventList.Add(args); @@ -105,7 +105,7 @@ public void WhenOnItemRemovedInvokedEventIsFired() [Fact] public void WhenEventSourceIsSetItemRemovedEventUsesSource() { - List eventSourceList = new(); + List eventSourceList = new List(); telemetryPolicy.SetEventSource(this); diff --git a/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs index 08e7d1c9..b3630597 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs @@ -36,8 +36,8 @@ public async Task WhenWorkIsScheduledItIsRun() { bool run = false; - TaskCompletionSource tcs = new TaskCompletionSource(); - scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(); }); + var tcs = new TaskCompletionSource(); + scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(true); }); await tcs.Task; Volatile.Read(ref run).Should().BeTrue(); @@ -58,8 +58,8 @@ public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty() [Fact] public async Task WhenWorkThrowsLastExceptionIsPopulated() { - TaskCompletionSource tcs = new TaskCompletionSource(); - scheduler.Run(() => { tcs.SetResult(); throw new InvalidCastException(); }); + var tcs = new TaskCompletionSource(); + scheduler.Run(() => { tcs.SetResult(true); throw new InvalidCastException(); }); await tcs.Task; await scheduler.WaitForExceptionAsync(); @@ -71,14 +71,14 @@ public async Task WhenWorkThrowsLastExceptionIsPopulated() [Fact] public void WhenBacklogExceededTasksAreDropped() { - TaskCompletionSource tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); for (int i = 0; i < BackgroundThreadScheduler.MaxBacklog * 2; i++) { scheduler.Run(() => { tcs.Task.Wait(); }); } - tcs.SetResult(); + tcs.SetResult(true); scheduler.RunCount.Should().BeCloseTo(BackgroundThreadScheduler.MaxBacklog, 1); } diff --git a/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs index 57bb20b1..e27bba9e 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs @@ -30,8 +30,8 @@ public async Task WhenWorkIsScheduledItIsRun() { bool run = false; - TaskCompletionSource tcs = new TaskCompletionSource(); - scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(); }); + var tcs = new TaskCompletionSource(); + scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(true); }); await tcs.Task; @@ -41,10 +41,10 @@ public async Task WhenWorkIsScheduledItIsRun() [Fact] public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty() { - TaskCompletionSource tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); scheduler.RunCount.Should().Be(0); - scheduler.Run(() => { tcs.SetResult(); }); + scheduler.Run(() => { tcs.SetResult(true); }); await tcs.Task; @@ -54,9 +54,9 @@ public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty() [Fact] public async Task WhenWorkThrowsLastExceptionIsPopulated() { - TaskCompletionSource tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); scheduler.Run(() => { throw new InvalidCastException(); }); - scheduler.Run(() => { tcs.SetResult(); }); + scheduler.Run(() => { tcs.SetResult(true); }); await tcs.Task; await scheduler.WaitForExceptionAsync(); diff --git a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs index 31714b27..2564dccf 100644 --- a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs +++ b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs @@ -14,7 +14,7 @@ public abstract class ScopedAsyncCacheTestBase protected const int capacity = 6; protected readonly IScopedAsyncCache cache; - protected List>> removedItems = new(); + protected List>> removedItems = new List>>(); protected ScopedAsyncCacheTestBase(IScopedAsyncCache cache) { diff --git a/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs b/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs index f74e0029..196d0a3e 100644 --- a/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs +++ b/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs @@ -14,7 +14,7 @@ public abstract class ScopedCacheTestBase protected const int capacity = 6; protected readonly IScopedCache cache; - protected List>> removedItems = new(); + protected List>> removedItems = new List>>(); protected ScopedCacheTestBase(IScopedCache cache) { From dfe1e8c9530d73962f9796cd341baaa1704b2749 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Mon, 3 Oct 2022 21:09:28 -0700 Subject: [PATCH 2/3] runs --- .../BitFaster.Caching.UnitTests.csproj | 4 ++-- BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj index fbfd6315..c6098f51 100644 --- a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj +++ b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj @@ -1,7 +1,7 @@  - netstandard2.0;netcoreapp3.1;net6.0 + net48;netcoreapp3.1;net6.0 @@ -20,7 +20,7 @@ - + diff --git a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs index a51db680..7a8025bf 100644 --- a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; -#if !NETSTANDARD2_0 +#if !NET48 using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; #endif @@ -109,7 +109,7 @@ public void WhenClearedCountIsReset() private static void SkipAvxIfNotSupported() { -#if !NETSTANDARD2_0 +#if !NET48 // when we are trying to test Avx2, skip the test if it's not supported Skip.If(typeof(I) == typeof(DetectIsa) && !Avx2.IsSupported); #endif From 96e8583b7c966634c354146a010d3dbc529ae00a Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Mon, 3 Oct 2022 21:34:22 -0700 Subject: [PATCH 3/3] xplat --- .github/workflows/gate.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index ff9e68e3..3cc147aa 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -24,7 +24,7 @@ jobs: - name: Build run: dotnet build --configuration Release --no-restore - name: Test - run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov --logger "trx;LogFileName=results.trx" + run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov --logger "trx;LogFilePrefix=results" --collect:"XPlat Code Coverage" - name: Publish NuGet artifacts uses: actions/upload-artifact@v3 with: @@ -62,7 +62,7 @@ jobs: - name: Build run: dotnet build --configuration Release --no-restore - name: Test - run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov --logger "trx;LogFileName=results.trx" + run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov --logger "trx;LogFilePrefix=results" --collect:"XPlat Code Coverage" - name: Publish coverage report to coveralls.io uses: coverallsapp/github-action@master with: