Skip to content

Commit 527a376

Browse files
committed
chore: benchmark tests
1 parent d1f983f commit 527a376

File tree

9 files changed

+238
-125
lines changed

9 files changed

+238
-125
lines changed

.devcontainer/docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
version: "3.8"
21
services:
32
devcontainer:
43
image: mcr.microsoft.com/devcontainers/dotnet:8.0-bookworm
54
volumes:
65
- ..:/workspace:cached
76
command: sleep infinity
87
redis:
9-
image: redis
8+
image: redis

.editorconfig

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
`L1L2RedisCache` is heavily inspired by development insights provided over the past several years by [StackOverflow](https://stackoverflow.com/). It attempts to simplify those concepts into a highly accessible `IDistributedCache` implementation that is more performant.
66

7+
I expect to gracefully decomission this project when [`StackExchange.Redis`](https://github.com/StackExchange/StackExchange.Redis) has [client-side caching](https://redis.io/docs/latest/develop/use/client-side-caching/) support.
8+
79
## Configuration
810

911
It is intended that L1L12RedisCache be used as an `IDistributedCache` implementation.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace L1L2RedisCache.Tests.System;
8+
9+
public abstract class BenchmarkBase
10+
{
11+
[Params(100)]
12+
public int Iterations { get; set; }
13+
14+
protected DistributedCacheEntryOptions DistributedCacheEntryOptions { get; set; } =
15+
new DistributedCacheEntryOptions
16+
{
17+
AbsoluteExpirationRelativeToNow =
18+
TimeSpan.FromHours(1),
19+
};
20+
protected IMemoryCache? L1Cache { get; set; }
21+
protected IDistributedCache? L1L2Cache { get; set; }
22+
protected IDistributedCache? L2Cache { get; set; }
23+
24+
[GlobalSetup]
25+
public void GlobalSetup()
26+
{
27+
var configuration = new ConfigurationBuilder()
28+
.AddJsonFile("appsettings.json")
29+
.AddEnvironmentVariables()
30+
.Build();
31+
32+
var services = new ServiceCollection();
33+
services.AddSingleton(configuration);
34+
services.AddL1L2RedisCache(options =>
35+
{
36+
configuration.Bind("L1L2RedisCache", options);
37+
});
38+
var serviceProvider = services
39+
.BuildServiceProvider();
40+
41+
L1Cache = serviceProvider
42+
.GetRequiredService<IMemoryCache>();
43+
L1L2Cache = serviceProvider
44+
.GetRequiredService<IDistributedCache>();
45+
L2Cache = serviceProvider
46+
.GetRequiredService<Func<IDistributedCache>>()
47+
.Invoke();
48+
}
49+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
using Microsoft.Extensions.Caching.Memory;
4+
5+
namespace L1L2RedisCache.Tests.System;
6+
7+
public class GetBenchmark : BenchmarkBase
8+
{
9+
public new void GlobalSetup()
10+
{
11+
base.GlobalSetup();
12+
13+
for (var iteration = 1;
14+
iteration <= Iterations;
15+
iteration++)
16+
{
17+
L1L2Cache!.SetString(
18+
$"Get:{iteration}",
19+
"Value",
20+
DistributedCacheEntryOptions);
21+
L1L2Cache!.SetString(
22+
$"GetPropagation:{iteration}",
23+
"Value",
24+
DistributedCacheEntryOptions);
25+
}
26+
}
27+
28+
[IterationSetup]
29+
public void IterationSetup()
30+
{
31+
for (var iteration = 1;
32+
iteration <= Iterations;
33+
iteration++)
34+
{
35+
L1Cache!.Remove(
36+
$"GetPropagation:{iteration}");
37+
}
38+
}
39+
40+
[Benchmark]
41+
public void L1Get()
42+
{
43+
for (var iteration = 1;
44+
iteration <= Iterations;
45+
iteration++)
46+
{
47+
L1Cache!.Get<string>(
48+
$"Get:{iteration}");
49+
}
50+
}
51+
52+
[Benchmark]
53+
public void L1L2Get()
54+
{
55+
for (var iteration = 1;
56+
iteration <= Iterations;
57+
iteration++)
58+
{
59+
L1L2Cache!.GetString(
60+
$"Get:{iteration}");
61+
}
62+
}
63+
64+
[Benchmark]
65+
public void L1L2GetPropagation()
66+
{
67+
for (var iteration = 1;
68+
iteration <= Iterations;
69+
iteration++)
70+
{
71+
L1L2Cache!.GetString(
72+
$"GetPropagation:{iteration}");
73+
}
74+
}
75+
76+
[Benchmark]
77+
public void L2Get()
78+
{
79+
for (var iteration = 1;
80+
iteration <= Iterations;
81+
iteration++)
82+
{
83+
L2Cache!.GetString(
84+
$"Get:{iteration}");
85+
}
86+
}
87+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
4+
namespace L1L2RedisCache.Tests.System;
5+
6+
[SimpleJob]
7+
public class SetBenchmark : BenchmarkBase
8+
{
9+
[Benchmark]
10+
public void L1L2Set()
11+
{
12+
for (var iteration = 1;
13+
iteration <= Iterations;
14+
iteration++)
15+
{
16+
L1L2Cache!.SetString(
17+
$"Set:{iteration}",
18+
"Value",
19+
DistributedCacheEntryOptions);
20+
}
21+
}
22+
23+
[Benchmark]
24+
public void L2Set()
25+
{
26+
for (var iteration = 1;
27+
iteration <= Iterations;
28+
iteration++)
29+
{
30+
L2Cache!.SetString(
31+
$"Set:{iteration}",
32+
"Value",
33+
DistributedCacheEntryOptions);
34+
}
35+
}
36+
}

tests/System/L1L2RedisCache.Tests.System.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
<ProjectReference Include="..\..\src\L1L2RedisCache.csproj" />
1515
</ItemGroup>
1616
<ItemGroup>
17+
<PackageReference Include="BenchmarkDotNet" Version="0.14.*" />
1718
<PackageReference Include="coverlet.collector" Version="6.0.*" />
1819
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.*" />
1920
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.*" />
2021
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.*" />
2122
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.*" />
2223
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.*" />
23-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.*" />
24-
<PackageReference Include="MSTest.TestAdapter" Version="3.2.*" />
25-
<PackageReference Include="MSTest.TestFramework" Version="3.2.*" />
24+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.*" />
25+
<PackageReference Include="MSTest.TestAdapter" Version="3.5.*" />
26+
<PackageReference Include="MSTest.TestFramework" Version="3.5.*" />
2627
</ItemGroup>
2728
</Project>

0 commit comments

Comments
 (0)