diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosElementSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosElementSerializer.cs index 6740e059a8..2cc38725ea 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosElementSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosElementSerializer.cs @@ -221,7 +221,7 @@ internal static MemoryStream ToStream( return new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count, writable: false, publiclyVisible: true); } - internal static IEnumerable GetResources( + internal static IReadOnlyList GetResources( IReadOnlyList cosmosArray, CosmosSerializerCore serializerCore) { @@ -232,7 +232,7 @@ internal static IEnumerable GetResources( if (typeof(CosmosElement).IsAssignableFrom(typeof(T))) { - return cosmosArray.Cast(); + return cosmosArray.Cast().ToList(); } return CosmosElementSerializer.GetResourcesHelper( @@ -240,7 +240,7 @@ internal static IEnumerable GetResources( serializerCore); } - private static IEnumerable GetResourcesHelper( + private static IReadOnlyList GetResourcesHelper( IReadOnlyList cosmosArray, CosmosSerializerCore serializerCore) { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ChangeFeed/SmokeTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ChangeFeed/SmokeTests.cs index 92801fe474..95abdb0dd4 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ChangeFeed/SmokeTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ChangeFeed/SmokeTests.cs @@ -230,7 +230,7 @@ private class FailedUserSerializer : CosmosSerializer public override T FromStream(Stream stream) { // Only let changes serialization pass through - if (typeof(T) == typeof(TestClass)) + if (typeof(T) == typeof(List)) { return this.cosmosSerializer.FromStream(stream); } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/CosmosElements/ReadFeedBenchmark.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/CosmosElements/ReadFeedBenchmark.cs index 736889a1f5..2e8dbdedc3 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/CosmosElements/ReadFeedBenchmark.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/CosmosElements/ReadFeedBenchmark.cs @@ -6,10 +6,7 @@ using System.Text; using BenchmarkDotNet.Attributes; using Microsoft.Azure.Cosmos.CosmosElements; - using Microsoft.Azure.Cosmos.Json; - using Microsoft.Azure.Cosmos.Json.Interop; using Microsoft.Azure.Cosmos.Serializer; - using Microsoft.Azure.Cosmos.Tests.Poco; using Newtonsoft.Json; [MemoryDiagnoser] @@ -31,7 +28,7 @@ public class ToDoActivity public ToDoActivity[] children { get; set; } - public override bool Equals(Object obj) + public override bool Equals(object obj) { ToDoActivity input = obj as ToDoActivity; if (input == null) @@ -110,23 +107,39 @@ public ReadFeedBenchmark() } [Benchmark] - public void ByteFindArrayParsingJson() + public void ByteParsingToFindJsonArray() { using (MemoryStream ms = new MemoryStream(this.payloadBytes)) { - using (MemoryStream memoryStream = CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope( - ms)) + long length = ms.Length; + using (MemoryStream memoryStream = CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope(ms)) { - if (ms.Length == memoryStream.Length) + if (length == memoryStream.Length) { throw new Exception(); } - } + } + } + } + + [Benchmark] + public void ByteParsingToFindJsonArrayWithSeriliazation() + { + using (MemoryStream ms = new MemoryStream(this.payloadBytes)) + { + IReadOnlyList results = CosmosFeedResponseSerializer.FromFeedResponseStream( + this.serializerCore, + ms); + + if (results.Count != 1000) + { + throw new Exception(); + } } } [Benchmark] - public void CosmosElements() + public void CosmosElementsToFindArray() { using (MemoryStream ms = new MemoryStream(this.payloadBytes)) { @@ -135,7 +148,7 @@ public void CosmosElements() Documents.ResourceType.Document, null); - using(MemoryStream memoryStream = CosmosElementSerializer.ElementToMemoryStream( + using (MemoryStream memoryStream = CosmosElementSerializer.ElementToMemoryStream( array, null)) { @@ -146,5 +159,26 @@ public void CosmosElements() } } } + + [Benchmark] + public void CosmosElementsToFindArrayWithSerialization() + { + using (MemoryStream ms = new MemoryStream(this.payloadBytes)) + { + CosmosArray array = CosmosElementSerializer.ToCosmosElements( + ms, + Documents.ResourceType.Document, + null); + + IReadOnlyList results = CosmosElementSerializer.GetResources( + array, + this.serializerCore); + + if (results.Count != 1000) + { + throw new Exception(); + } + } + } } } \ No newline at end of file