-
Notifications
You must be signed in to change notification settings - Fork 270
/
MemoryCacheTests.cs
180 lines (151 loc) · 5.83 KB
/
MemoryCacheTests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
using Microsoft.Extensions.Primitives;
using System;
using System.Linq;
namespace Microsoft.Extensions.Caching.Memory.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class MemoryCacheTests
{
private MemoryCache _memCache;
private (object key, object value)[] _items;
[GlobalSetup(Targets = new[] { nameof(GetHit), nameof(TryGetValueHit), nameof(GetMiss), nameof(TryGetValueMiss), nameof(SetOverride), nameof(CreateEntry) })]
public void SetupBasic()
{
_memCache = new MemoryCache(new MemoryCacheOptions());
for (var i = 0; i < 1024; i++)
{
_memCache.Set(i, i.ToString());
}
}
[GlobalCleanup(Targets = new[] { nameof(GetHit), nameof(TryGetValueHit), nameof(GetMiss), nameof(TryGetValueMiss), nameof(SetOverride), nameof(CreateEntry) })]
public void CleanupBasic() => _memCache.Dispose();
[Benchmark]
public object GetHit() => _memCache.Get("256");
[Benchmark]
public bool TryGetValueHit() => _memCache.TryGetValue("256", out _);
[Benchmark]
public object GetMiss() => _memCache.Get("-1");
[Benchmark]
public bool TryGetValueMiss() => _memCache.TryGetValue("-1", out _);
[Benchmark]
public object SetOverride() => _memCache.Set("512", "512");
[Benchmark]
public void CreateEntry() => _memCache.CreateEntry(this).Dispose();
[GlobalSetup(Targets = new[] { nameof(AddThenRemove_NoExpiration), nameof(AddThenRemove_AbsoluteExpiration), nameof(AddThenRemove_RelativeExpiration), nameof(AddThenRemove_SlidingExpiration), nameof(AddThenRemove_ExpirationTokens) })]
public void Setup_AddThenRemove()
{
_items = ValuesGenerator.ArrayOfUniqueValues<int>(100).Select(x => ((object)x, (object)x.ToString())).ToArray();
}
[Benchmark]
public void AddThenRemove_NoExpiration()
{
using (MemoryCache cache = new MemoryCache(new MemoryCacheOptions()))
{
foreach (var item in _items)
{
using (ICacheEntry entry = cache.CreateEntry(item.key))
{
entry.Value = item.value;
} // entry.Dispose is adding it to the cache
}
foreach (var item in _items)
{
cache.Remove(item.key);
}
}
}
[Benchmark]
public void AddThenRemove_AbsoluteExpiration()
{
DateTimeOffset absolute = DateTimeOffset.UtcNow.AddHours(1);
using (MemoryCache cache = new MemoryCache(new MemoryCacheOptions()))
{
foreach (var item in _items)
{
using (ICacheEntry entry = cache.CreateEntry(item.key))
{
entry.AbsoluteExpiration = absolute;
entry.Value = item.value;
}
}
foreach (var item in _items)
{
cache.Remove(item.key);
}
}
}
[Benchmark]
public void AddThenRemove_RelativeExpiration()
{
TimeSpan relative = TimeSpan.FromHours(1);
using (MemoryCache cache = new MemoryCache(new MemoryCacheOptions()))
{
foreach (var item in _items)
{
using (ICacheEntry entry = cache.CreateEntry(item.key))
{
entry.AbsoluteExpirationRelativeToNow = relative;
entry.Value = item.value;
}
}
foreach (var item in _items)
{
cache.Remove(item.key);
}
}
}
[Benchmark]
public void AddThenRemove_SlidingExpiration()
{
TimeSpan relative = TimeSpan.FromHours(1);
using (MemoryCache cache = new MemoryCache(new MemoryCacheOptions()))
{
foreach (var item in _items)
{
using (ICacheEntry entry = cache.CreateEntry(item.key))
{
entry.SlidingExpiration = relative;
entry.Value = item.value;
}
}
foreach (var item in _items)
{
cache.Remove(item.key);
}
}
}
[Benchmark]
public void AddThenRemove_ExpirationTokens()
{
Token token = new Token();
using (MemoryCache cache = new MemoryCache(new MemoryCacheOptions()))
{
foreach (var item in _items)
{
using (ICacheEntry entry = cache.CreateEntry(item.key))
{
entry.ExpirationTokens.Add(token);
entry.Value = item.value;
}
}
foreach (var item in _items)
{
cache.Remove(item.key);
}
}
}
private sealed class Token : IChangeToken, IDisposable
{
public bool HasChanged => false;
public bool ActiveChangeCallbacks => false;
public void Dispose() { }
public IDisposable RegisterChangeCallback(Action<object> callback, object state) => this;
}
}
}