Skip to content

Commit

Permalink
Cleanup BDN organization (#737)
Browse files Browse the repository at this point in the history
* Cleanup BDN organization
  • Loading branch information
badrishc authored Oct 21, 2024
1 parent 01f17f4 commit 428acbb
Show file tree
Hide file tree
Showing 30 changed files with 613 additions and 907 deletions.
10 changes: 5 additions & 5 deletions benchmark/BDN.benchmark/BDN.benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\playground\Embedded.perftest\EmbeddedRespServer.cs" Link="EmbeddedRespServer.cs" />
<Compile Include="..\..\playground\Embedded.perftest\DummyNetworkSender.cs" Link="DummyNetworkSender.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictObject.cs" Link="MyDictObject.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictSet.cs" Link="MyDictSet.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictGet.cs" Link="MyDictGet.cs" />
<Compile Include="..\..\playground\Embedded.perftest\EmbeddedRespServer.cs" Link="Utils\EmbeddedRespServer.cs" />
<Compile Include="..\..\playground\Embedded.perftest\DummyNetworkSender.cs" Link="Utils\DummyNetworkSender.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictObject.cs" Link="CustomProcs\MyDictObject.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictSet.cs" Link="CustomProcs\MyDictSet.cs" />
<Compile Include="..\..\main\GarnetServer\Extensions\MyDictGet.cs" Link="CustomProcs\MyDictGet.cs" />
</ItemGroup>

</Project>
46 changes: 18 additions & 28 deletions benchmark/BDN.benchmark/Cluster/ClusterContext.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using BDN.benchmark.CustomProcs;
using Embedded.perftest;
using Garnet.common;
using Garnet.server;

namespace BDN.benchmark.Cluster
{
public unsafe struct Request
{
public byte[] buffer;
public byte* ptr;

public Request(int size)
{
buffer = GC.AllocateArray<byte>(size, pinned: true);
ptr = (byte*)Unsafe.AsPointer(ref buffer[0]);
}
}

public unsafe class ClusterContext
unsafe class ClusterContext
{
EmbeddedRespServer server;
RespServerSession session;
BenchUtils benchUtils = new();

private readonly int Port = 7000;
readonly BenchUtils benchUtils = new();
readonly int port = 7000;

public ReadOnlySpan<byte> keyTag => "{0}"u8;
public static ReadOnlySpan<byte> keyTag => "{0}"u8;
public Request[] singleGetSet;
public Request[] singleMGetMSet;
public Request singleCPBSET;
Expand All @@ -40,18 +28,20 @@ public void Dispose()
server.Dispose();
}

public void SetupSingleInstance(bool enableCluster = true)
public void SetupSingleInstance(bool disableSlotVerification = false)
{
var opt = new GarnetServerOptions
{
QuietMode = true,
EnableCluster = enableCluster,
Port = Port,
EnableCluster = !disableSlotVerification,
Port = port,
CleanClusterConfig = true,
};
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
opt.CheckpointDir = "/tmp";
server = new EmbeddedRespServer(opt);
session = server.GetRespSession();
server.Register.NewTransactionProc(CustomProcSetBench.CommandName, () => new CustomProcSetBench(), new RespCommandsInfo { Arity = CustomProcSetBench.Arity });
_ = server.Register.NewTransactionProc(CustomProcSet.CommandName, () => new CustomProcSet(), new RespCommandsInfo { Arity = CustomProcSet.Arity });
}

public void AddSlotRange(List<(int, int)> slotRanges)
Expand All @@ -66,7 +56,7 @@ public void AddSlotRange(List<(int, int)> slotRanges)
}
}

