Skip to content

Commit

Permalink
Merge branch 'main' into talzacc/bdn_module
Browse files Browse the repository at this point in the history
  • Loading branch information
TalZaccai authored Jan 17, 2025
2 parents c8af088 + 5d93ac5 commit f953f37
Show file tree
Hide file tree
Showing 42 changed files with 3,563 additions and 463 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-bdnbenchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
os: [ ubuntu-latest, windows-latest ]
framework: [ 'net8.0' ]
configuration: [ 'Release' ]
test: [ 'Operations.BasicOperations', 'Operations.ObjectOperations', 'Operations.HashObjectOperations', 'Cluster.ClusterMigrate', 'Cluster.ClusterOperations', 'Lua.LuaScripts', 'Operations.CustomOperations', 'Operations.RawStringOperations', 'Operations.ScriptOperations','Network.BasicOperations', 'Network.RawStringOperations' ]
test: [ 'Operations.BasicOperations', 'Operations.ObjectOperations', 'Operations.HashObjectOperations', 'Cluster.ClusterMigrate', 'Cluster.ClusterOperations', 'Lua.LuaScripts', 'Lua.LuaScriptCacheOperations','Lua.LuaRunnerOperations','Operations.CustomOperations', 'Operations.RawStringOperations', 'Operations.ScriptOperations','Network.BasicOperations', 'Network.RawStringOperations' ]
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<!-- Versioning property for builds and packages -->
<PropertyGroup>
<VersionPrefix>1.0.50</VersionPrefix>
<VersionPrefix>1.0.51</VersionPrefix>
</PropertyGroup>
</Project>
12 changes: 11 additions & 1 deletion benchmark/BDN.benchmark/Embedded/EmbeddedRespServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the MIT license.

using Garnet;
using Garnet.common;
using Garnet.server;
using Microsoft.Extensions.Logging;
using Tsavorite.core;

