Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce struct BasicProperties for basicPublish #1096

Merged
merged 2 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public AsyncBasicConsumerFake(ManualResetEventSlim autoResetEvent)
_autoResetEvent = autoResetEvent;
}

public Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, ReadOnlyMemory<byte> body)
public Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, in ReadOnlyBasicProperties properties, ReadOnlyMemory<byte> body)
{
if (Interlocked.Increment(ref _current) == Count)
{
Expand All @@ -29,7 +29,7 @@ public Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redel
}

void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
IBasicProperties properties, ReadOnlyMemory<byte> body)
in ReadOnlyBasicProperties properties, ReadOnlyMemory<byte> body)
{
if (Interlocked.Increment(ref _current) == Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConsumerDispatcherBase
protected readonly ulong _deliveryTag = 500UL;
protected readonly string _exchange = "Exchange";
protected readonly string _routingKey = "RoutingKey";
protected readonly IBasicProperties _properties = new Client.Framing.BasicProperties();
protected readonly ReadOnlyBasicProperties _properties = new ReadOnlyBasicProperties();
protected readonly byte[] _body = new byte[512];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ namespace Benchmarks.Networking
[MemoryDiagnoser]
public class Networking_BasicDeliver_Commons
{
private const int messageCount = 10000;


public static async Task Publish_Hello_World(IConnection connection, uint n, byte[] body)
public static async Task Publish_Hello_World(IConnection connection, uint messageCount, byte[] body)
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
using (var model = connection.CreateModel())
Expand All @@ -31,7 +28,7 @@ public static async Task Publish_Hello_World(IConnection connection, uint n, byt

for (int i = 0; i < messageCount; i++)
{
model.BasicPublish("", queue.QueueName, null, body);
model.BasicPublish("", queue.QueueName, body);
}

await tcs.Task;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void GlobalCleanup()
}

[Benchmark(Baseline = true)]
public async Task Publish_Hello_World()
public Task Publish_Hello_World()
{
await Networking_BasicDeliver_Commons.Publish_Hello_World(_connection, messageCount, _body);
return Networking_BasicDeliver_Commons.Publish_Hello_World(_connection, messageCount, _body);
}
}
}
15 changes: 7 additions & 8 deletions projects/Benchmarks/WireFormatting/MethodFraming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using System.Text;

using BenchmarkDotNet.Attributes;

using RabbitMQ.Client;
using RabbitMQ.Client.client.impl;
using RabbitMQ.Client.Framing.Impl;
using RabbitMQ.Client.Impl;

using BasicProperties = RabbitMQ.Client.Framing.BasicProperties;

namespace RabbitMQ.Benchmarks
{
[Config(typeof(Config))]
Expand All @@ -30,8 +29,8 @@ public class MethodFramingBasicPublish
private const string StringValue = "Exchange_OR_RoutingKey";
private BasicPublish _basicPublish = new BasicPublish(StringValue, StringValue, false, false);
private BasicPublishMemory _basicPublishMemory = new BasicPublishMemory(Encoding.UTF8.GetBytes(StringValue), Encoding.UTF8.GetBytes(StringValue), false, false);
private readonly BasicProperties _propertiesEmpty = new BasicProperties();
private readonly BasicProperties _properties = new BasicProperties { AppId = "Application id", MessageId = "Random message id" };
private EmptyBasicProperty _propertiesEmpty = new EmptyBasicProperty();
private BasicProperties _properties = new BasicProperties { AppId = "Application id", MessageId = "Random message id" };
private readonly ReadOnlyMemory<byte> _bodyEmpty = ReadOnlyMemory<byte>.Empty;
private readonly ReadOnlyMemory<byte> _body = new byte[512];

Expand All @@ -42,13 +41,13 @@ public class MethodFramingBasicPublish
public int FrameMax { get; set; }

[Benchmark]
public ReadOnlyMemory<byte> BasicPublishWriteNonEmpty() => Framing.SerializeToFrames(ref _basicPublish, _properties, _body, Channel, FrameMax);
public ReadOnlyMemory<byte> BasicPublishWriteNonEmpty() => Framing.SerializeToFrames(ref _basicPublish, ref _properties, _body, Channel, FrameMax);

[Benchmark]
public ReadOnlyMemory<byte> BasicPublishWrite() => Framing.SerializeToFrames(ref _basicPublish, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
public ReadOnlyMemory<byte> BasicPublishWrite() => Framing.SerializeToFrames(ref _basicPublish, ref _propertiesEmpty, _bodyEmpty, Channel, FrameMax);

[Benchmark]
public ReadOnlyMemory<byte> BasicPublishMemoryWrite() => Framing.SerializeToFrames(ref _basicPublishMemory, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
public ReadOnlyMemory<byte> BasicPublishMemoryWrite() => Framing.SerializeToFrames(ref _basicPublishMemory, ref _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
}

[Config(typeof(Config))]
Expand Down
26 changes: 12 additions & 14 deletions projects/Benchmarks/WireFormatting/MethodSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using System.Text;

using BenchmarkDotNet.Attributes;

using RabbitMQ.Client.Framing;
using RabbitMQ.Client;
using RabbitMQ.Client.Framing.Impl;

namespace RabbitMQ.Benchmarks
Expand All @@ -21,13 +20,13 @@ public virtual void SetUp() { }
public class MethodBasicAck : MethodSerializationBase
{
private readonly BasicAck _basicAck = new BasicAck(ulong.MaxValue, true);
public override void SetUp() => _basicAck.WriteArgumentsTo(_buffer.Span);
public override void SetUp() => _basicAck.WriteTo(_buffer.Span);

[Benchmark]
public ulong BasicAckRead() => new BasicAck(_buffer.Span)._deliveryTag; // return one property to not box when returning an object instead

[Benchmark]
public int BasicAckWrite() => _basicAck.WriteArgumentsTo(_buffer.Span);
public int BasicAckWrite() => _basicAck.WriteTo(_buffer.Span);
}

public class MethodBasicDeliver : MethodSerializationBase
Expand All @@ -38,7 +37,6 @@ public class MethodBasicDeliver : MethodSerializationBase

public override void SetUp()
{
int length = _buffer.Length;
int offset = Client.Impl.WireFormatting.WriteShortstr(ref _buffer.Span.GetStart(), string.Empty);
offset += Client.Impl.WireFormatting.WriteLonglong(ref _buffer.Span.GetOffset(offset), 0);
offset += Client.Impl.WireFormatting.WriteBits(ref _buffer.Span.GetOffset(offset), false);
Expand All @@ -50,10 +48,10 @@ public override void SetUp()
public object BasicDeliverRead() => new BasicDeliver(_buffer.Span)._consumerTag; // return one property to not box when returning an object instead

[Benchmark]
public int BasicPublishWrite() => _basicPublish.WriteArgumentsTo(_buffer.Span);
public int BasicPublishWrite() => _basicPublish.WriteTo(_buffer.Span);

[Benchmark]
public int BasicPublishMemoryWrite() => _basicPublishMemory.WriteArgumentsTo(_buffer.Span);
public int BasicPublishMemoryWrite() => _basicPublishMemory.WriteTo(_buffer.Span);

[Benchmark]
public int BasicPublishSize() => _basicPublish.GetRequiredBufferSize();
Expand All @@ -66,27 +64,27 @@ public class MethodChannelClose : MethodSerializationBase
{
private readonly ChannelClose _channelClose = new ChannelClose(333, string.Empty, 0099, 2999);

public override void SetUp() => _channelClose.WriteArgumentsTo(_buffer.Span);
public override void SetUp() => _channelClose.WriteTo(_buffer.Span);

[Benchmark]
public object ChannelCloseRead() => new ChannelClose(_buffer.Span)._replyText; // return one property to not box when returning an object instead

[Benchmark]
public int ChannelCloseWrite() => _channelClose.WriteArgumentsTo(_buffer.Span);
public int ChannelCloseWrite() => _channelClose.WriteTo(_buffer.Span);
}

public class MethodBasicProperties : MethodSerializationBase
{
private readonly BasicProperties _basicProperties = new BasicProperties { Persistent = true, AppId = "AppId", ContentEncoding = "content", };
public override void SetUp() => _basicProperties.WritePropertiesTo(_buffer.Span);
private readonly IAmqpWriteable _basicProperties = new BasicProperties { Persistent = true, AppId = "AppId", ContentEncoding = "content", };
public override void SetUp() => _basicProperties.WriteTo(_buffer.Span);

[Benchmark]
public object BasicPropertiesRead() => new BasicProperties(_buffer.Span);
public ReadOnlyBasicProperties BasicPropertiesRead() => new ReadOnlyBasicProperties(_buffer.Span);

[Benchmark]
public int BasicPropertiesWrite() => _basicProperties.WritePropertiesTo(_buffer.Span);
public int BasicPropertiesWrite() => _basicProperties.WriteTo(_buffer.Span);

[Benchmark]
public int BasicDeliverSize() => _basicProperties.GetRequiredPayloadBufferSize();
public int BasicDeliverSize() => _basicProperties.GetRequiredBufferSize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public virtual Task HandleBasicDeliver(string consumerTag,
bool redelivered,
string exchange,
string routingKey,
IBasicProperties properties,
in ReadOnlyBasicProperties properties,
ReadOnlyMemory<byte> body)
{
// Nothing to do here.
Expand Down Expand Up @@ -165,7 +165,7 @@ void IBasicConsumer.HandleBasicConsumeOk(string consumerTag)
throw new InvalidOperationException("Should never be called.");
}

void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, ReadOnlyMemory<byte> body)
void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, in ReadOnlyBasicProperties properties, ReadOnlyMemory<byte> body)
{
throw new InvalidOperationException("Should never be called.");
}
Expand Down
8 changes: 4 additions & 4 deletions projects/RabbitMQ.Client/client/api/BasicGetResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed class BasicGetResult : IDisposable
/// <param name="basicProperties">The Basic-class content header properties for the message.</param>
/// <param name="body">The body</param>
public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange, string routingKey,
uint messageCount, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
uint messageCount, in ReadOnlyBasicProperties basicProperties, ReadOnlyMemory<byte> body)
{
DeliveryTag = deliveryTag;
Redelivered = redelivered;
Expand All @@ -76,7 +76,7 @@ public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange, stri
/// <param name="body">The body</param>
/// <param name="rentedArray">The rented array which body is part of.</param>
public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange, string routingKey,
uint messageCount, IBasicProperties basicProperties, ReadOnlyMemory<byte> body, byte[] rentedArray)
uint messageCount, in ReadOnlyBasicProperties basicProperties, ReadOnlyMemory<byte> body, byte[] rentedArray)
{
DeliveryTag = deliveryTag;
Redelivered = redelivered;
Expand All @@ -91,7 +91,7 @@ public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange, stri
/// <summary>
/// Retrieves the Basic-class content header properties for this message.
/// </summary>
public IBasicProperties BasicProperties { get; }
public ReadOnlyBasicProperties BasicProperties { get; }

/// <summary>
/// Retrieves the body of this message.
Expand Down Expand Up @@ -130,7 +130,7 @@ public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange, stri
/// <inheritdoc />
public void Dispose()
{
if (!(_rentedArray is null))
if (_rentedArray is not null)
{
ArrayPool<byte>.Shared.Return(_rentedArray);
}
Expand Down
Loading