-
Notifications
You must be signed in to change notification settings - Fork 713
Expand file tree
/
Copy pathWriteBatchBenchmark.cs
More file actions
281 lines (238 loc) · 9.56 KB
/
Copy pathWriteBatchBenchmark.cs
File metadata and controls
281 lines (238 loc) · 9.56 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Evm.State;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.State.Flat;
using Nethermind.State.Flat.Persistence;
using Nethermind.State.Flat.PersistedSnapshots;
using Nethermind.State.Flat.ScopeProvider;
using Nethermind.Trie;
using FlatSnapshot = Nethermind.State.Flat.Snapshot;
namespace Nethermind.Benchmarks.State;
[MemoryDiagnoser]
[WarmupCount(3)]
[MinIterationCount(3)]
[MaxIterationCount(10)]
public class WriteBatchBenchmark
{
private const int SnapshotCount = 1;
private FlatDbConfig _config = null!;
private ResourcePool _resourcePool = null!;
private List<FlatSnapshot> _baseSnapshots = null!;
private StateId _currentStateId;
private Address[] _addresses = null!;
private FlatWorldStateScope _scope = null!;
[Params(100, 500)]
public int AccountCount { get; set; }
[Params(100, 1000, 3000)]
public int StorageSlotsPerAccount { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_config = new FlatDbConfig();
_resourcePool = new ResourcePool(_config);
_baseSnapshots = new List<FlatSnapshot>(SnapshotCount);
_currentStateId = new StateId(0, Keccak.EmptyTreeHash);
int totalAccountCount = 0;
for (ulong block = 0; block < SnapshotCount; block++)
{
int accountCount = 500;
int storageAccountCount = 10;
int slotsPerStorageAccount = 50;
SnapshotPooledList prevSnapshots = new(_baseSnapshots.Count);
foreach (FlatSnapshot s in _baseSnapshots)
{
s.TryAcquire();
prevSnapshots.Add(s);
}
ReadOnlySnapshotBundle readOnly = new(
prevSnapshots, new NoopPersistenceReader(), recordDetailedMetrics: false,
PersistedSnapshotStack.Empty());
NullTrieNodeCache cache = new();
SnapshotBundle bundle = new(
readOnly, cache, _resourcePool, ResourcePool.Usage.MainBlockProcessing);
CapturingCommitTarget commitTarget = new();
FlatWorldStateScope scope = new(
currentStateId: _currentStateId,
snapshotBundle: bundle,
codeDb: new NullCodeDb(),
commitTarget: commitTarget,
configuration: _config,
trieCacheWarmer: new NoopTrieWarmer(),
logManager: NullLogManager.Instance);
int addressOffset = totalAccountCount;
Address[] addresses = new Address[accountCount];
Parallel.For(0, accountCount, i =>
{
addresses[i] = DeriveAddress(addressOffset + i + 1);
});
using (IWorldStateScopeProvider.IWorldStateWriteBatch batch =
scope.StartWriteBatch(accountCount))
{
IWorldStateScopeProvider.IStorageWriteBatch[] storageBatches =
new IWorldStateScopeProvider.IStorageWriteBatch[storageAccountCount];
for (int i = 0; i < accountCount; i++)
{
batch.Set(addresses[i], new Account(balance: (UInt256)(addressOffset + i + 1)));
if (i < storageAccountCount)
{
storageBatches[i] = batch.CreateStorageWriteBatch(addresses[i],
estimatedEntries: slotsPerStorageAccount);
}
}
int slots = slotsPerStorageAccount;
Parallel.For(0, storageAccountCount, i =>
{
IWorldStateScopeProvider.IStorageWriteBatch storageBatch = storageBatches[i];
for (int s = 0; s < slots; s++)
{
storageBatch.Set((UInt256)(ulong)(s + 1),
new byte[] { (byte)((s + 1) & 0xFF) });
}
storageBatch.Dispose();
});
}
scope.Commit(blockNumber: block + 1);
FlatSnapshot snapshot = commitTarget.LastSnapshot
?? throw new InvalidOperationException(
$"Block {block + 1}: Commit produced no snapshot");
snapshot.TryAcquire();
_baseSnapshots.Add(snapshot);
_currentStateId = new StateId(block + 1, scope.RootHash);
totalAccountCount += accountCount;
}
// Pre-compute addresses for benchmark iterations
_addresses = new Address[AccountCount];
Parallel.For(0, AccountCount, i =>
{
_addresses[i] = DeriveAddress(totalAccountCount + i + 1);
});
}
[IterationSetup]
public void IterationSetup()
{
SnapshotPooledList prevSnapshots = new(_baseSnapshots.Count);
foreach (FlatSnapshot s in _baseSnapshots)
{
s.TryAcquire();
prevSnapshots.Add(s);
}
ReadOnlySnapshotBundle readOnly = new(
prevSnapshots, new NoopPersistenceReader(), recordDetailedMetrics: false,
PersistedSnapshotStack.Empty());
NullTrieNodeCache cache = new();
SnapshotBundle bundle = new(
readOnly, cache, _resourcePool, ResourcePool.Usage.MainBlockProcessing);
CapturingCommitTarget commitTarget = new();
_scope = new FlatWorldStateScope(
currentStateId: _currentStateId,
snapshotBundle: bundle,
codeDb: new NullCodeDb(),
commitTarget: commitTarget,
configuration: _config,
trieCacheWarmer: new NoopTrieWarmer(),
logManager: NullLogManager.Instance);
}
[IterationCleanup]
public void IterationCleanup()
{
_scope?.Dispose();
_scope = null!;
}
[Benchmark]
public void BatchWriteAccount()
{
using IWorldStateScopeProvider.IWorldStateWriteBatch batch =
_scope.StartWriteBatch(AccountCount);
for (int i = 0; i < AccountCount; i++)
{
batch.Set(_addresses[i], new Account(balance: (UInt256)(ulong)(i + 1)));
}
}
[Benchmark]
public void BatchWriteStorage()
{
using IWorldStateScopeProvider.IWorldStateWriteBatch batch =
_scope.StartWriteBatch(AccountCount);
for (int i = 0; i < AccountCount; i++)
{
batch.Set(_addresses[i], new Account(balance: (UInt256)(ulong)(i + 1)));
using IWorldStateScopeProvider.IStorageWriteBatch storageBatch =
batch.CreateStorageWriteBatch(_addresses[i], estimatedEntries: StorageSlotsPerAccount);
for (int s = 0; s < StorageSlotsPerAccount; s++)
{
storageBatch.Set((UInt256)(ulong)(s + 1),
new byte[] { (byte)((s + 1) & 0xFF) });
}
}
}
[Benchmark]
public void ParallelBatchWriteStorage()
{
using IWorldStateScopeProvider.IWorldStateWriteBatch batch =
_scope.StartWriteBatch(AccountCount);
// Phase 1 (sequential): set accounts and create storage batches
IWorldStateScopeProvider.IStorageWriteBatch[] storageBatches =
new IWorldStateScopeProvider.IStorageWriteBatch[AccountCount];
for (int i = 0; i < AccountCount; i++)
{
batch.Set(_addresses[i], new Account(balance: (UInt256)(ulong)(i + 1)));
storageBatches[i] = batch.CreateStorageWriteBatch(_addresses[i],
estimatedEntries: StorageSlotsPerAccount);
}
// Phase 2 (parallel): fill storage slots
int slots = StorageSlotsPerAccount;
Parallel.For(0, AccountCount, i =>
{
IWorldStateScopeProvider.IStorageWriteBatch storageBatch = storageBatches[i];
for (int s = 0; s < slots; s++)
{
storageBatch.Set((UInt256)(ulong)(s + 1),
new byte[] { (byte)((s + 1) & 0xFF) });
}
storageBatch.Dispose();
});
}
private static Address DeriveAddress(int index) =>
new(Keccak.Compute(Address.FromNumber((UInt256)(ulong)index).Bytes));
private sealed class NullTrieNodeCache : ITrieNodeCache
{
public bool TryGet(Hash256 address, in TreePath path, Hash256 hash, out TrieNode node)
{
node = null;
return false;
}
public void Add(TransientResource transientResource) { }
public void Clear() { }
}
private sealed class CapturingCommitTarget : IFlatCommitTarget
{
public FlatSnapshot LastSnapshot { get; private set; }
public TransientResource LastResource { get; private set; }
public void AddSnapshot(FlatSnapshot snapshot, TransientResource transientResource)
{
LastSnapshot = snapshot;
LastResource = transientResource;
}
}
private sealed class NullCodeDb : IWorldStateScopeProvider.ICodeDb
{
public byte[] GetCode(in ValueHash256 codeHash) => null;
public IWorldStateScopeProvider.ICodeSetter BeginCodeWrite()
=> NullCodeSetter.Instance;
private sealed class NullCodeSetter : IWorldStateScopeProvider.ICodeSetter
{
public static readonly NullCodeSetter Instance = new();
public void Set(in ValueHash256 codeHash, ReadOnlySpan<byte> code) { }
public void Dispose() { }
}
}
}