Skip to content

Instantly share code, notes, and snippets.

@AnthonySteele
Last active December 28, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonySteele/3544807f54831ca9bce8ef32c31f5ebd to your computer and use it in GitHub Desktop.
Save AnthonySteele/3544807f54831ca9bce8ef32c31f5ebd to your computer and use it in GitHub Desktop.

A microbenchmark on the new message context store in JustSaying. The last one, ReadWriteNewMessageContext is probably the most realistic.

So we have an overhead of around 50 nanoseconds (5×10-5 ms). This is good. And I suggest that we don't bother with ways to opt in or out of this, just have it on all the time, regardless of if it is used by handlers or not.

// * Summary *

BenchmarkDotNet=v0.11.3, OS=Windows 10.0.17134.112 (1803/April2018Update/Redstone4)   
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores   
.NET Core SDK=2.2.101   
  [Host]     : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT  
  DefaultJob : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT  
Method Mean Error StdDev Gen 0/1k Op Allocated Memory/Op
ReadMessageContext 9.928 ns 0.0497 ns 0.0441 ns - -
ReadWriteMessageContext 18.014 ns 0.0462 ns 0.0409 ns - -
ReadWriteNewMessageContext 46.583 ns 0.3081 ns 0.2882 ns 0.0220 104 B
using System;
using Amazon.SQS.Model;
using BenchmarkDotNet.Attributes;
using JustSaying.Messaging.MessageHandling;

namespace Benchmark
{
    [MemoryDiagnoser]
    public class ContextBenchmark
    {
        private MessageContext _messageContext;
        private Message _sqsMessage;
        private Uri _queueUri;

        private IMessageContextReader _reader;
        private IMessageContextAccessor _writer;

        private MessageContext MakeContext()
        {
            // it matches the app code to make a new message context instance
            // over existing SQS message and queue uri objects
            return new MessageContext(_sqsMessage, _queueUri);
        }

        [GlobalSetup]
        public void Setup()
        {
            _sqsMessage = new Message
            {
                Body = "this is a test message",
                MessageId = "1234"
            };
            _queueUri = new Uri("http://some.queue.com.12345");

            _messageContext = MakeContext();


            var accessor = new MessageContextAccessor();

            _reader = accessor;
            _writer = accessor;

            _writer.MessageContext = _messageContext;
        }

        [Benchmark]
        public MessageContext ReadMessageContext()
        {
            var ctx = _reader.MessageContext;
            return ctx;
        }

        [Benchmark]
        public MessageContext ReadWriteMessageContext()
        {
            _writer.MessageContext = _messageContext;
            return _reader.MessageContext;
        }

        [Benchmark]
        public MessageContext ReadWriteNewMessageContext()
        {
            _writer.MessageContext = MakeContext();
            return _reader.MessageContext;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment