-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockFreeBenchmarks.cs
More file actions
180 lines (155 loc) · 5.31 KB
/
Copy pathLockFreeBenchmarks.cs
File metadata and controls
180 lines (155 loc) · 5.31 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
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
using BenchmarkDotNet.Attributes;
using Store = SharedMemoryStore.MemoryStore;
namespace SharedMemoryStore.Benchmarks;
/// <summary>
/// Benchmarks for the sole SMS2 lock-free engine. Keys and buffers are prepared during setup so the
/// MemoryDiagnoser reports library-path allocations rather than harness noise.
/// </summary>
[MemoryDiagnoser]
public class LockFreeBenchmarks
{
private const int SlotCount = 64;
private const int PayloadBytes = 256;
private const int PrimaryBucketCount = 64;
private Store _store = null!;
private byte[][] _keys = null!;
private byte[][] _collidingKeys = null!;
private byte[] _payload = null!;
private int _cursor;
[GlobalSetup]
public void Setup()
{
string name = $"sms-lock-free-benchmark-{Guid.NewGuid():N}";
SharedMemoryStoreOptions options = SharedMemoryStoreOptions.Create(
name,
SlotCount,
PayloadBytes,
maxDescriptorBytes: 8,
maxKeyBytes: 8,
leaseRecordCount: 128,
participantRecordCount: 16,
openMode: OpenMode.CreateNew,
enableLeaseRecovery: true);
StoreOpenStatus open = Store.TryCreateOrOpen(options, out Store? store);
if (open != StoreOpenStatus.Success || store is null)
{
throw new InvalidOperationException($"Cannot open SMS2 benchmark store: {open}.");
}
_store = store;
_keys = Enumerable.Range(0, SlotCount).Select(BitConverter.GetBytes).ToArray();
_payload = new byte[PayloadBytes];
for (var index = 0; index < _payload.Length; index++)
{
_payload[index] = unchecked((byte)(index * 31));
}
_collidingKeys = FindCollidingKeys(9);
for (var index = 0; index < 8; index++)
{
Ensure(_store.TryPublish(_collidingKeys[index], _payload));
}
Ensure(_store.TryPublish(_keys[0], _payload));
}
[GlobalCleanup]
public void Cleanup()
{
_store.Dispose();
}
[Benchmark(Baseline = true)]
public StoreStatus AcquireProjectRelease()
{
StoreStatus acquire = _store.TryAcquire(_keys[0], out ValueLease lease);
if (acquire != StoreStatus.Success)
{
return acquire;
}
_ = lease.ValueSpan[PayloadBytes - 1];
return lease.Release();
}
[Benchmark]
public StoreStatus PublishRemove()
{
int index = 1 + (Interlocked.Increment(ref _cursor) & 31);
byte[] key = _keys[index];
StoreStatus publish = _store.TryPublish(key, _payload);
return publish == StoreStatus.Success ? _store.TryRemove(key) : publish;
}
[Benchmark]
public StoreStatus ZeroCopyReserveCommitRemove()
{
int index = 33 + (Interlocked.Increment(ref _cursor) & 15);
byte[] key = _keys[index];
StoreStatus reserve = _store.TryReserve(key, PayloadBytes, [], out ValueReservation reservation);
if (reserve != StoreStatus.Success)
{
return reserve;
}
_payload.CopyTo(reservation.GetSpan());
StoreStatus advance = reservation.Advance(PayloadBytes);
if (advance != StoreStatus.Success)
{
_ = reservation.Abort();
return advance;
}
StoreStatus commit = reservation.Commit();
return commit == StoreStatus.Success ? _store.TryRemove(key) : commit;
}
[Benchmark]
public StoreStatus CollisionOverflowPublishRemove()
{
byte[] key = _collidingKeys[8];
StoreStatus publish = _store.TryPublish(key, _payload);
return publish == StoreStatus.Success ? _store.TryRemove(key) : publish;
}
[Benchmark]
public StoreStatus CurrentOwnerLeaseRecovery()
{
int index = 49 + (Interlocked.Increment(ref _cursor) & 7);
byte[] key = _keys[index];
StoreStatus publish = _store.TryPublish(key, _payload);
if (publish != StoreStatus.Success)
{
return publish;
}
StoreStatus acquire = _store.TryAcquire(key, out _);
if (acquire != StoreStatus.Success)
{
_ = _store.TryRemove(key);
return acquire;
}
StoreStatus recovery = _store.TryRecoverLeases(
new LeaseRecoveryOptions(RecoverCurrentProcessLeases: true),
out _);
StoreStatus remove = _store.TryRemove(key);
return recovery == StoreStatus.Success ? remove : recovery;
}
private static byte[][] FindCollidingKeys(int count)
{
var keys = new List<byte[]>(count);
for (long candidate = 0; keys.Count < count; candidate++)
{
byte[] key = BitConverter.GetBytes(candidate);
if ((Hash(key) & (PrimaryBucketCount - 1)) == 0)
{
keys.Add(key);
}
}
return keys.ToArray();
}
private static ulong Hash(ReadOnlySpan<byte> key)
{
ulong hash = 14_695_981_039_346_656_037UL;
foreach (byte value in key)
{
hash ^= value;
hash *= 1_099_511_628_211UL;
}
return hash;
}
private static void Ensure(StoreStatus status)
{
if (status != StoreStatus.Success)
{
throw new InvalidOperationException("Benchmark setup failed: " + status);
}
}
}