namespace Embedded.server
{
Expand All @@ -13,6 +15,7 @@ namespace Embedded.server
internal sealed class EmbeddedRespServer : GarnetServer
{
readonly GarnetServerEmbedded garnetServerEmbedded;
readonly SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>> subscribeBroker;

/// <summary>
/// Creates an EmbeddedRespServer instance
Expand All @@ -23,6 +26,13 @@ internal sealed class EmbeddedRespServer : GarnetServer
public EmbeddedRespServer(GarnetServerOptions opts, ILoggerFactory loggerFactory = null, GarnetServerEmbedded server = null) : base(opts, loggerFactory, server)
{
this.garnetServerEmbedded = server;
this.subscribeBroker = opts.DisablePubSub ? null :
new SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>>(
new SpanByteKeySerializer(),
null,
opts.PubSubPageSizeBytes(),
opts.SubscriberRefreshFrequencyMs,
true);
}

/// <summary>
Expand All @@ -38,7 +48,7 @@ public EmbeddedRespServer(GarnetServerOptions opts, ILoggerFactory loggerFactory
/// <returns>A new RESP server session</returns>
internal RespServerSession GetRespSession()
{
return new RespServerSession(0, new EmbeddedNetworkSender(), storeWrapper, null, null, true);
return new RespServerSession(0, new EmbeddedNetworkSender(), storeWrapper, subscribeBroker: subscribeBroker, null, true);
}

internal EmbeddedNetworkHandler GetNetworkHandler()
Expand Down
24 changes: 18 additions & 6 deletions benchmark/BDN.benchmark/Lua/LuaParams.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Code;
using Garnet.server;

namespace BDN.benchmark.Lua
{
/// <summary>
/// Cluster parameters
/// Lua parameters
/// </summary>
public struct LuaParams
public readonly struct LuaParams
{
public readonly LuaMemoryManagementMode Mode { get; }
public readonly bool MemoryLimit { get; }

/// <summary>
/// Constructor
/// </summary>
public LuaParams()
public LuaParams(LuaMemoryManagementMode mode, bool memoryLimit)
{
Mode = mode;
MemoryLimit = memoryLimit;
}

/// <summary>
/// Get the equivalent <see cref="LuaOptions"/>.
/// </summary>
public LuaOptions CreateOptions()
=> new(Mode, MemoryLimit ? "2m" : "");

/// <summary>
/// String representation
/// </summary>
public override string ToString()
{
return "None";
}
=> $"{Mode},{(MemoryLimit ? "Limit" : "None")}";
}
}
39 changes: 30 additions & 9 deletions benchmark/BDN.benchmark/Lua/LuaRunnerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ public unsafe class LuaRunnerOperations
/// Lua parameters provider
/// </summary>
public IEnumerable<LuaParams> LuaParamsProvider()
{
yield return new();
}
=> [
new(LuaMemoryManagementMode.Native, false),
new(LuaMemoryManagementMode.Tracked, false),
new(LuaMemoryManagementMode.Tracked, true),
new(LuaMemoryManagementMode.Managed, false),
new(LuaMemoryManagementMode.Managed, true),
];

private EmbeddedRespServer server;
private RespServerSession session;
Expand All @@ -151,16 +155,21 @@ public IEnumerable<LuaParams> LuaParamsProvider()
private LuaRunner smallCompileRunner;
private LuaRunner largeCompileRunner;

private LuaOptions opts;

[GlobalSetup]
public void GlobalSetup()
{
server = new EmbeddedRespServer(new GarnetServerOptions() { EnableLua = true, QuietMode = true });
opts = Params.CreateOptions();

server = new EmbeddedRespServer(new GarnetServerOptions() { EnableLua = true, QuietMode = true, LuaOptions = opts });

session = server.GetRespSession();
paramsRunner = new LuaRunner("return nil");

smallCompileRunner = new LuaRunner(SmallScript);
largeCompileRunner = new LuaRunner(LargeScript);
paramsRunner = new LuaRunner(opts, "return nil");

smallCompileRunner = new LuaRunner(opts, SmallScript);
largeCompileRunner = new LuaRunner(opts, LargeScript);
}

[GlobalCleanup]
Expand All @@ -171,6 +180,18 @@ public void GlobalCleanup()
paramsRunner.Dispose();
}

[IterationSetup]
public void IterationSetup()
{
session.EnterAndGetResponseObject();
}

[IterationCleanup]
public void IterationCleanup()
{
session.ExitAndReturnResponseObject();
}

[Benchmark]
public void ResetParametersSmall()
{
Expand All @@ -194,13 +215,13 @@ public void ResetParametersLarge()
[Benchmark]
public void ConstructSmall()
{
using var runner = new LuaRunner(SmallScript);
using var runner = new LuaRunner(opts, SmallScript);
}

[Benchmark]
public void ConstructLarge()
{
using var runner = new LuaRunner(LargeScript);
using var runner = new LuaRunner(opts, LargeScript);
}

[Benchmark]
Expand Down
14 changes: 10 additions & 4 deletions benchmark/BDN.benchmark/Lua/LuaScriptCacheOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ public class LuaScriptCacheOperations
/// Lua parameters provider
/// </summary>
public IEnumerable<LuaParams> LuaParamsProvider()
{
yield return new();
}
=> [
new(LuaMemoryManagementMode.Native, false),
new(LuaMemoryManagementMode.Tracked, false),
new(LuaMemoryManagementMode.Tracked, true),
new(LuaMemoryManagementMode.Managed, false),
new(LuaMemoryManagementMode.Managed, true),
];

private EmbeddedRespServer server;
private StoreWrapper storeWrapper;
Expand All @@ -38,7 +42,9 @@ public IEnumerable<LuaParams> LuaParamsProvider()
[GlobalSetup]
public void GlobalSetup()
{
server = new EmbeddedRespServer(new GarnetServerOptions() { EnableLua = true, QuietMode = true });
var options = Params.CreateOptions();

server = new EmbeddedRespServer(new GarnetServerOptions() { EnableLua = true, QuietMode = true, LuaOptions = options });
storeWrapper = server.StoreWrapper;
sessionScriptCache = new SessionScriptCache(storeWrapper, new GarnetNoAuthAuthenticator());
session = server.GetRespSession();
Expand Down
20 changes: 13 additions & 7 deletions benchmark/BDN.benchmark/Lua/LuaScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ public unsafe class LuaScripts
/// Lua parameters provider
/// </summary>
public IEnumerable<LuaParams> LuaParamsProvider()
{
yield return new();
}
=> [
new(LuaMemoryManagementMode.Native, false),
new(LuaMemoryManagementMode.Tracked, false),
new(LuaMemoryManagementMode.Tracked, true),
new(LuaMemoryManagementMode.Managed, false),
new(LuaMemoryManagementMode.Managed, true),
];

LuaRunner r1, r2, r3, r4;
readonly string[] keys = ["key1"];

[GlobalSetup]
public void GlobalSetup()
{
r1 = new LuaRunner("return");
var options = Params.CreateOptions();

r1 = new LuaRunner(options, "return");
r1.CompileForRunner();
r2 = new LuaRunner("return 1 + 1");
r2 = new LuaRunner(options, "return 1 + 1");
r2.CompileForRunner();
r3 = new LuaRunner("return KEYS[1]");
r3 = new LuaRunner(options, "return KEYS[1]");
r3.CompileForRunner();
r4 = new LuaRunner("return redis.call(KEYS[1])");
r4 = new LuaRunner(options, "return redis.call(KEYS[1])");
r4.CompileForRunner();
}

Expand Down
8 changes: 5 additions & 3 deletions benchmark/BDN.benchmark/Operations/OperationsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public IEnumerable<OperationParams> OperationParamsProvider()
internal const int batchSize = 100;
internal EmbeddedRespServer server;
internal RespServerSession session;
internal RespServerSession subscribeSession;

/// <summary>
/// Setup
Expand All @@ -55,7 +56,9 @@ public virtual void GlobalSetup()
QuietMode = true,
EnableLua = true,
DisablePubSub = true,
LuaOptions = new(LuaMemoryManagementMode.Native, ""),
};

if (Params.useAof)
{
opts.EnableAOF = true;
Expand All @@ -72,7 +75,7 @@ public virtual void GlobalSetup()
if (Params.useACLs)
{
aclFile = Path.GetTempFileName();
File.WriteAllText(aclFile, @"user default on nopass -@all +ping +set +get +setex +incr +decr +incrby +decrby +zadd +zrem +lpush +lpop +sadd +srem +hset +hdel +@custom");
File.WriteAllText(aclFile, @"user default on nopass -@all +ping +set +get +setex +incr +decr +incrby +decrby +zadd +zrem +lpush +lpop +sadd +srem +hset +hdel +publish +subscribe +@custom");
opts.AuthSettings = new AclAuthenticationPasswordSettings(aclFile);
}

Expand All @@ -84,8 +87,6 @@ public virtual void GlobalSetup()
if (aclFile != null)
File.Delete(aclFile);
}

session = server.GetRespSession();
}

/// <summary>
Expand All @@ -95,6 +96,7 @@ public virtual void GlobalSetup()
public virtual void GlobalCleanup()
{
session.Dispose();
subscribeSession?.Dispose();
server.Dispose();
}

Expand Down
55 changes: 55 additions & 0 deletions benchmark/BDN.benchmark/Operations/PubSubOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Attributes;
using Embedded.server;
using Garnet.server;

namespace BDN.benchmark.Operations
{
/// <summary>
/// Benchmark for PubSubOperations
/// </summary>
[MemoryDiagnoser]
public unsafe class PubSubOperations : OperationsBase
{
static ReadOnlySpan<byte> PUBLISH => "*3\r\n$7\r\nPUBLISH\r\n$7\r\nchannel\r\n$7\r\nmessage\r\n"u8;
Request publish;

static ReadOnlySpan<byte> SUBSCRIBE => "*2\r\n$9\r\nSUBSCRIBE\r\n$7\r\nchannel\r\n"u8;
Request subscribe;

public override void GlobalSetup()
{
var opts = new GarnetServerOptions
{
QuietMode = true,
DisablePubSub = false,
};

server = new EmbeddedRespServer(opts, null, new GarnetServerEmbedded());
session = server.GetRespSession();
subscribeSession = server.GetRespSession();

SetupOperation(ref publish, PUBLISH);
SetupOperation(ref subscribe, SUBSCRIBE);

// Subscribe to secondary session
_ = subscribeSession.TryConsumeMessages(subscribe.bufferPtr, subscribe.buffer.Length);

// Warm up
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
SlowConsumeMessage(new Span<byte>(publish.bufferPtr, publish.buffer.Length));
}

[Benchmark]
public void Publish()
{
Send(publish);
}
}
}
Loading

0 comments on commit f953f37

Please sign in to comment.