From c007c1284538f1fa0eed997debb56bb9ce8cefa5 Mon Sep 17 00:00:00 2001 From: martincostello Date: Wed, 3 Jul 2024 15:26:37 +0100 Subject: [PATCH] Avoid ServiceBusSenderOptions allocations - Avoid allocating empty `ServiceBusSenderOptions` instances when creating instances of `ServiceBusSender`. - Fix tests that fail if the machine they are run on does not use US date formatting. --- .../tests/AmqpAnnotatedMessageConverterTests.cs | 15 +++++++-------- .../Azure.Messaging.ServiceBus/CHANGELOG.md | 2 ++ .../src/Client/ServiceBusClient.cs | 3 +-- .../src/Sender/ServiceBusSender.cs | 7 +++---- .../tests/Message/MessageLiveTests.cs | 16 +++++++--------- .../tests/Sender/SenderTests.cs | 4 ++-- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/sdk/core/Azure.Core.Amqp/tests/AmqpAnnotatedMessageConverterTests.cs b/sdk/core/Azure.Core.Amqp/tests/AmqpAnnotatedMessageConverterTests.cs index f7ca33f24815c..85101630c0d7b 100644 --- a/sdk/core/Azure.Core.Amqp/tests/AmqpAnnotatedMessageConverterTests.cs +++ b/sdk/core/Azure.Core.Amqp/tests/AmqpAnnotatedMessageConverterTests.cs @@ -3,11 +3,10 @@ using System; using System.Collections.Generic; -using System.ComponentModel; +using System.Globalization; using System.IO; using System.Linq; using System.Text; -using Azure.Core.Amqp.Shared; using Microsoft.Azure.Amqp; using Microsoft.Azure.Amqp.Encoding; using Microsoft.Azure.Amqp.Framing; @@ -35,10 +34,10 @@ public class AmqpAnnotatedMessageConverterTests new double[] { 3.1415926 }, new decimal(3.1415926), new decimal[] { new decimal(3.1415926) }, - DateTimeOffset.Parse("3/24/21").UtcDateTime, - new DateTime[] {DateTimeOffset.Parse("3/24/21").UtcDateTime }, - DateTimeOffset.Parse("3/24/21"), - new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21") }, + DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, + new DateTime[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime }, + DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture), + new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture) }, TimeSpan.FromSeconds(5), new TimeSpan[] {TimeSpan.FromSeconds(5)}, new Uri("http://localHost"), @@ -53,7 +52,7 @@ public class AmqpAnnotatedMessageConverterTests new Dictionary {{ "key", 1 } }, new Dictionary {{ "key", 3.1415926 } }, new Dictionary {{ "key", new decimal(3.1415926) } }, - new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21").UtcDateTime } }, + new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime } }, // for some reason dictionaries with DateTimeOffset, Timespan, or Uri values are not supported in AMQP lib // new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21") } }, // new Dictionary {{ "key", TimeSpan.FromSeconds(5) } }, @@ -69,7 +68,7 @@ public class AmqpAnnotatedMessageConverterTests Enumerable.Repeat(new object[] { long.MaxValue }, 2), Enumerable.Repeat(new object[] { 1 }, 2), Enumerable.Repeat(new object[] { 3.1415926, true }, 2), - Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21").UtcDateTime, true }, 2), + Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, true }, 2), new List> { new List { "first", 1}, new List { "second", 2 } } }; diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md index c2ad2c867a04f..fb55ddac919fa 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md @@ -19,6 +19,8 @@ - Updated the `Microsoft.Azure.Amqp` dependency to 2.6.7, which contains a fix for decoding messages with a null format code as the body. +- Instances of `ServiceBusSender` created with no explicit `ServiceBusSenderOptions` value allocate less memory. + ## 7.18.0-beta.1 (2024-05-08) ### Features Added diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Client/ServiceBusClient.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Client/ServiceBusClient.cs index 262cccc2fa11e..ce542dad734a1 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Client/ServiceBusClient.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Client/ServiceBusClient.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using System; -using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using Azure.Core; @@ -256,7 +255,7 @@ private ServiceBusClient( /// The was constructed with a connection string containing the "EntityPath" token /// that has a different value than the value specified here. /// - public virtual ServiceBusSender CreateSender(string queueOrTopicName) => CreateSender(queueOrTopicName, new ServiceBusSenderOptions()); + public virtual ServiceBusSender CreateSender(string queueOrTopicName) => CreateSender(queueOrTopicName, null); /// /// Creates a instance that can be used for sending messages to a specific diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs index 357c3f2b8ea6b..c614c5cd17516 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Threading; @@ -120,10 +119,10 @@ internal ServiceBusSender( Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath)); connection.ThrowIfClosed(); - options = options?.Clone() ?? new ServiceBusSenderOptions(); + var identifier = string.IsNullOrEmpty(options?.Identifier) ? DiagnosticUtilities.GenerateIdentifier(EntityPath) : options.Identifier; EntityPath = entityPath; - Identifier = string.IsNullOrEmpty(options.Identifier) ? DiagnosticUtilities.GenerateIdentifier(EntityPath) : options.Identifier; + Identifier = identifier; _connection = connection; _retryPolicy = _connection.RetryOptions.ToRetryPolicy(); _innerSender = _connection.CreateTransportSender( @@ -159,7 +158,7 @@ protected ServiceBusSender() /// The client instance to use for the sender. /// The name of the queue or topic to send to. protected ServiceBusSender(ServiceBusClient client, string queueOrTopicName) : - this(queueOrTopicName, client.Connection, new ServiceBusSenderOptions()) + this(queueOrTopicName, client.Connection, null) { } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs index 33f801c2c78db..2d552ce386953 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs @@ -3,15 +3,13 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; -using System.Text; using System.Threading.Tasks; using Azure.Core.Amqp; using Azure.Core.Serialization; using Azure.Core.Shared; using Azure.Messaging.ServiceBus.Amqp; -using Azure.Messaging.ServiceBus.Primitives; -using Microsoft.Azure.Amqp.Encoding; using NUnit.Framework; namespace Azure.Messaging.ServiceBus.Tests.Message @@ -431,10 +429,10 @@ public async Task CanSendMultipleDataSections() new double[] { 3.1415926 }, new decimal(3.1415926), new decimal[] { new decimal(3.1415926) }, - DateTimeOffset.Parse("3/24/21").UtcDateTime, - new DateTime[] {DateTimeOffset.Parse("3/24/21").UtcDateTime }, - DateTimeOffset.Parse("3/24/21"), - new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21") }, + DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, + new DateTime[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime }, + DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture), + new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture) }, TimeSpan.FromSeconds(5), new TimeSpan[] {TimeSpan.FromSeconds(5)}, new Uri("http://localHost"), @@ -449,7 +447,7 @@ public async Task CanSendMultipleDataSections() new Dictionary {{ "key", 1 } }, new Dictionary {{ "key", 3.1415926 } }, new Dictionary {{ "key", new decimal(3.1415926) } }, - new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21").UtcDateTime } }, + new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime } }, // for some reason dictionaries with DateTimeOffset, Timespan, or Uri values are not supported in AMQP lib // new Dictionary {{ "key", DateTimeOffset.Parse("3/24/21") } }, // new Dictionary {{ "key", TimeSpan.FromSeconds(5) } }, @@ -494,7 +492,7 @@ public async Task CanSendValueSection(object value) Enumerable.Repeat(new object[] { long.MaxValue }, 2), Enumerable.Repeat(new object[] { 1 }, 2), Enumerable.Repeat(new object[] { 3.1415926, true }, 2), - Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21").UtcDateTime, true }, 2), + Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, true }, 2), new List> { new List { "first", 1}, new List { "second", 2 } } }; diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs index aead8358c20ca..eecf5dcd421a6 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs @@ -172,7 +172,7 @@ public async Task SendBatchManagesLockingTheBatch() Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, "The batch should not be locked before sending."); - var sender = new ServiceBusSender("dummy", mockConnection.Object, new ServiceBusSenderOptions()); + var sender = new ServiceBusSender("dummy", mockConnection.Object); var sendTask = sender.SendMessagesAsync(batch); Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Throws.InstanceOf(), "The batch should be locked while sending."); @@ -281,7 +281,7 @@ public async Task CloseRespectsCancellationToken() It.IsAny())) .Returns(mockTransportSender.Object); - var sender = new ServiceBusSender("fake", mockConnection.Object, new ServiceBusSenderOptions()); + var sender = new ServiceBusSender("fake", mockConnection.Object); await sender.CloseAsync(cts.Token); mockTransportSender.Verify(transportReceiver => transportReceiver.CloseAsync(It.Is(ct => ct == cts.Token))); }