forked from jitbit/FastCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (88 loc) · 2.39 KB
/
Program.cs
File metadata and controls
101 lines (88 loc) · 2.39 KB
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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Jitbit.Utils;
using System.Collections.Concurrent;
using System.Runtime.Caching;
BenchmarkRunner.Run<BenchMark>();
[ShortRunJob, MemoryDiagnoser]
public class BenchMark
{
private static FastCache<string, int> _cache = new(600_000);
private static ConcurrentDictionary<string, int> _dict = new();
private static DateTime _dtPlus10Mins = DateTime.Now.AddMinutes(10);
[GlobalSetup]
public void GlobalSetup()
{
//add 10000 values
for (int i = 0; i < 1000; i++)
{
_dict.TryAdd("test" + i, i);
_cache.AddOrUpdate("test" + i, i, TimeSpan.FromMinutes(10));
MemoryCache.Default.Add("test" + i, i, _dtPlus10Mins);
}
}
/*
[Benchmark]
public void EvictExpired()
{
_cache.EvictExpired();
}
[Benchmark]
public void EvictExpired2()
{
_cache.EvictExpiredOptimized();
}
*/
[Benchmark]
public void DictionaryLookup()
{
_cache.TryGet("test123", out _);
_cache.TryGet("test234", out _);
_cache.TryGet("test673", out _);
_cache.TryGet("test987", out _);
}
[Benchmark]
public void FastCacheLookup()
{
_cache.TryGet("test123", out _);
_cache.TryGet("test234", out _);
_cache.TryGet("test673", out _);
_cache.TryGet("test987", out _);
}
[Benchmark]
public void MemoryCacheLookup()
{
var x = MemoryCache.Default["test123"];
x = MemoryCache.Default["test234"];
x = MemoryCache.Default["test673"];
x = MemoryCache.Default["test987"];
}
[Benchmark]
public void FastCacheGetOrAdd()
{
_cache.GetOrAdd("test123", 123, TimeSpan.FromSeconds(1));
_cache.GetOrAdd("test234", 124, TimeSpan.FromSeconds(1));
_cache.GetOrAdd("test673", 125, TimeSpan.FromSeconds(1));
_cache.GetOrAdd("test987", 126, TimeSpan.FromSeconds(1));
}
[Benchmark]
public void MemoryCacheGetOrAdd()
{
MemoryCache.Default.AddOrGetExisting("test123", 123, DateTime.UtcNow.AddSeconds(1));
MemoryCache.Default.AddOrGetExisting("test234", 124, DateTime.UtcNow.AddSeconds(1));
MemoryCache.Default.AddOrGetExisting("test673", 125, DateTime.UtcNow.AddSeconds(1));
MemoryCache.Default.AddOrGetExisting("test987", 126, DateTime.UtcNow.AddSeconds(1));
}
[Benchmark]
public void FastCacheAddRemove()
{
_cache.AddOrUpdate("1111", 42, TimeSpan.FromMinutes(10));
_cache.Remove("1111");
}
[Benchmark]
public void MemoryCacheAddRemove()
{
MemoryCache.Default.Add("1111", 42, _dtPlus10Mins);
MemoryCache.Default.Remove("1111");
}
}