-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectIngestAllocationBenchmarks.cs
More file actions
88 lines (75 loc) · 2.71 KB
/
Copy pathDirectIngestAllocationBenchmarks.cs
File metadata and controls
88 lines (75 loc) · 2.71 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
using BenchmarkDotNet.Attributes;
using Store = SharedMemoryStore.MemoryStore;
namespace SharedMemoryStore.Benchmarks;
[MemoryDiagnoser]
public class DirectIngestAllocationBenchmarks
{
private Store _store = null!;
private readonly byte[] _key = [1];
private readonly byte[] _descriptor = [9, 8, 7, 6];
private int _payloadLength;
[GlobalSetup]
public void Setup()
{
_payloadLength = BenchmarkEnvironment.FramePayloadBytes;
_store = BenchmarkStoreFactory.Create(slotCount: 2, maxValueBytes: _payloadLength, maxDescriptorBytes: _descriptor.Length);
}
[GlobalCleanup]
public void Cleanup() => _store.Dispose();
[Benchmark]
public StoreStatus ReserveFillCommitRemove()
{
var status = _store.TryReserve(_key, _payloadLength, _descriptor, out var reservation);
if (status != StoreStatus.Success)
{
return status;
}
reservation.GetSpan(_payloadLength).Fill(0x5A);
status = reservation.Advance(_payloadLength);
if (status != StoreStatus.Success)
{
_ = reservation.Abort();
return status;
}
status = reservation.Commit();
return status == StoreStatus.Success ? _store.TryRemove(_key) : status;
}
[Benchmark]
public DirectIngestAllocationValidationResult ValidateOneHundredThousandFramesAllocation()
{
_ = ReserveFillCommitRemove();
_ = ReserveFillCommitRemove();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var before = GC.GetAllocatedBytesForCurrentThread();
var status = StoreStatus.Success;
var completedFrames = 0;
for (; completedFrames < BenchmarkEnvironment.DirectIngestAllocationFrames; completedFrames++)
{
status = ReserveFillCommitRemove();
if (status != StoreStatus.Success)
{
break;
}
}
var totalAllocatedBytes = GC.GetAllocatedBytesForCurrentThread() - before;
var allocatedBytesPerFrame = completedFrames == 0
? totalAllocatedBytes
: totalAllocatedBytes / (double)completedFrames;
return new DirectIngestAllocationValidationResult(
completedFrames,
totalAllocatedBytes,
allocatedBytesPerFrame,
status,
completedFrames == BenchmarkEnvironment.DirectIngestAllocationFrames
&& totalAllocatedBytes == 0
&& status == StoreStatus.Success);
}
}
public readonly record struct DirectIngestAllocationValidationResult(
int FrameCount,
long TotalAllocatedBytes,
double AllocatedBytesPerFrame,
StoreStatus FinalStatus,
bool Passed);