Skip to content

Commit 0b8c566

Browse files
committed
task: change lock count to be 32 based on perf test
test-result on 6 (12 HT) core machine: # Locks, Cache req in 10s 1 6240625 2 10452129 6 19198527 12 27735172 24 27086363 32 32524781 128 32039528 1024 31144725 8000 33000461
1 parent 04abe50 commit 0b8c566

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using FluentAssertions;
@@ -909,6 +910,69 @@ public async Task GetOrAddAsyncWithImmediateExpirationDoesExpireItems()
909910
Assert.That(actual, Is.Null);
910911
}
911912

913+
[Test]
914+
[Ignore("Not a real unit tests - just used for hammering the cache")]
915+
public async Task PerfTest()
916+
{
917+
var watch = new Stopwatch();
918+
watch.Start();
919+
var asyncThreads = 10;
920+
var syncThreads = 10;
921+
var uniqueCacheItems = 20;
922+
int cacheMiss = 0;
923+
int hits = 0;
924+
var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(10));
925+
926+
async Task<ComplexTestObject> GetStuffAsync()
927+
{
928+
await Task.Delay(25);
929+
Interlocked.Increment(ref cacheMiss);
930+
return new ComplexTestObject();
931+
}
932+
933+
ComplexTestObject GetStuff()
934+
{
935+
Thread.Sleep(25);
936+
Interlocked.Increment(ref cacheMiss);
937+
return new ComplexTestObject();
938+
}
939+
940+
941+
var asyncActions = Task.Run(() =>
942+
{
943+
Parallel.For(1, asyncThreads, async i =>
944+
{
945+
while (!cancel.IsCancellationRequested)
946+
{
947+
var key = $"stuff-{hits % uniqueCacheItems}";
948+
var cached = await sut.GetOrAddAsync(key, () => GetStuffAsync(), DateTimeOffset.UtcNow.AddSeconds(1));
949+
if(!cancel.IsCancellationRequested) Interlocked.Increment(ref hits);
950+
}
951+
});
952+
});
953+
954+
var syncActions = Task.Run(() =>
955+
{
956+
Parallel.For(1, syncThreads, i =>
957+
{
958+
while (!cancel.IsCancellationRequested)
959+
{
960+
var key = $"stuff-{hits % uniqueCacheItems}";
961+
var cached = sut.GetOrAdd(key, () => GetStuff(), DateTimeOffset.UtcNow.AddSeconds(1));
962+
if (!cancel.IsCancellationRequested) Interlocked.Increment(ref hits);
963+
}
964+
});
965+
});
966+
967+
await Task.WhenAll(asyncActions, syncActions);
968+
969+
watch.Stop();
970+
Console.WriteLine(watch.Elapsed);
971+
Console.WriteLine("miss " + cacheMiss);
972+
Console.WriteLine("hit " + hits);
973+
}
974+
975+
912976
[Test]
913977
public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
914978
{

LazyCache/CachingService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CachingService : IAppCache
1313
{
1414
private readonly Lazy<ICacheProvider> cacheProvider;
1515

16-
private readonly int[] keyLocks = new int[8192];
16+
private readonly int[] keyLocks = new int[32];
1717

1818
public CachingService() : this(DefaultCacheProvider)
1919
{

0 commit comments

Comments
 (0)