1ffe3c632Sopenharmony_ci#region Copyright notice and license 2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 3ffe3c632Sopenharmony_ci// Copyright 2019 Google Inc. All rights reserved. 4ffe3c632Sopenharmony_ci// https://github.com/protocolbuffers/protobuf 5ffe3c632Sopenharmony_ci// 6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 8ffe3c632Sopenharmony_ci// met: 9ffe3c632Sopenharmony_ci// 10ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 12ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 15ffe3c632Sopenharmony_ci// distribution. 16ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 18ffe3c632Sopenharmony_ci// this software without specific prior written permission. 19ffe3c632Sopenharmony_ci// 20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31ffe3c632Sopenharmony_ci#endregion 32ffe3c632Sopenharmony_ci 33ffe3c632Sopenharmony_ciusing BenchmarkDotNet.Attributes; 34ffe3c632Sopenharmony_ciusing System; 35ffe3c632Sopenharmony_ciusing System.Collections.Generic; 36ffe3c632Sopenharmony_ciusing System.IO; 37ffe3c632Sopenharmony_ciusing System.Linq; 38ffe3c632Sopenharmony_ciusing System.Buffers; 39ffe3c632Sopenharmony_ciusing Google.Protobuf.WellKnownTypes; 40ffe3c632Sopenharmony_ciusing Benchmarks.Proto3; 41ffe3c632Sopenharmony_ci 42ffe3c632Sopenharmony_cinamespace Google.Protobuf.Benchmarks 43ffe3c632Sopenharmony_ci{ 44ffe3c632Sopenharmony_ci /// <summary> 45ffe3c632Sopenharmony_ci /// Benchmark that tests parsing performance for various messages. 46ffe3c632Sopenharmony_ci /// </summary> 47ffe3c632Sopenharmony_ci [MemoryDiagnoser] 48ffe3c632Sopenharmony_ci public class ParseMessagesBenchmark 49ffe3c632Sopenharmony_ci { 50ffe3c632Sopenharmony_ci const int MaxMessages = 100; 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ci SubTest manyWrapperFieldsTest = new SubTest(CreateManyWrapperFieldsMessage(), ManyWrapperFieldsMessage.Parser, () => new ManyWrapperFieldsMessage(), MaxMessages); 53ffe3c632Sopenharmony_ci SubTest manyPrimitiveFieldsTest = new SubTest(CreateManyPrimitiveFieldsMessage(), ManyPrimitiveFieldsMessage.Parser, () => new ManyPrimitiveFieldsMessage(), MaxMessages); 54ffe3c632Sopenharmony_ci SubTest repeatedFieldTest = new SubTest(CreateRepeatedFieldMessage(), GoogleMessage1.Parser, () => new GoogleMessage1(), MaxMessages); 55ffe3c632Sopenharmony_ci SubTest emptyMessageTest = new SubTest(new Empty(), Empty.Parser, () => new Empty(), MaxMessages); 56ffe3c632Sopenharmony_ci 57ffe3c632Sopenharmony_ci public IEnumerable<int> MessageCountValues => new[] { 10, 100 }; 58ffe3c632Sopenharmony_ci 59ffe3c632Sopenharmony_ci [GlobalSetup] 60ffe3c632Sopenharmony_ci public void GlobalSetup() 61ffe3c632Sopenharmony_ci { 62ffe3c632Sopenharmony_ci } 63ffe3c632Sopenharmony_ci 64ffe3c632Sopenharmony_ci [Benchmark] 65ffe3c632Sopenharmony_ci public IMessage ManyWrapperFieldsMessage_ParseFromByteArray() 66ffe3c632Sopenharmony_ci { 67ffe3c632Sopenharmony_ci return manyWrapperFieldsTest.ParseFromByteArray(); 68ffe3c632Sopenharmony_ci } 69ffe3c632Sopenharmony_ci 70ffe3c632Sopenharmony_ci [Benchmark] 71ffe3c632Sopenharmony_ci public IMessage ManyWrapperFieldsMessage_ParseFromReadOnlySequence() 72ffe3c632Sopenharmony_ci { 73ffe3c632Sopenharmony_ci return manyWrapperFieldsTest.ParseFromReadOnlySequence(); 74ffe3c632Sopenharmony_ci } 75ffe3c632Sopenharmony_ci 76ffe3c632Sopenharmony_ci [Benchmark] 77ffe3c632Sopenharmony_ci public IMessage ManyPrimitiveFieldsMessage_ParseFromByteArray() 78ffe3c632Sopenharmony_ci { 79ffe3c632Sopenharmony_ci return manyPrimitiveFieldsTest.ParseFromByteArray(); 80ffe3c632Sopenharmony_ci } 81ffe3c632Sopenharmony_ci 82ffe3c632Sopenharmony_ci [Benchmark] 83ffe3c632Sopenharmony_ci public IMessage ManyPrimitiveFieldsMessage_ParseFromReadOnlySequence() 84ffe3c632Sopenharmony_ci { 85ffe3c632Sopenharmony_ci return manyPrimitiveFieldsTest.ParseFromReadOnlySequence(); 86ffe3c632Sopenharmony_ci } 87ffe3c632Sopenharmony_ci 88ffe3c632Sopenharmony_ci [Benchmark] 89ffe3c632Sopenharmony_ci public IMessage RepeatedFieldMessage_ParseFromByteArray() 90ffe3c632Sopenharmony_ci { 91ffe3c632Sopenharmony_ci return repeatedFieldTest.ParseFromByteArray(); 92ffe3c632Sopenharmony_ci } 93ffe3c632Sopenharmony_ci 94ffe3c632Sopenharmony_ci [Benchmark] 95ffe3c632Sopenharmony_ci public IMessage RepeatedFieldMessage_ParseFromReadOnlySequence() 96ffe3c632Sopenharmony_ci { 97ffe3c632Sopenharmony_ci return repeatedFieldTest.ParseFromReadOnlySequence(); 98ffe3c632Sopenharmony_ci } 99ffe3c632Sopenharmony_ci 100ffe3c632Sopenharmony_ci [Benchmark] 101ffe3c632Sopenharmony_ci public IMessage EmptyMessage_ParseFromByteArray() 102ffe3c632Sopenharmony_ci { 103ffe3c632Sopenharmony_ci return emptyMessageTest.ParseFromByteArray(); 104ffe3c632Sopenharmony_ci } 105ffe3c632Sopenharmony_ci 106ffe3c632Sopenharmony_ci [Benchmark] 107ffe3c632Sopenharmony_ci public IMessage EmptyMessage_ParseFromReadOnlySequence() 108ffe3c632Sopenharmony_ci { 109ffe3c632Sopenharmony_ci return emptyMessageTest.ParseFromReadOnlySequence(); 110ffe3c632Sopenharmony_ci } 111ffe3c632Sopenharmony_ci 112ffe3c632Sopenharmony_ci [Benchmark] 113ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 114ffe3c632Sopenharmony_ci public void ManyWrapperFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) 115ffe3c632Sopenharmony_ci { 116ffe3c632Sopenharmony_ci manyWrapperFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount); 117ffe3c632Sopenharmony_ci } 118ffe3c632Sopenharmony_ci 119ffe3c632Sopenharmony_ci [Benchmark] 120ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 121ffe3c632Sopenharmony_ci public void ManyWrapperFieldsMessage_ParseDelimitedMessagesFromReadOnlySequence(int messageCount) 122ffe3c632Sopenharmony_ci { 123ffe3c632Sopenharmony_ci manyWrapperFieldsTest.ParseDelimitedMessagesFromReadOnlySequence(messageCount); 124ffe3c632Sopenharmony_ci } 125ffe3c632Sopenharmony_ci 126ffe3c632Sopenharmony_ci [Benchmark] 127ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 128ffe3c632Sopenharmony_ci public void ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) 129ffe3c632Sopenharmony_ci { 130ffe3c632Sopenharmony_ci manyPrimitiveFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount); 131ffe3c632Sopenharmony_ci } 132ffe3c632Sopenharmony_ci 133ffe3c632Sopenharmony_ci [Benchmark] 134ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 135ffe3c632Sopenharmony_ci public void ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromReadOnlySequence(int messageCount) 136ffe3c632Sopenharmony_ci { 137ffe3c632Sopenharmony_ci manyPrimitiveFieldsTest.ParseDelimitedMessagesFromReadOnlySequence(messageCount); 138ffe3c632Sopenharmony_ci } 139ffe3c632Sopenharmony_ci 140ffe3c632Sopenharmony_ci [Benchmark] 141ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 142ffe3c632Sopenharmony_ci public void RepeatedFieldMessage_ParseDelimitedMessagesFromByteArray(int messageCount) 143ffe3c632Sopenharmony_ci { 144ffe3c632Sopenharmony_ci repeatedFieldTest.ParseDelimitedMessagesFromByteArray(messageCount); 145ffe3c632Sopenharmony_ci } 146ffe3c632Sopenharmony_ci 147ffe3c632Sopenharmony_ci [Benchmark] 148ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 149ffe3c632Sopenharmony_ci public void RepeatedFieldMessage_ParseDelimitedMessagesFromReadOnlySequence(int messageCount) 150ffe3c632Sopenharmony_ci { 151ffe3c632Sopenharmony_ci repeatedFieldTest.ParseDelimitedMessagesFromReadOnlySequence(messageCount); 152ffe3c632Sopenharmony_ci } 153ffe3c632Sopenharmony_ci 154ffe3c632Sopenharmony_ci public static ManyWrapperFieldsMessage CreateManyWrapperFieldsMessage() 155ffe3c632Sopenharmony_ci { 156ffe3c632Sopenharmony_ci // Example data match data of an internal benchmarks 157ffe3c632Sopenharmony_ci return new ManyWrapperFieldsMessage() 158ffe3c632Sopenharmony_ci { 159ffe3c632Sopenharmony_ci Int64Field19 = 123, 160ffe3c632Sopenharmony_ci Int64Field37 = 1000032, 161ffe3c632Sopenharmony_ci Int64Field26 = 3453524500, 162ffe3c632Sopenharmony_ci DoubleField79 = 1.2, 163ffe3c632Sopenharmony_ci DoubleField25 = 234, 164ffe3c632Sopenharmony_ci DoubleField9 = 123.3, 165ffe3c632Sopenharmony_ci DoubleField28 = 23, 166ffe3c632Sopenharmony_ci DoubleField7 = 234, 167ffe3c632Sopenharmony_ci DoubleField50 = 2.45 168ffe3c632Sopenharmony_ci }; 169ffe3c632Sopenharmony_ci } 170ffe3c632Sopenharmony_ci 171ffe3c632Sopenharmony_ci public static ManyPrimitiveFieldsMessage CreateManyPrimitiveFieldsMessage() 172ffe3c632Sopenharmony_ci { 173ffe3c632Sopenharmony_ci // Example data match data of an internal benchmarks 174ffe3c632Sopenharmony_ci return new ManyPrimitiveFieldsMessage() 175ffe3c632Sopenharmony_ci { 176ffe3c632Sopenharmony_ci Int64Field19 = 123, 177ffe3c632Sopenharmony_ci Int64Field37 = 1000032, 178ffe3c632Sopenharmony_ci Int64Field26 = 3453524500, 179ffe3c632Sopenharmony_ci DoubleField79 = 1.2, 180ffe3c632Sopenharmony_ci DoubleField25 = 234, 181ffe3c632Sopenharmony_ci DoubleField9 = 123.3, 182ffe3c632Sopenharmony_ci DoubleField28 = 23, 183ffe3c632Sopenharmony_ci DoubleField7 = 234, 184ffe3c632Sopenharmony_ci DoubleField50 = 2.45 185ffe3c632Sopenharmony_ci }; 186ffe3c632Sopenharmony_ci } 187ffe3c632Sopenharmony_ci 188ffe3c632Sopenharmony_ci public static GoogleMessage1 CreateRepeatedFieldMessage() 189ffe3c632Sopenharmony_ci { 190ffe3c632Sopenharmony_ci // Message with a repeated fixed length item collection 191ffe3c632Sopenharmony_ci var message = new GoogleMessage1(); 192ffe3c632Sopenharmony_ci for (ulong i = 0; i < 1000; i++) 193ffe3c632Sopenharmony_ci { 194ffe3c632Sopenharmony_ci message.Field5.Add(i); 195ffe3c632Sopenharmony_ci } 196ffe3c632Sopenharmony_ci return message; 197ffe3c632Sopenharmony_ci } 198ffe3c632Sopenharmony_ci 199ffe3c632Sopenharmony_ci private class SubTest 200ffe3c632Sopenharmony_ci { 201ffe3c632Sopenharmony_ci private readonly IMessage message; 202ffe3c632Sopenharmony_ci private readonly MessageParser parser; 203ffe3c632Sopenharmony_ci private readonly Func<IMessage> factory; 204ffe3c632Sopenharmony_ci private readonly byte[] data; 205ffe3c632Sopenharmony_ci private readonly byte[] multipleMessagesData; 206ffe3c632Sopenharmony_ci 207ffe3c632Sopenharmony_ci private ReadOnlySequence<byte> dataSequence; 208ffe3c632Sopenharmony_ci private ReadOnlySequence<byte> multipleMessagesDataSequence; 209ffe3c632Sopenharmony_ci 210ffe3c632Sopenharmony_ci public SubTest(IMessage message, MessageParser parser, Func<IMessage> factory, int maxMessageCount) 211ffe3c632Sopenharmony_ci { 212ffe3c632Sopenharmony_ci this.message = message; 213ffe3c632Sopenharmony_ci this.parser = parser; 214ffe3c632Sopenharmony_ci this.factory = factory; 215ffe3c632Sopenharmony_ci this.data = message.ToByteArray(); 216ffe3c632Sopenharmony_ci this.multipleMessagesData = CreateBufferWithMultipleMessages(message, maxMessageCount); 217ffe3c632Sopenharmony_ci this.dataSequence = new ReadOnlySequence<byte>(this.data); 218ffe3c632Sopenharmony_ci this.multipleMessagesDataSequence = new ReadOnlySequence<byte>(this.multipleMessagesData); 219ffe3c632Sopenharmony_ci } 220ffe3c632Sopenharmony_ci 221ffe3c632Sopenharmony_ci public IMessage ParseFromByteArray() => parser.ParseFrom(data); 222ffe3c632Sopenharmony_ci 223ffe3c632Sopenharmony_ci public IMessage ParseFromReadOnlySequence() => parser.ParseFrom(dataSequence); 224ffe3c632Sopenharmony_ci 225ffe3c632Sopenharmony_ci public void ParseDelimitedMessagesFromByteArray(int messageCount) 226ffe3c632Sopenharmony_ci { 227ffe3c632Sopenharmony_ci var input = new CodedInputStream(multipleMessagesData); 228ffe3c632Sopenharmony_ci for (int i = 0; i < messageCount; i++) 229ffe3c632Sopenharmony_ci { 230ffe3c632Sopenharmony_ci var msg = factory(); 231ffe3c632Sopenharmony_ci input.ReadMessage(msg); 232ffe3c632Sopenharmony_ci } 233ffe3c632Sopenharmony_ci } 234ffe3c632Sopenharmony_ci 235ffe3c632Sopenharmony_ci public void ParseDelimitedMessagesFromReadOnlySequence(int messageCount) 236ffe3c632Sopenharmony_ci { 237ffe3c632Sopenharmony_ci ParseContext.Initialize(multipleMessagesDataSequence, out ParseContext ctx); 238ffe3c632Sopenharmony_ci for (int i = 0; i < messageCount; i++) 239ffe3c632Sopenharmony_ci { 240ffe3c632Sopenharmony_ci var msg = factory(); 241ffe3c632Sopenharmony_ci ctx.ReadMessage(msg); 242ffe3c632Sopenharmony_ci } 243ffe3c632Sopenharmony_ci } 244ffe3c632Sopenharmony_ci 245ffe3c632Sopenharmony_ci private static byte[] CreateBufferWithMultipleMessages(IMessage msg, int msgCount) 246ffe3c632Sopenharmony_ci { 247ffe3c632Sopenharmony_ci var ms = new MemoryStream(); 248ffe3c632Sopenharmony_ci var cos = new CodedOutputStream(ms); 249ffe3c632Sopenharmony_ci for (int i = 0; i < msgCount; i++) 250ffe3c632Sopenharmony_ci { 251ffe3c632Sopenharmony_ci cos.WriteMessage(msg); 252ffe3c632Sopenharmony_ci } 253ffe3c632Sopenharmony_ci cos.Flush(); 254ffe3c632Sopenharmony_ci return ms.ToArray(); 255ffe3c632Sopenharmony_ci } 256ffe3c632Sopenharmony_ci } 257ffe3c632Sopenharmony_ci } 258ffe3c632Sopenharmony_ci} 259