Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions BitFaster.Caching.HitRateAnalysis/results.wikibench.csv
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace BitFaster.Caching.UnitTests.Atomic
public class AtomicFactoryAsyncCacheTests
{
private const int capacity = 6;
private readonly AtomicFactoryAsyncCache<int, int> cache = new(new ConcurrentLru<int, AsyncAtomicFactory<int, int>>(capacity));
private readonly AtomicFactoryAsyncCache<int, int> cache = new AtomicFactoryAsyncCache<int, int>(new ConcurrentLru<int, AsyncAtomicFactory<int, int>>(capacity));

private List<ItemRemovedEventArgs<int, int>> removedItems = new();
private List<ItemRemovedEventArgs<int, int>> removedItems = new List<ItemRemovedEventArgs<int, int>>();

[Fact]
public void WhenInnerCacheIsNullCtorThrows()
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace BitFaster.Caching.UnitTests.Atomic
public class AtomicFactoryCacheTests
{
private const int capacity = 6;
private readonly AtomicFactoryCache<int, int> cache = new(new ConcurrentLru<int, AtomicFactory<int, int>>(capacity));
private readonly AtomicFactoryCache<int, int> cache = new AtomicFactoryCache<int, int>(new ConcurrentLru<int, AtomicFactory<int, int>>(capacity));

private List<ItemRemovedEventArgs<int, int>> removedItems = new();
private List<ItemRemovedEventArgs<int, int>> removedItems = new List<ItemRemovedEventArgs<int, int>>();

[Fact]
public void WhenInnerCacheIsNullCtorThrows()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net48;netcoreapp3.1;net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,7 +20,11 @@
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BitFaster.Caching\BitFaster.Caching.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CacheEventProxyBaseTests
private TestCacheEvents<int, int> testCacheEvents;
private EventProxy<int, int> eventProxy;

private List<ItemRemovedEventArgs<int, int>> removedItems = new();
private List<ItemRemovedEventArgs<int, int>> removedItems = new List<ItemRemovedEventArgs<int, int>>();

public CacheEventProxyBaseTests()
{
Expand Down
9 changes: 9 additions & 0 deletions BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
using BitFaster.Caching.Lfu;
using FluentAssertions;
using System.Collections.Generic;


#if !NET48
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#endif


using Xunit;

namespace BitFaster.Caching.UnitTests.Lfu
Expand Down Expand Up @@ -102,8 +109,10 @@ public void WhenClearedCountIsReset()

private static void SkipAvxIfNotSupported()
{
#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
}
}
}
4 changes: 2 additions & 2 deletions BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
});

Expand Down
16 changes: 8 additions & 8 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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");

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void WhenItemRemovedDontIncrementEvictedCount()
[Fact]
public void WhenOnItemRemovedInvokedEventIsFired()
{
List<ItemRemovedEventArgs<int, int>> eventList = new();
var eventList = new List<ItemRemovedEventArgs<int, int>>();

telemetryPolicy.ItemRemoved += (source, args) => eventList.Add(args);

Expand All @@ -105,7 +105,7 @@ public void WhenOnItemRemovedInvokedEventIsFired()
[Fact]
public void WhenEventSourceIsSetItemRemovedEventUsesSource()
{
List<object> eventSourceList = new();
List<object> eventSourceList = new List<object>();

telemetryPolicy.SetEventSource(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>();
scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(true); });
await tcs.Task;

Volatile.Read(ref run).Should().BeTrue();
Expand All @@ -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<bool>();
scheduler.Run(() => { tcs.SetResult(true); throw new InvalidCastException(); });

await tcs.Task;
await scheduler.WaitForExceptionAsync();
Expand All @@ -71,14 +71,14 @@ public async Task WhenWorkThrowsLastExceptionIsPopulated()
[Fact]
public void WhenBacklogExceededTasksAreDropped()
{
TaskCompletionSource tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource<bool>();

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>();
scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(true); });

await tcs.Task;

Expand All @@ -41,10 +41,10 @@ public async Task WhenWorkIsScheduledItIsRun()
[Fact]
public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty()
{
TaskCompletionSource tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource<bool>();
scheduler.RunCount.Should().Be(0);

scheduler.Run(() => { tcs.SetResult(); });
scheduler.Run(() => { tcs.SetResult(true); });

await tcs.Task;

Expand All @@ -54,9 +54,9 @@ public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty()
[Fact]
public async Task WhenWorkThrowsLastExceptionIsPopulated()
{
TaskCompletionSource tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource<bool>();
scheduler.Run(() => { throw new InvalidCastException(); });
scheduler.Run(() => { tcs.SetResult(); });
scheduler.Run(() => { tcs.SetResult(true); });

await tcs.Task;
await scheduler.WaitForExceptionAsync();
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class ScopedAsyncCacheTestBase
protected const int capacity = 6;
protected readonly IScopedAsyncCache<int, Disposable> cache;

protected List<ItemRemovedEventArgs<int, Scoped<Disposable>>> removedItems = new();
protected List<ItemRemovedEventArgs<int, Scoped<Disposable>>> removedItems = new List<ItemRemovedEventArgs<int, Scoped<Disposable>>>();

protected ScopedAsyncCacheTestBase(IScopedAsyncCache<int, Disposable> cache)
{
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class ScopedCacheTestBase
protected const int capacity = 6;
protected readonly IScopedCache<int, Disposable> cache;

protected List<ItemRemovedEventArgs<int, Scoped<Disposable>>> removedItems = new();
protected List<ItemRemovedEventArgs<int, Scoped<Disposable>>> removedItems = new List<ItemRemovedEventArgs<int, Scoped<Disposable>>>();

protected ScopedCacheTestBase(IScopedCache<int, Disposable> cache)
{
Expand Down