public void CreateGetSet(int keySize = 8, int valueSize = 32, int batchSize = 128)
public void CreateGetSet(int keySize = 8, int valueSize = 32, int batchSize = 100)
{
var pairs = new (byte[], byte[])[batchSize];
for (var i = 0; i < batchSize; i++)
Expand All @@ -78,7 +68,7 @@ public void CreateGetSet(int keySize = 8, int valueSize = 32, int batchSize = 12
benchUtils.RandomBytes(ref pairs[i].Item2);
}

var setByteCount = batchSize * ("*2\r\n$3\r\nSET\r\n"u8.Length + (1 + NumUtils.NumDigits(keySize) + 2 + keySize + 2) + (1 + NumUtils.NumDigits(valueSize) + 2 + valueSize + 2));
var setByteCount = batchSize * ("*2\r\n$3\r\nSET\r\n"u8.Length + 1 + NumUtils.NumDigits(keySize) + 2 + keySize + 2 + 1 + NumUtils.NumDigits(valueSize) + 2 + valueSize + 2);
var setReq = new Request(setByteCount);
var curr = setReq.ptr;
var end = curr + setReq.buffer.Length;
Expand All @@ -103,7 +93,7 @@ public void CreateGetSet(int keySize = 8, int valueSize = 32, int batchSize = 12
singleGetSet = [getReq, setReq];
}

public void CreateMGetMSet(int keySize = 8, int valueSize = 32, int batchSize = 128)
public void CreateMGetMSet(int keySize = 8, int valueSize = 32, int batchSize = 100)
{
var pairs = new (byte[], byte[])[batchSize];
for (var i = 0; i < batchSize; i++)
Expand All @@ -127,14 +117,14 @@ public void CreateMGetMSet(int keySize = 8, int valueSize = 32, int batchSize =
for (var i = 0; i < batchSize; i++)
_ = RespWriteUtils.WriteBulkString(pairs[i].Item1, ref curr, end);

var mSetHeaderSize = 1 + NumUtils.NumDigits(1 + batchSize * 2) + 2 + "$4\r\nMSET\r\n"u8.Length;
var mSetHeaderSize = 1 + NumUtils.NumDigits(1 + (batchSize * 2)) + 2 + "$4\r\nMSET\r\n"u8.Length;
var setRespSize = 1 + NumUtils.NumDigits(keySize) + 2 + keySize + 2 + 1 + NumUtils.NumDigits(valueSize) + 2 + valueSize + 2;
var mSetByteCount = mSetHeaderSize + (batchSize * setRespSize);
var mSetReq = new Request(mSetByteCount);

curr = mSetReq.ptr;
end = curr + mSetReq.buffer.Length;
_ = RespWriteUtils.WriteArrayLength(1 + batchSize * 2, ref curr, end);
_ = RespWriteUtils.WriteArrayLength(1 + (batchSize * 2), ref curr, end);
_ = RespWriteUtils.WriteBulkString("MSET"u8, ref curr, end);
for (var i = 0; i < batchSize; i++)
{
Expand All @@ -144,7 +134,7 @@ public void CreateMGetMSet(int keySize = 8, int valueSize = 32, int batchSize =
singleMGetMSet = [mGetReq, mSetReq];
}

public void CreateCPBSET(int keySize = 8, int valueSize = 32, int batchSize = 128)
public void CreateCPBSET(int keySize = 8, int batchSize = 100)
{
var keys = new byte[8][];
for (var i = 0; i < 8; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

namespace BDN.benchmark.Cluster
{
/// <summary>
/// Cluster migrate benchmark
/// </summary>
[MemoryDiagnoser]
public unsafe class RespClusterMigrateBench
public unsafe class ClusterMigrate
{
ClusterContext cc;

[GlobalSetup]
public void GlobalSetup()
{
Expand Down Expand Up @@ -44,7 +48,7 @@ public void GlobalSetup()
cc.Consume(req, gossipReq.Length);

// Set slot to migrating state
var slot = HashSlotUtils.HashSlot(cc.keyTag.ToArray());
var slot = HashSlotUtils.HashSlot(ClusterContext.keyTag.ToArray());
SetSlot(slot, "MIGRATING", config.LocalNodeId);
}

Expand All @@ -58,12 +62,12 @@ private void SetSlot(int slot, string state, string nodeId)
var setSlotReq = new Request(reqBytes);
var curr = setSlotReq.ptr;
var end = curr + setSlotReq.buffer.Length;
RespWriteUtils.WriteArrayLength(5, ref curr, end);
RespWriteUtils.WriteBulkString("CLUSTER"u8, ref curr, end);
RespWriteUtils.WriteBulkString("SETSLOT"u8, ref curr, end);
RespWriteUtils.WriteIntegerAsBulkString(slot, ref curr, end);
RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(state), ref curr, end);
RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(nodeId), ref curr, end);
_ = RespWriteUtils.WriteArrayLength(5, ref curr, end);
_ = RespWriteUtils.WriteBulkString("CLUSTER"u8, ref curr, end);
_ = RespWriteUtils.WriteBulkString("SETSLOT"u8, ref curr, end);
_ = RespWriteUtils.WriteIntegerAsBulkString(slot, ref curr, end);
_ = RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(state), ref curr, end);
_ = RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(nodeId), ref curr, end);
cc.Consume(setSlotReq.ptr, setSlotReq.buffer.Length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@

namespace BDN.benchmark.Cluster
{
/// <summary>
/// Cluster operations benchmark
/// </summary>
[MemoryDiagnoser]
public unsafe class RespClusterBench
public unsafe class ClusterOperations
{
protected bool enableCluster = true;
/// <summary>
/// Cluster parameters
/// </summary>
[ParamsSource(nameof(ClusterParamsProvider))]
public ClusterParams Params { get; set; }

/// <summary>
/// Cluster parameters provider
/// </summary>
public IEnumerable<ClusterParams> ClusterParamsProvider()
{
yield return new(false);
yield return new(true);
}

ClusterContext cc;

[GlobalSetup]
public virtual void GlobalSetup()
{
cc = new ClusterContext();
cc.SetupSingleInstance(enableCluster);
cc.SetupSingleInstance(Params.disableSlotVerification);
cc.AddSlotRange([(0, 16383)]);
cc.CreateGetSet();
cc.CreateMGetMSet();
Expand Down
38 changes: 38 additions & 0 deletions benchmark/BDN.benchmark/Cluster/ClusterParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace BDN.benchmark.Cluster
{
/// <summary>
/// Cluster parameters
/// </summary>
public struct ClusterParams
{
/// <summary>
/// Whether to disable slot verification
/// </summary>
public bool disableSlotVerification;

/// <summary>
/// Constructor
/// </summary>
public ClusterParams(bool disableSlotVerification)
{
this.disableSlotVerification = disableSlotVerification;
}

/// <summary>
/// String representation
/// </summary>
public override string ToString()
{
if (!disableSlotVerification)
return "None";

var ret = "";
if (disableSlotVerification)
ret += "DSV";
return ret;
}
}
}
23 changes: 23 additions & 0 deletions benchmark/BDN.benchmark/Cluster/Request.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Runtime.CompilerServices;

namespace BDN.benchmark.Cluster
{
/// <summary>
/// Request struct
/// </summary>
unsafe struct Request
{
public byte[] buffer;
public byte* ptr;

public Request(int size)
{
buffer = GC.AllocateArray<byte>(size, pinned: true);
ptr = (byte*)Unsafe.AsPointer(ref buffer[0]);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
using Garnet.server;
using Tsavorite.core;

namespace BDN.benchmark
namespace BDN.benchmark.CustomProcs
{
sealed class CustomProcSetBench : CustomTransactionProcedure
/// <summary>
/// Custom procedure to set values
/// </summary>
sealed class CustomProcSet : CustomTransactionProcedure
{
/// <summary>
/// Parameters including command
Expand Down
28 changes: 28 additions & 0 deletions benchmark/BDN.benchmark/Geo/GeoHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Attributes;

namespace BDN.benchmark.Geo
{
/// <summary>
/// Benchmark for GeoHash
/// </summary>
[MemoryDiagnoser]
public class GeoHash
{
private const double Latitude = 47.642219912251285;
private const double Longitude = -122.14205560231471;

private const long GeoHashInteger = 1557413161902764;

[Benchmark]
public long GeoToLongValue() => Garnet.server.GeoHash.GeoToLongValue(Latitude, Longitude);

[Benchmark]
public (double, double) GetCoordinatesFromLong() => Garnet.server.GeoHash.GetCoordinatesFromLong(GeoHashInteger);

[Benchmark]
public string GetGeoHashCode() => Garnet.server.GeoHash.GetGeoHashCode(GeoHashInteger);
}
}
27 changes: 0 additions & 27 deletions benchmark/BDN.benchmark/GeoHashBenchmarks.cs

This file was deleted.

Loading

0 comments on commit 428acbb

Please sign in to comment.