Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add]: LevelDB Benchmarks #3667

Merged
merged 12 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions benchmarks/Neo.Benchmarks/Persistence/Bechmarks_LevelDB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2015-2025 The Neo Project.
//
// Bechmarks_LevelDB.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.Persistence;
using Neo.Plugins.Storage;
using Neo.SmartContract;
using System.Diagnostics;

namespace Neo.Benchmarks.Persistence.Benchmarks
{
public class Bechmarks_LevelDB
{
// avoid allocations in benchmarks
private static StorageKey key1;
private static readonly byte[] value = new UInt256().GetSpan().ToArray();

private const string PathLevelDB = "Data_LevelDB_Benchmarks";

private static readonly LevelDBStore levelDb = new();
private static ISnapshot snapshot;

[GlobalSetup]
public void Setup()
{
if (Directory.Exists(PathLevelDB))
Directory.Delete(PathLevelDB, true);

key1 = new KeyBuilder(1, 1).Add(new UInt160());

var levelDbStore = levelDb.GetStore(PathLevelDB);
snapshot = levelDbStore.GetSnapshot();
}

[GlobalCleanup]
public void Cleanup()
{
snapshot.Dispose();
levelDb.Dispose();
if (Directory.Exists(PathLevelDB))
Directory.Delete(PathLevelDB, true);
}

[Benchmark]
public void LevelDBSnapshotWrites()
{
snapshot.Put(key1.ToArray(), value);
snapshot.Delete(key1.ToArray());
}
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
BenchmarkRunner.Run<Benchmarks_Hash>();
BenchmarkRunner.Run<Benchmarks_StorageKey>();
BenchmarkRunner.Run<Bechmarks_ReadOnlyStoreView>();
BenchmarkRunner.Run<Bechmarks_LevelDB>();
7 changes: 4 additions & 3 deletions src/Neo/Persistence/MemorySnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

namespace Neo.Persistence
{
/// <summary>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class MemorySnapshot : ISnapshot
{
private readonly ConcurrentDictionary<byte[], byte[]> _innerData;
Expand Down Expand Up @@ -47,9 +50,7 @@ public void Delete(byte[] key)
_writeBatch[key] = null;
}

public void Dispose()
{
}
public void Dispose() { }

public void Put(byte[] key, byte[] value)
{
Expand Down
21 changes: 15 additions & 6 deletions src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ namespace Neo.Plugins.Storage
{
/// <summary>
/// <code>Iterating over the whole dataset can be time-consuming. Depending upon how large the dataset is.</code>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class Snapshot : ISnapshot, IEnumerable<KeyValuePair<byte[], byte[]>>
{
private readonly DB _db;
private readonly LSnapshot _snapshot;
private readonly ReadOptions _readOptions;
private readonly WriteBatch _batch;

#if NET9_0_OR_GREATER
private readonly System.Threading.Lock _lock = new();
#else
private readonly object _lock = new();
#endif

public Snapshot(DB db)
{
Expand All @@ -36,18 +42,27 @@ public Snapshot(DB db)
_batch = new WriteBatch();
}

/// <inheritdoc/>
public void Commit()
{
lock (_lock)
_db.Write(WriteOptions.Default, _batch);
}

/// <inheritdoc/>
public void Delete(byte[] key)
{
lock (_lock)
_batch.Delete(key);
}

/// <inheritdoc/>
public void Put(byte[] key, byte[] value)
{
lock (_lock)
_batch.Put(key, value);
}

public void Dispose()
{
_snapshot.Dispose();
Expand All @@ -60,12 +75,6 @@ public void Dispose()
return _db.Seek(_readOptions, keyOrPrefix, direction);
}

public void Put(byte[] key, byte[] value)
{
lock (_lock)
_batch.Put(key, value);
}

public bool Contains(byte[] key)
{
return _db.Contains(_readOptions, key);
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace Neo.Plugins.Storage
{
/// <summary>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class Snapshot : ISnapshot
{
private readonly RocksDb db;
Expand Down
Loading