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_ci 41ffe3c632Sopenharmony_cinamespace Google.Protobuf.Benchmarks 42ffe3c632Sopenharmony_ci{ 43ffe3c632Sopenharmony_ci /// <summary> 44ffe3c632Sopenharmony_ci /// Benchmark that tests writing performance for various messages. 45ffe3c632Sopenharmony_ci /// </summary> 46ffe3c632Sopenharmony_ci [MemoryDiagnoser] 47ffe3c632Sopenharmony_ci public class WriteMessagesBenchmark 48ffe3c632Sopenharmony_ci { 49ffe3c632Sopenharmony_ci const int MaxMessages = 100; 50ffe3c632Sopenharmony_ci 51ffe3c632Sopenharmony_ci SubTest manyWrapperFieldsTest = new SubTest(ParseMessagesBenchmark.CreateManyWrapperFieldsMessage(), MaxMessages); 52ffe3c632Sopenharmony_ci SubTest manyPrimitiveFieldsTest = new SubTest(ParseMessagesBenchmark.CreateManyPrimitiveFieldsMessage(), MaxMessages); 53ffe3c632Sopenharmony_ci SubTest emptyMessageTest = new SubTest(new Empty(), MaxMessages); 54ffe3c632Sopenharmony_ci 55ffe3c632Sopenharmony_ci public IEnumerable<int> MessageCountValues => new[] { 10, 100 }; 56ffe3c632Sopenharmony_ci 57ffe3c632Sopenharmony_ci [GlobalSetup] 58ffe3c632Sopenharmony_ci public void GlobalSetup() 59ffe3c632Sopenharmony_ci { 60ffe3c632Sopenharmony_ci } 61ffe3c632Sopenharmony_ci 62ffe3c632Sopenharmony_ci [Benchmark] 63ffe3c632Sopenharmony_ci public byte[] ManyWrapperFieldsMessage_ToByteArray() 64ffe3c632Sopenharmony_ci { 65ffe3c632Sopenharmony_ci return manyWrapperFieldsTest.ToByteArray(); 66ffe3c632Sopenharmony_ci } 67ffe3c632Sopenharmony_ci 68ffe3c632Sopenharmony_ci [Benchmark] 69ffe3c632Sopenharmony_ci public byte[] ManyWrapperFieldsMessage_WriteToCodedOutputStream() 70ffe3c632Sopenharmony_ci { 71ffe3c632Sopenharmony_ci return manyWrapperFieldsTest.WriteToCodedOutputStream_PreAllocatedBuffer(); 72ffe3c632Sopenharmony_ci } 73ffe3c632Sopenharmony_ci 74ffe3c632Sopenharmony_ci [Benchmark] 75ffe3c632Sopenharmony_ci public byte[] ManyWrapperFieldsMessage_WriteToSpan() 76ffe3c632Sopenharmony_ci { 77ffe3c632Sopenharmony_ci return manyWrapperFieldsTest.WriteToSpan_PreAllocatedBuffer(); 78ffe3c632Sopenharmony_ci } 79ffe3c632Sopenharmony_ci 80ffe3c632Sopenharmony_ci 81ffe3c632Sopenharmony_ci [Benchmark] 82ffe3c632Sopenharmony_ci public byte[] ManyPrimitiveFieldsMessage_ToByteArray() 83ffe3c632Sopenharmony_ci { 84ffe3c632Sopenharmony_ci return manyPrimitiveFieldsTest.ToByteArray(); 85ffe3c632Sopenharmony_ci } 86ffe3c632Sopenharmony_ci 87ffe3c632Sopenharmony_ci [Benchmark] 88ffe3c632Sopenharmony_ci public byte[] ManyPrimitiveFieldsMessage_WriteToCodedOutputStream() 89ffe3c632Sopenharmony_ci { 90ffe3c632Sopenharmony_ci return manyPrimitiveFieldsTest.WriteToCodedOutputStream_PreAllocatedBuffer(); 91ffe3c632Sopenharmony_ci } 92ffe3c632Sopenharmony_ci 93ffe3c632Sopenharmony_ci [Benchmark] 94ffe3c632Sopenharmony_ci public byte[] ManyPrimitiveFieldsMessage_WriteToSpan() 95ffe3c632Sopenharmony_ci { 96ffe3c632Sopenharmony_ci return manyPrimitiveFieldsTest.WriteToSpan_PreAllocatedBuffer(); 97ffe3c632Sopenharmony_ci } 98ffe3c632Sopenharmony_ci 99ffe3c632Sopenharmony_ci [Benchmark] 100ffe3c632Sopenharmony_ci public byte[] EmptyMessage_ToByteArray() 101ffe3c632Sopenharmony_ci { 102ffe3c632Sopenharmony_ci return emptyMessageTest.ToByteArray(); 103ffe3c632Sopenharmony_ci } 104ffe3c632Sopenharmony_ci 105ffe3c632Sopenharmony_ci [Benchmark] 106ffe3c632Sopenharmony_ci public byte[] EmptyMessage_WriteToCodedOutputStream() 107ffe3c632Sopenharmony_ci { 108ffe3c632Sopenharmony_ci return emptyMessageTest.WriteToCodedOutputStream_PreAllocatedBuffer(); 109ffe3c632Sopenharmony_ci } 110ffe3c632Sopenharmony_ci 111ffe3c632Sopenharmony_ci [Benchmark] 112ffe3c632Sopenharmony_ci public byte[] EmptyMessage_WriteToSpan() 113ffe3c632Sopenharmony_ci { 114ffe3c632Sopenharmony_ci return emptyMessageTest.WriteToSpan_PreAllocatedBuffer(); 115ffe3c632Sopenharmony_ci } 116ffe3c632Sopenharmony_ci 117ffe3c632Sopenharmony_ci [Benchmark] 118ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 119ffe3c632Sopenharmony_ci public void ManyWrapperFieldsMessage_WriteDelimitedMessagesToCodedOutputStream(int messageCount) 120ffe3c632Sopenharmony_ci { 121ffe3c632Sopenharmony_ci manyWrapperFieldsTest.WriteDelimitedMessagesToCodedOutputStream_PreAllocatedBuffer(messageCount); 122ffe3c632Sopenharmony_ci } 123ffe3c632Sopenharmony_ci 124ffe3c632Sopenharmony_ci [Benchmark] 125ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 126ffe3c632Sopenharmony_ci public void ManyWrapperFieldsMessage_WriteDelimitedMessagesToSpan(int messageCount) 127ffe3c632Sopenharmony_ci { 128ffe3c632Sopenharmony_ci manyWrapperFieldsTest.WriteDelimitedMessagesToSpan_PreAllocatedBuffer(messageCount); 129ffe3c632Sopenharmony_ci } 130ffe3c632Sopenharmony_ci 131ffe3c632Sopenharmony_ci [Benchmark] 132ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 133ffe3c632Sopenharmony_ci public void ManyPrimitiveFieldsMessage_WriteDelimitedMessagesToCodedOutputStream(int messageCount) 134ffe3c632Sopenharmony_ci { 135ffe3c632Sopenharmony_ci manyPrimitiveFieldsTest.WriteDelimitedMessagesToCodedOutputStream_PreAllocatedBuffer(messageCount); 136ffe3c632Sopenharmony_ci } 137ffe3c632Sopenharmony_ci 138ffe3c632Sopenharmony_ci [Benchmark] 139ffe3c632Sopenharmony_ci [ArgumentsSource(nameof(MessageCountValues))] 140ffe3c632Sopenharmony_ci public void ManyPrimitiveFieldsMessage_WriteDelimitedMessagesToSpan(int messageCount) 141ffe3c632Sopenharmony_ci { 142ffe3c632Sopenharmony_ci manyPrimitiveFieldsTest.WriteDelimitedMessagesToSpan_PreAllocatedBuffer(messageCount); 143ffe3c632Sopenharmony_ci } 144ffe3c632Sopenharmony_ci 145ffe3c632Sopenharmony_ci private class SubTest 146ffe3c632Sopenharmony_ci { 147ffe3c632Sopenharmony_ci private readonly IMessage message; 148ffe3c632Sopenharmony_ci private readonly byte[] outputBuffer; 149ffe3c632Sopenharmony_ci private readonly byte[] multipleMessagesOutputBuffer; 150ffe3c632Sopenharmony_ci 151ffe3c632Sopenharmony_ci public SubTest(IMessage message, int maxMessageCount) 152ffe3c632Sopenharmony_ci { 153ffe3c632Sopenharmony_ci this.message = message; 154ffe3c632Sopenharmony_ci 155ffe3c632Sopenharmony_ci int messageSize = message.CalculateSize(); 156ffe3c632Sopenharmony_ci this.outputBuffer = new byte[messageSize]; 157ffe3c632Sopenharmony_ci this.multipleMessagesOutputBuffer = new byte[maxMessageCount * (messageSize + CodedOutputStream.ComputeLengthSize(messageSize))]; 158ffe3c632Sopenharmony_ci } 159ffe3c632Sopenharmony_ci 160ffe3c632Sopenharmony_ci public byte[] ToByteArray() => message.ToByteArray(); 161ffe3c632Sopenharmony_ci 162ffe3c632Sopenharmony_ci public byte[] WriteToCodedOutputStream_PreAllocatedBuffer() 163ffe3c632Sopenharmony_ci { 164ffe3c632Sopenharmony_ci var cos = new CodedOutputStream(outputBuffer); // use pre-existing output buffer 165ffe3c632Sopenharmony_ci message.WriteTo(cos); 166ffe3c632Sopenharmony_ci return outputBuffer; 167ffe3c632Sopenharmony_ci } 168ffe3c632Sopenharmony_ci 169ffe3c632Sopenharmony_ci public byte[] WriteToSpan_PreAllocatedBuffer() 170ffe3c632Sopenharmony_ci { 171ffe3c632Sopenharmony_ci var span = new Span<byte>(outputBuffer); // use pre-existing output buffer 172ffe3c632Sopenharmony_ci message.WriteTo(span); 173ffe3c632Sopenharmony_ci return outputBuffer; 174ffe3c632Sopenharmony_ci } 175ffe3c632Sopenharmony_ci 176ffe3c632Sopenharmony_ci public byte[] WriteDelimitedMessagesToCodedOutputStream_PreAllocatedBuffer(int messageCount) 177ffe3c632Sopenharmony_ci { 178ffe3c632Sopenharmony_ci var cos = new CodedOutputStream(multipleMessagesOutputBuffer); // use pre-existing output buffer 179ffe3c632Sopenharmony_ci for (int i = 0; i < messageCount; i++) 180ffe3c632Sopenharmony_ci { 181ffe3c632Sopenharmony_ci cos.WriteMessage(message); 182ffe3c632Sopenharmony_ci } 183ffe3c632Sopenharmony_ci return multipleMessagesOutputBuffer; 184ffe3c632Sopenharmony_ci } 185ffe3c632Sopenharmony_ci 186ffe3c632Sopenharmony_ci public byte[] WriteDelimitedMessagesToSpan_PreAllocatedBuffer(int messageCount) 187ffe3c632Sopenharmony_ci { 188ffe3c632Sopenharmony_ci var span = new Span<byte>(multipleMessagesOutputBuffer); // use pre-existing output buffer 189ffe3c632Sopenharmony_ci WriteContext.Initialize(ref span, out WriteContext ctx); 190ffe3c632Sopenharmony_ci for (int i = 0; i < messageCount; i++) 191ffe3c632Sopenharmony_ci { 192ffe3c632Sopenharmony_ci ctx.WriteMessage(message); 193ffe3c632Sopenharmony_ci } 194ffe3c632Sopenharmony_ci return multipleMessagesOutputBuffer; 195ffe3c632Sopenharmony_ci } 196ffe3c632Sopenharmony_ci } 197ffe3c632Sopenharmony_ci } 198ffe3c632Sopenharmony_ci} 199