-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concurrent_Tests.cs
166 lines (152 loc) · 5.93 KB
/
Concurrent_Tests.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
using BenchmarkDotNet.Attributes;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Security.Cryptography;
using wan24.Core;
namespace Wan24_Core_Benchmark_Tests
{
[MemoryDiagnoser]
public class Concurrent_Tests
{
private static readonly ParallelOptions ParallelOptions = new() { MaxDegreeOfParallelism = Environment.ProcessorCount };
private static readonly Dictionary<int, bool> HugeDict;
private static readonly ConcurrentDictionary<int, bool> HugeConcurrentDict;
private static readonly ConcurrentLockDictionary<int, bool> HugeConcurrentLockDict;
private static readonly AsyncConcurrentDictionary<int, bool> HugeAsyncConcurrentDict;
private static readonly ImmutableArray<int> RandomIndex;
static Concurrent_Tests()
{
HugeDict = new(ushort.MaxValue << 3);
for (int i = 0; i < ushort.MaxValue << 3; HugeDict[i] = true, i++) ;
HugeConcurrentDict = new(Environment.ProcessorCount, ushort.MaxValue << 3);
for (int i = 0; i < ushort.MaxValue << 3; HugeConcurrentDict[i] = true, i++) ;
HugeConcurrentLockDict = new(ushort.MaxValue << 3);
for (int i = 0; i < ushort.MaxValue << 3; HugeConcurrentLockDict[i] = true, i++) ;
HugeAsyncConcurrentDict = new(Enumerable.Range(0, ushort.MaxValue << 3).Select(i => new KeyValuePair<int, bool>(i, true)));
RandomIndex = [.. Enumerable.Range(0, ushort.MaxValue << 3).OrderBy(i => RandomNumberGenerator.GetInt32(int.MaxValue))];
}
[Benchmark]
public void Dictionary()
{
Dictionary<int, bool> dict = [];
Parallel.For(0, ushort.MaxValue, ParallelOptions, i =>
{
lock (dict) dict[0] = !dict.TryGetValue(0, out bool value) || !value;
});
}
[Benchmark]
public void ConcurrentDictionary()
{
ConcurrentDictionary<int, bool> dict = [];
Parallel.For(
0,
ushort.MaxValue,
ParallelOptions,
i => dict.AddOrUpdate(0, key => true, (key, value) => !value)
);
}
[Benchmark]
public void ConcurrentLockDictionary()
{
ConcurrentLockDictionary<int, bool> dict = [];
Parallel.For(
0,
ushort.MaxValue,
ParallelOptions,
i => dict.AddOrUpdate(0, key => true, (key, value) => !value)
);
}
[Benchmark]
public async Task AsyncConcurrentDictionary()
{
AsyncConcurrentDictionary<int, bool> dict = new();
await Parallel.ForAsync(
0,
ushort.MaxValue,
ParallelOptions,
async (i, ct) => await dict.AddOrUpdateAsync(0, key => true, (key, value) => !value, ct).DynamicContext()
);
}
[Benchmark]
public void DictionaryManyItems()
{
Dictionary<int, bool> dict = HugeDict;
Parallel.For(0, ushort.MaxValue << 3, ParallelOptions, i =>
{
lock (dict) dict[i] = !dict.TryGetValue(i, out bool value) || !value;
});
}
[Benchmark]
public void ConcurrentDictionaryManyItems()
{
ConcurrentDictionary<int, bool> dict = HugeConcurrentDict;
Parallel.For(
0,
ushort.MaxValue << 3,
ParallelOptions,
i => dict.AddOrUpdate(i, key => true, (key, value) => !value)
);
}
[Benchmark]
public void ConcurrentLockDictionaryManyItems()
{
ConcurrentLockDictionary<int, bool> dict = HugeConcurrentLockDict;
Parallel.For(
0,
ushort.MaxValue << 3,
ParallelOptions,
i => dict.AddOrUpdate(i, key => true, (key, value) => !value)
);
}
[Benchmark]
public async Task AsyncConcurrentDictionaryManyItems()
{
AsyncConcurrentDictionary<int, bool> dict = HugeAsyncConcurrentDict;
await Parallel.ForAsync(
0,
ushort.MaxValue << 3,
ParallelOptions,
async (i, ct) => await dict.AddOrUpdateAsync(i, key => true, (key, value) => !value, ct).DynamicContext()
).DynamicContext();
}
[Benchmark]
public void DictionaryManyItemsRnd()
{
Dictionary<int, bool> dict = HugeDict;
Parallel.ForEach(RandomIndex, ParallelOptions, i =>
{
lock (dict) dict[i] = !dict.TryGetValue(i, out bool value) || !value;
});
}
[Benchmark]
public void ConcurrentDictionaryManyItemsRnd()
{
ConcurrentDictionary<int, bool> dict = HugeConcurrentDict;
Parallel.ForEach(
RandomIndex,
ParallelOptions,
i => dict.AddOrUpdate(i, key => true, (key, value) => !value)
);
}
[Benchmark]
public void ConcurrentLockDictionaryManyItemsRnd()
{
ConcurrentLockDictionary<int, bool> dict = HugeConcurrentLockDict;
Parallel.ForEach(
RandomIndex,
ParallelOptions,
i => dict.AddOrUpdate(i, key => true, (key, value) => !value)
);
}
[Benchmark]
public async Task AsyncConcurrentDictionaryManyItemsRnd()
{
AsyncConcurrentDictionary<int, bool> dict = HugeAsyncConcurrentDict;
await Parallel.ForEachAsync(
RandomIndex,
ParallelOptions,
async (i, ct) => await dict.AddOrUpdateAsync(i, key => true, (key, value) => !value, ct).DynamicContext()
).DynamicContext();
}
}
}