-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
89 lines (76 loc) · 2.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.DependencyInjection;
using Net.DistributedFileStoreCache;
using TestSupport.Helpers;
namespace ConsoleBenchmark;
public class ConsoleBenchmark
{
private readonly IDistributedFileStoreCacheString _distributedCache;
public ConsoleBenchmark()
{
var services = new ServiceCollection();
services.AddDistributedFileStoreCache(options =>
{
options.WhichVersion = FileStoreCacheVersions.String;
options.PathToCacheFileDirectory = TestData.GetCallingAssemblyTopLevelDir();
options.SecondPartOfCacheFileName = GetType().Name;
options.MaxBytesInJsonCacheFile = 50 * 10000;
});
var serviceProvider = services.BuildServiceProvider();
_distributedCache = serviceProvider.GetRequiredService<IDistributedFileStoreCacheString>();
}
//[Params(10_000)]
[Params(100, 1000, 10_000)]
public int NumKeysAtStart { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
var allKeyValues = new List<KeyValuePair<string,string>>();
for (int i = 0; i < NumKeysAtStart; i++)
{
allKeyValues.Add(new KeyValuePair<string, string>($"Key{i:D4}", DateTime.UtcNow.ToString("O")));
}
_distributedCache.ClearAll(allKeyValues);
}
[Benchmark]
public void AddKey()
{
_distributedCache.Set("NewKey", DateTime.UtcNow.ToString("O"), null);
_distributedCache.Get("NewKey"); //This forces an read
}
[Benchmark]
public void AddManyKey100()
{
var allKeyValues = new List<KeyValuePair<string, string>>();
for (int i = 0; i < 100; i++)
{
allKeyValues.Add(new KeyValuePair<string, string>($"NewKey{i:D4}", DateTime.UtcNow.ToString("O")));
}
_distributedCache.SetMany(allKeyValues);
_distributedCache.Get("NewKey"); //This forces an read
}
[Benchmark]
public async Task AddKeyAsync()
{
await _distributedCache.SetAsync("NewKey", DateTime.UtcNow.ToString("O"), null);
await _distributedCache.GetAsync("NewKey"); //This forces an read
}
[Benchmark]
public void GetKey()
{
_distributedCache.Get("Key0000");
}
[Benchmark]
public void GetAllKeyValues()
{
var all = _distributedCache.GetAllKeyValues();
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}