Skip to content

Commit

Permalink
* Add ZAddRem to BDN (#457)
Browse files Browse the repository at this point in the history
* Separate out ACL stress to its own BDN subclass
* Allow debugging in process
  • Loading branch information
badrishc authored Jun 10, 2024
1 parent 7c9ac99 commit 361f26a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 44 deletions.
7 changes: 6 additions & 1 deletion benchmark/BDN.benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, new BaseConfig());
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
#if DEBUG
.Run(args, new DebugInProcessConfig());
#else
.Run(args, new BaseConfig());
#endif

public class BaseConfig : ManualConfig
{
Expand Down
38 changes: 38 additions & 0 deletions benchmark/BDN.benchmark/Resp/RespParseACLStress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Attributes;
using Garnet.server.Auth.Settings;

namespace BDN.benchmark.Resp
{
[MemoryDiagnoser]
public unsafe class RespParseACLStress : RespParseStress
{
[Params(false, true)]
public bool UseACLs { get; set; }

public new void GlobalSetup()
{
string aclFile = null;

try
{
if (UseACLs)
{
aclFile = Path.GetTempFileName();
File.WriteAllText(aclFile, @"user default on nopass -@all +ping +set +get");
authSettings = new AclAuthenticationPasswordSettings(aclFile);
}
base.GlobalSetup();
}
finally
{
if (aclFile != null)
{
File.Delete(aclFile);
}
}
}
}
}
90 changes: 47 additions & 43 deletions benchmark/BDN.benchmark/Resp/RespParseStress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public unsafe class RespParseStress
{
EmbeddedRespServer server;
RespServerSession session;
protected IAuthenticationSettings authSettings = null;

const int batchSize = 128;

Expand All @@ -29,54 +30,43 @@ public unsafe class RespParseStress
byte[] getRequestBuffer;
byte* getRequestBufferPointer;

[Params(false, true)]
public bool UseACLs { get; set; }
static ReadOnlySpan<byte> ZADDREM => "*4\r\n$4\r\nZADD\r\n$1\r\nc\r\n$1\r\n1\r\n$1\r\nc\r\n*3\r\n$4\r\nZREM\r\n$1\r\nc\r\n$1\r\nc\r\n"u8;
byte[] zAddRemRequestBuffer;
byte* zAddRemRequestBufferPointer;

[GlobalSetup]
public void GlobalSetup()
{
IAuthenticationSettings authSettings = null;
string aclFile = null;

try
{
if (UseACLs)
{
aclFile = Path.GetTempFileName();
File.WriteAllText(aclFile, @"user default on nopass -@all +ping +set +get");
authSettings = new AclAuthenticationPasswordSettings(aclFile);
}

var opt = new GarnetServerOptions
{
QuietMode = true,
AuthSettings = authSettings,
};
server = new EmbeddedRespServer(opt);
session = server.GetRespSession();

pingRequestBuffer = GC.AllocateArray<byte>(INLINE_PING.Length * batchSize, pinned: true);
pingRequestBufferPointer = (byte*)Unsafe.AsPointer(ref pingRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
INLINE_PING.CopyTo(new Span<byte>(pingRequestBuffer).Slice(i * INLINE_PING.Length));

setRequestBuffer = GC.AllocateArray<byte>(SET.Length * batchSize, pinned: true);
setRequestBufferPointer = (byte*)Unsafe.AsPointer(ref setRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
SET.CopyTo(new Span<byte>(setRequestBuffer).Slice(i * SET.Length));

getRequestBuffer = GC.AllocateArray<byte>(GET.Length * batchSize, pinned: true);
getRequestBufferPointer = (byte*)Unsafe.AsPointer(ref getRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
GET.CopyTo(new Span<byte>(getRequestBuffer).Slice(i * GET.Length));
}
finally
var opt = new GarnetServerOptions
{
if (aclFile != null)
{
File.Delete(aclFile);
}
}
QuietMode = true,
AuthSettings = authSettings,
};
server = new EmbeddedRespServer(opt);
session = server.GetRespSession();

pingRequestBuffer = GC.AllocateArray<byte>(INLINE_PING.Length * batchSize, pinned: true);
pingRequestBufferPointer = (byte*)Unsafe.AsPointer(ref pingRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
INLINE_PING.CopyTo(new Span<byte>(pingRequestBuffer).Slice(i * INLINE_PING.Length));

setRequestBuffer = GC.AllocateArray<byte>(SET.Length * batchSize, pinned: true);
setRequestBufferPointer = (byte*)Unsafe.AsPointer(ref setRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
SET.CopyTo(new Span<byte>(setRequestBuffer).Slice(i * SET.Length));

getRequestBuffer = GC.AllocateArray<byte>(GET.Length * batchSize, pinned: true);
getRequestBufferPointer = (byte*)Unsafe.AsPointer(ref getRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
GET.CopyTo(new Span<byte>(getRequestBuffer).Slice(i * GET.Length));

zAddRemRequestBuffer = GC.AllocateArray<byte>(ZADDREM.Length * batchSize, pinned: true);
zAddRemRequestBufferPointer = (byte*)Unsafe.AsPointer(ref zAddRemRequestBuffer[0]);
for (int i = 0; i < batchSize; i++)
ZADDREM.CopyTo(new Span<byte>(zAddRemRequestBuffer).Slice(i * ZADDREM.Length));

// Pre-populate sorted set with a single element to avoid repeatedly emptying it during the benchmark
SlowConsumeMessage("*4\r\n$4\r\nZADD\r\n$1\r\nc\r\n$1\r\n1\r\n$1\r\nd\r\n"u8);
}

[GlobalCleanup]
Expand All @@ -103,5 +93,19 @@ public void Get()
{
_ = session.TryConsumeMessages(getRequestBufferPointer, getRequestBuffer.Length);
}

[Benchmark]
public void ZAddRem()
{
_ = session.TryConsumeMessages(zAddRemRequestBufferPointer, zAddRemRequestBuffer.Length);
}

private void SlowConsumeMessage(ReadOnlySpan<byte> message)
{
var buffer = GC.AllocateArray<byte>(message.Length, pinned: true);
var bufferPointer = (byte*)Unsafe.AsPointer(ref buffer[0]);
message.CopyTo(new Span<byte>(buffer));
_ = session.TryConsumeMessages(bufferPointer, buffer.Length);
}
}
}

0 comments on commit 361f26a

Please sign in to comment.