/
CachePerformanceTest.cs
250 lines (238 loc) · 8.14 KB
/
CachePerformanceTest.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Diagnostics;
using KGySoft.Collections;
using KGySoft.CoreLibraries.PerformanceTests.Reflection;
using NUnit.Framework;
namespace KGySoft.CoreLibraries.PerformanceTests.Collections
{
/// <summary>
/// In this test out-of-cache retrieval is fast to demonstrate that RemoveLeastRecentUsed behavior is just a little bit slower than RemoveOldest.
/// See <see cref="ReflectorPerformanceTest.TestMethodInvoke"/> to test a simulation of re-using the most recent elements
/// </summary>
[TestFixture]
public class CachePerformanceTest
{
const int capacity = 10000;
Cache<int, string> testCache;
[SetUp]
public void ResetCache()
{
testCache = new Cache<int, string>(i => i.ToString(), capacity);
}
[Test]
public void Main()
{
var isPrimeCache = new Cache<int, bool>(IsPrime, 2) { Behavior = CacheBehavior.RemoveOldestElement };
Console.WriteLine("5 is prime: {0}", isPrimeCache[5]);
Console.WriteLine("6 is prime: {0}", isPrimeCache[6]);
Console.WriteLine("5 is prime: {0}", isPrimeCache[5]);
Console.WriteLine("7 is prime: {0}", isPrimeCache[7]);
Console.WriteLine("5 is prime: {0}", isPrimeCache[5]);
}
private static bool IsPrime(int number)
{
Console.WriteLine("IsPrime executed for: {0}", number);
if (number <= 1)
return false;
if (number % 2 == 0)
return true;
int i = 3;
int sqrt = (int)Math.Floor(Math.Sqrt(number));
while (i <= sqrt)
{
if (number % i == 0)
return false;
i += 2;
}
return true;
}
[Test]
public void PopulateTest()
{
const int iterations = 1000;
Console.WriteLine("==========Populate (iterations: {0:N0})===========", iterations);
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
testCache.Clear();
for (int j = 0; j < capacity; j++)
{
string s = testCache[j];
}
}
watch.Stop();
Console.WriteLine("Elapsed time: {0} ms", watch.ElapsedMilliseconds);
/* Circular list:
==========Populate (iterations: 1,000)===========
Elapsed time: 4232 ms
* CacheItem:
==========Populate (iterations: 1,000)===========
Elapsed time: 6444 ms
*/
}
[Test]
public void RemoveOldestNoDeleteTest()
{
const int iterations = 1000000;
Console.WriteLine("==========Remove oldest no delete (iterations: {0:N0})===========", iterations);
testCache.Behavior = CacheBehavior.RemoveOldestElement;
Random rnd = new Random(0);
int range = capacity;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
string s = testCache[rnd.Next(range)];
}
watch.Stop();
Console.WriteLine("Elapsed time: {0} ms", watch.ElapsedMilliseconds);
Console.WriteLine(testCache.GetStatistics());
/* Circular list:
==========Remove oldest no delete (iterations: 1,000,000)===========
Elapsed time: 52 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 10000
Number of reads: 1000000
Number of cache hits: 990000
Number of deletes: 0
Hit rate: 99,00 %
* CacheItem:
==========Remove oldest no delete (iterations: 1,000,000)===========
Elapsed time: 147 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 10000
Number of reads: 1000000
Number of cache hits: 990000
Number of deletes: 0
Hit rate: 99,00 %
*/
}
[Test]
public void RemoveLeastRecentUsedNoDeleteTest()
{
const int iterations = 1000000;
Console.WriteLine("==========Remove least recent used no delete (iterations: {0:N0})===========", iterations);
testCache.Behavior = CacheBehavior.RemoveLeastRecentUsedElement;
Random rnd = new Random(0);
int range = capacity;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
string s = testCache[rnd.Next(range)];
}
watch.Stop();
Console.WriteLine("Elapsed time: {0} ms", watch.ElapsedMilliseconds);
Console.WriteLine(testCache.GetStatistics());
/* Circular list:
==========Remove least recent used no delete (iterations: 1,000,000)===========
Elapsed time: 7528 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 10000
Number of reads: 1000000
Number of cache hits: 990000
Number of deletes: 0
Hit rate: 99,00 %
* CacheItem:
==========Remove least recent used no delete (iterations: 1,000,000)===========
Elapsed time: 289 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 10000
Number of reads: 1000000
Number of cache hits: 990000
Number of deletes: 0
Hit rate: 99,00 %
*/
}
[Test]
public void RemoveOldestTest()
{
const int iterations = 1000000;
Console.WriteLine("==========Remove oldest (iterations: {0:N0})===========", iterations);
testCache.Behavior = CacheBehavior.RemoveOldestElement;
Random rnd = new Random(0);
int range = (int)(capacity * 1.5);
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
string s = testCache[rnd.Next(range)];
}
watch.Stop();
Console.WriteLine("Elapsed time: {0} ms", watch.ElapsedMilliseconds);
Console.WriteLine(testCache.GetStatistics());
/* Circular list:
==========Remove oldest (iterations: 1,000,000)===========
Elapsed time: 309 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 338326
Number of reads: 1000000
Number of cache hits: 661674
Number of deletes: 328326
Hit rate: 66,17 %
* CacheItem:
==========Remove oldest (iterations: 1,000,000)===========
Elapsed time: 471 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 338326
Number of reads: 1000000
Number of cache hits: 661674
Number of deletes: 328326
Hit rate: 66,17 %
*/
}
[Test]
public void RemoveLeastRecentUsed()
{
const int iterations = 1000000;
Console.WriteLine("==========Remove least recent used (iterations: {0:N0})===========", iterations);
testCache.Behavior = CacheBehavior.RemoveLeastRecentUsedElement;
Random rnd = new Random(0);
int range = (int)(capacity * 1.5);
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
string s = testCache[rnd.Next(range)];
}
watch.Stop();
Console.WriteLine("Elapsed time: {0} ms", watch.ElapsedMilliseconds);
Console.WriteLine(testCache.GetStatistics());
/* Circular list:
==========Remove least recent used (iterations: 1,000,000)===========
Elapsed time: 6844 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 338451
Number of reads: 1000000
Number of cache hits: 661549
Number of deletes: 328451
Hit rate: 66,15 %
* CacheItem:
==========Remove least recent used (iterations: 1,000,000)===========
Elapsed time: 458 ms
Cache<Int32, String> cache statistics:
Count: 10000
Capacity: 10000
Number of writes: 338451
Number of reads: 1000000
Number of cache hits: 661549
Number of deletes: 328451
Hit rate: 66,15 % */
}
}
}