Skip to content

Commit

Permalink
task: change lock count to be 32 based on perf test
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alastairtree committed Sep 20, 2020
1 parent 04abe50 commit 0b8c566
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -909,6 +910,69 @@ public async Task GetOrAddAsyncWithImmediateExpirationDoesExpireItems()
Assert.That(actual, Is.Null);
}

[Test]
[Ignore("Not a real unit tests - just used for hammering the cache")]
public async Task PerfTest()
{
var watch = new Stopwatch();
watch.Start();
var asyncThreads = 10;
var syncThreads = 10;
var uniqueCacheItems = 20;
int cacheMiss = 0;
int hits = 0;
var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(10));

async Task<ComplexTestObject> GetStuffAsync()
{
await Task.Delay(25);
Interlocked.Increment(ref cacheMiss);
return new ComplexTestObject();
}

ComplexTestObject GetStuff()
{
Thread.Sleep(25);
Interlocked.Increment(ref cacheMiss);
return new ComplexTestObject();
}


var asyncActions = Task.Run(() =>
{
Parallel.For(1, asyncThreads, async i =>
{
while (!cancel.IsCancellationRequested)
{
var key = $"stuff-{hits % uniqueCacheItems}";
var cached = await sut.GetOrAddAsync(key, () => GetStuffAsync(), DateTimeOffset.UtcNow.AddSeconds(1));
if(!cancel.IsCancellationRequested) Interlocked.Increment(ref hits);
}
});
});

var syncActions = Task.Run(() =>
{
Parallel.For(1, syncThreads, i =>
{
while (!cancel.IsCancellationRequested)
{
var key = $"stuff-{hits % uniqueCacheItems}";
var cached = sut.GetOrAdd(key, () => GetStuff(), DateTimeOffset.UtcNow.AddSeconds(1));
if (!cancel.IsCancellationRequested) Interlocked.Increment(ref hits);
}
});
});

await Task.WhenAll(asyncActions, syncActions);

watch.Stop();
Console.WriteLine(watch.Elapsed);
Console.WriteLine("miss " + cacheMiss);
Console.WriteLine("hit " + hits);
}


[Test]
public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
{
Expand Down
2 changes: 1 addition & 1 deletion LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CachingService : IAppCache
{
private readonly Lazy<ICacheProvider> cacheProvider;

private readonly int[] keyLocks = new int[8192];
private readonly int[] keyLocks = new int[32];

public CachingService() : this(DefaultCacheProvider)
{
Expand Down

0 comments on commit 0b8c566

Please sign in to comment.