Repository: microsoft/garnet
File: benchmark/BDN.benchmark/Operations/ObjectOperations.cs (C#)

Code before:

namespace BDN.benchmark.Operations
{
    /// <summary>
    /// Benchmark for ObjectOperations
    /// </summary>
    [MemoryDiagnoser]
    public unsafe class ObjectOperations : OperationsBase
    {
        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;

        static ReadOnlySpan<byte> LPUSHPOP => "*3\r\n$5\r\nLPUSH\r\n$1\r\nd\r\n$1\r\ne\r\n*2\r\n$4\r\nLPOP\r\n$1\r\nd\r\n"u8;
        byte[] lPushPopRequestBuffer;
        byte* lPushPopRequestBufferPointer;

        static ReadOnlySpan<byte> SADDREM => "*3\r\n$4\r\nSADD\r\n$1\r\ne\r\n$1\r\na\r\n*3\r\n$4\r\nSREM\r\n$1\r\ne\r\n$1\r\na\r\n"u8;
        byte[] sAddRemRequestBuffer;
        byte* sAddRemRequestBufferPointer;

        static ReadOnlySpan<byte> HSETDEL => "*4\r\n$4\r\nHSET\r\n$1\r\nf\r\n$1\r\na\r\n$1\r\na\r\n*3\r\n$4\r\nHDEL\r\n$1\r\nf\r\n$1\r\na\r\n"u8;
        byte[] hSetDelRequestBuffer;
        byte* hSetDelRequestBufferPointer;

        static ReadOnlySpan<byte> HEXISTS => "*3\r\n$7\r\nHEXISTS\r\n$1\r\nf\r\n$1\r\na\r\n"u8;
        byte[] hExistsRequestBuffer;
        byte* hExistsRequestBufferPointer;

        static ReadOnlySpan<byte> HGET => "*3\r\n$4\r\nHGET\r\n$1\r\nf\r\n$1\r\na\r\n"u8;
        byte[] hGetRequestBuffer;
        byte* hGetRequestBufferPointer;

        static ReadOnlySpan<byte> HGETALL => "*2\r\n$7\r\nHGETALL\r\n$1\r\nf\r\n"u8;
        byte[] hGetAllRequestBuffer;
        byte* hGetAllRequestBufferPointer;

        static ReadOnlySpan<byte> HINCRBY => "*4\r\n$7\r\nHINCRBY\r\n$1\r\nf\r\n$1\r\nc\r\n$1\r\n1\r\n"u8;
        byte[] hIncrbyRequestBuffer;
        byte* hIncrbyRequestBufferPointer;

        static ReadOnlySpan<byte> HINCRBYFLOAT => "*4\r\n$12\r\nHINCRBYFLOAT\r\n$1\r\nf\r\n$1\r\nd\r\n$3\r\n1.5\r\n"u8;
        byte[] hIncrbyFloatRequestBuffer;
        byte* hIncrbyFloatRequestBufferPointer;

        static ReadOnlySpan<byte> HKEYS => "*2\r\n$5\r\nHKEYS\r\n$1\r\nf\r\n"u8;
        byte[] hKeysRequestBuffer;
        byte* hKeysRequestBufferPointer;

        static ReadOnlySpan<byte> HLEN => "*2\r\n$4\r\nHLEN\r\n$1\r\nf\r\n"u8;
        byte[] hLenRequestBuffer;

Review comment (refactor):
This is a lot, and might grow when we add for sorted-set etc. so it might be a good idea to refactor into its own class called `HashObjectOperations` which will hold all the hash operations.

Code after:

namespace BDN.benchmark.Operations
{
    /// <summary>
    /// Benchmark for ObjectOperations
    /// </summary>
    [MemoryDiagnoser]
    public unsafe class ObjectOperations : OperationsBase
    {
        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;

        static ReadOnlySpan<byte> LPUSHPOP => "*3\r\n$5\r\nLPUSH\r\n$1\r\nd\r\n$1\r\ne\r\n*2\r\n$4\r\nLPOP\r\n$1\r\nd\r\n"u8;
        byte[] lPushPopRequestBuffer;
        byte* lPushPopRequestBufferPointer;

        static ReadOnlySpan<byte> SADDREM => "*3\r\n$4\r\nSADD\r\n$1\r\ne\r\n$1\r\na\r\n*3\r\n$4\r\nSREM\r\n$1\r\ne\r\n$1\r\na\r\n"u8;
        byte[] sAddRemRequestBuffer;
        byte* sAddRemRequestBufferPointer;

        public override void GlobalSetup()
        {
            base.GlobalSetup();
            SetupOperation(ref zAddRemRequestBuffer, ref zAddRemRequestBufferPointer, ZADDREM);
            SetupOperation(ref lPushPopRequestBuffer, ref lPushPopRequestBufferPointer, LPUSHPOP);
            SetupOperation(ref sAddRemRequestBuffer, ref sAddRemRequestBufferPointer, SADDREM);

            // Pre-populate data
            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);
            SlowConsumeMessage("*3\r\n$5\r\nLPUSH\r\n$1\r\nd\r\n$1\r\nf\r\n"u8);
            SlowConsumeMessage("*3\r\n$4\r\nSADD\r\n$1\r\ne\r\n$1\r\nb\r\n"u8);
        }

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

        [Benchmark]
        public void LPushPop()
        {
            _ = session.TryConsumeMessages(lPushPopRequestBufferPointer, lPushPopRequestBuffer.Length);
        }

        [Benchmark]
        public void SAddRem()
        {
            _ = session.TryConsumeMessages(sAddRemRequestBufferPointer, sAddRemRequestBuffer.Length);
        }