-
Notifications
You must be signed in to change notification settings - Fork 1k
/
VersionVectorBenchmark.cs
181 lines (155 loc) · 6.72 KB
/
VersionVectorBenchmark.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//-----------------------------------------------------------------------
// <copyright file="VersionVectorBenchmark.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Akka.Benchmarks.Configurations;
using Akka.Cluster;
using Akka.DistributedData;
using BenchmarkDotNet.Attributes;
using FluentAssertions;
using static Akka.DistributedData.VersionVector;
namespace Akka.Benchmarks.DData
{
[Config(typeof(MicroBenchmarkConfig))]
public class VersionVectorBenchmarks
{
[Params(100)]
public int ClockSize;
[Params(1000)]
public int Iterations;
internal (VersionVector clock, ImmutableSortedSet<UniqueAddress> nodes) CreateVectorClockOfSize(int size)
{
UniqueAddress GenerateUniqueAddress(int nodeCount){
return new UniqueAddress(new Akka.Actor.Address("akka.tcp", "ClusterSys", "localhost", nodeCount), nodeCount);
}
return Enumerable.Range(1, size)
.Aggregate((VersionVector.Empty, ImmutableSortedSet<UniqueAddress>.Empty),
(tuple, i) =>
{
var (vc, nodes) = tuple;
var node = GenerateUniqueAddress(i);
return (vc.Increment(node), nodes.Add(node));
});
}
internal VersionVector CopyVectorClock(VersionVector vc)
{
var versions = ImmutableDictionary<UniqueAddress, long>.Empty;
var enumerator = vc.VersionEnumerator;
while(enumerator.MoveNext()){
var nodePair = enumerator.Current;
versions = versions.SetItem(nodePair.Key, nodePair.Value);
}
return VersionVector.Create(versions);
}
private UniqueAddress _firstNode;
private UniqueAddress _lastNode;
private UniqueAddress _middleNode;
private ImmutableSortedSet<UniqueAddress> _nodes;
private VersionVector _vcBefore;
private VersionVector _vcBaseLast;
private VersionVector _vcAfterLast;
private VersionVector _vcConcurrentLast;
private VersionVector _vcBaseMiddle;
private VersionVector _vcAfterMiddle;
private VersionVector _vcConcurrentMiddle;
[GlobalSetup]
public void Setup()
{
var (vcBefore, nodes) = CreateVectorClockOfSize(ClockSize);
_vcBefore = vcBefore;
_nodes = nodes;
_firstNode = nodes.First();
_lastNode = nodes.Last();
_middleNode = nodes[ClockSize / 2];
_vcBaseLast = vcBefore.Increment(_lastNode);
_vcAfterLast = _vcBaseLast.Increment(_firstNode);
_vcConcurrentLast = _vcBaseLast.Increment(_lastNode);
_vcBaseMiddle = _vcBefore.Increment(_middleNode);
_vcAfterMiddle = _vcBaseMiddle.Increment(_firstNode);
_vcConcurrentMiddle = _vcBaseMiddle.Increment(_middleNode);
}
private void CheckThunkFor(VersionVector vc1, VersionVector vc2, Action<VersionVector, VersionVector> thunk, int times)
{
var vcc1 = CopyVectorClock(vc1);
var vcc2 = CopyVectorClock(vc2);
for (var i = 0; i < times; i++)
{
thunk(vcc1, vcc2);
}
}
private void CompareTo(VersionVector vc1, VersionVector vc2, Ordering ordering)
{
vc1.Compare(vc2).Should().Be(ordering);
}
private void NotEqual(VersionVector vc1, VersionVector vc2)
{
(vc1 == vc2).Should().BeFalse();
}
private void Merge(VersionVector vc1, VersionVector vc2)
{
vc1.Merge(vc2);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_same()
{
CheckThunkFor(_vcBaseLast, _vcBaseLast, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.Same), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_Before_last()
{
CheckThunkFor(_vcBefore, _vcBaseLast, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.Before), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_After_last()
{
CheckThunkFor(_vcAfterLast, _vcBaseLast, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.After), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_Concurrent_last()
{
CheckThunkFor(_vcAfterLast, _vcConcurrentLast, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.Concurrent), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_Before_middle()
{
CheckThunkFor(_vcBefore, _vcBaseMiddle, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.Before), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_After_middle()
{
CheckThunkFor(_vcAfterMiddle, _vcBaseMiddle, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.After), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_Concurrent_middle()
{
CheckThunkFor(_vcAfterMiddle, _vcConcurrentMiddle, (clock, vectorClock) => CompareTo(clock, vectorClock, Ordering.Concurrent), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_notEquals_Before_Middle()
{
CheckThunkFor(_vcBefore, _vcBaseMiddle, (clock, vectorClock) => NotEqual(clock, vectorClock), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_notEquals_After_Middle()
{
CheckThunkFor(_vcAfterMiddle, _vcBaseMiddle, (clock, vectorClock) => NotEqual(clock, vectorClock), Iterations);
}
[Benchmark]
public void VectorClock_comparisons_should_compare_notEquals_Concurrent_Middle()
{
CheckThunkFor(_vcAfterMiddle, _vcConcurrentMiddle, (clock, vectorClock) => NotEqual(clock, vectorClock), Iterations);
}
[Benchmark]
public void VersionVector_merge_Multi_Multi()
{
CheckThunkFor(_vcBefore, _vcAfterLast, (vector, versionVector) => Merge(vector, versionVector), Iterations);
}
}
}