-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameThroughputBenchmarks.cs
More file actions
84 lines (72 loc) · 2.58 KB
/
Copy pathFrameThroughputBenchmarks.cs
File metadata and controls
84 lines (72 loc) · 2.58 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
using BenchmarkDotNet.Attributes;
using System.Diagnostics;
using Store = SharedMemoryStore.MemoryStore;
namespace SharedMemoryStore.Benchmarks;
[MemoryDiagnoser]
public class FrameThroughputBenchmarks
{
private Store _store = null!;
private readonly byte[] _key = [1];
private readonly byte[] _descriptor = [1, 0, 0, 0];
private byte[] _payload = null!;
[GlobalSetup]
public void Setup()
{
_payload = new byte[BenchmarkEnvironment.FramePayloadBytes];
for (var i = 0; i < _payload.Length; i++)
{
_payload[i] = (byte)(i % 251);
}
_store = BenchmarkStoreFactory.Create(slotCount: 2, maxValueBytes: _payload.Length, maxDescriptorBytes: _descriptor.Length);
}
[GlobalCleanup]
public void Cleanup() => _store.Dispose();
[Benchmark]
public StoreStatus FramePublishRemove()
{
var publish = _store.TryPublish(_key, _payload, _descriptor);
var remove = _store.TryRemove(_key);
return publish == StoreStatus.Success ? remove : publish;
}
[Benchmark]
public FrameThroughputValidationResult SustainedFramePublishRemoveForSixtySeconds()
{
return RunSustainedValidation(TimeSpan.FromSeconds(BenchmarkEnvironment.FrameThroughputDurationSeconds));
}
private FrameThroughputValidationResult RunSustainedValidation(TimeSpan duration)
{
var stopwatch = Stopwatch.StartNew();
long publishes = 0;
var status = StoreStatus.Success;
while (stopwatch.Elapsed < duration)
{
status = _store.TryPublish(_key, _payload, _descriptor);
if (status != StoreStatus.Success)
{
break;
}
publishes++;
status = _store.TryRemove(_key);
if (status != StoreStatus.Success)
{
break;
}
}
stopwatch.Stop();
var publishesPerSecond = publishes / Math.Max(stopwatch.Elapsed.TotalSeconds, 0.001);
return new FrameThroughputValidationResult(
publishes,
publishesPerSecond,
BenchmarkEnvironment.TargetFramePublishesPerSecond,
BenchmarkEnvironment.FrameThroughputDurationSeconds,
status,
publishesPerSecond >= BenchmarkEnvironment.TargetFramePublishesPerSecond && status == StoreStatus.Success);
}
}
public readonly record struct FrameThroughputValidationResult(
long PublishCount,
double PublishesPerSecond,
int TargetPublishesPerSecond,
int DurationSeconds,
StoreStatus FinalStatus,
bool Passed);