xref: /aosp_15_r20/external/protobuf/csharp/src/Google.Protobuf.Benchmarks/GoogleMessageBenchmark.cs (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard Worker #region Copyright notice and license
2*1b3f573fSAndroid Build Coastguard Worker // Protocol Buffers - Google's data interchange format
3*1b3f573fSAndroid Build Coastguard Worker // Copyright 2019 Google Inc.  All rights reserved.
4*1b3f573fSAndroid Build Coastguard Worker // https://github.com/protocolbuffers/protobuf
5*1b3f573fSAndroid Build Coastguard Worker //
6*1b3f573fSAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without
7*1b3f573fSAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are
8*1b3f573fSAndroid Build Coastguard Worker // met:
9*1b3f573fSAndroid Build Coastguard Worker //
10*1b3f573fSAndroid Build Coastguard Worker //     * Redistributions of source code must retain the above copyright
11*1b3f573fSAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer.
12*1b3f573fSAndroid Build Coastguard Worker //     * Redistributions in binary form must reproduce the above
13*1b3f573fSAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following disclaimer
14*1b3f573fSAndroid Build Coastguard Worker // in the documentation and/or other materials provided with the
15*1b3f573fSAndroid Build Coastguard Worker // distribution.
16*1b3f573fSAndroid Build Coastguard Worker //     * Neither the name of Google Inc. nor the names of its
17*1b3f573fSAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived from
18*1b3f573fSAndroid Build Coastguard Worker // this software without specific prior written permission.
19*1b3f573fSAndroid Build Coastguard Worker //
20*1b3f573fSAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*1b3f573fSAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*1b3f573fSAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*1b3f573fSAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*1b3f573fSAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*1b3f573fSAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*1b3f573fSAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*1b3f573fSAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*1b3f573fSAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*1b3f573fSAndroid Build Coastguard Worker #endregion
32*1b3f573fSAndroid Build Coastguard Worker 
33*1b3f573fSAndroid Build Coastguard Worker using BenchmarkDotNet.Attributes;
34*1b3f573fSAndroid Build Coastguard Worker using System.Collections.Generic;
35*1b3f573fSAndroid Build Coastguard Worker using System.IO;
36*1b3f573fSAndroid Build Coastguard Worker using System.Linq;
37*1b3f573fSAndroid Build Coastguard Worker 
38*1b3f573fSAndroid Build Coastguard Worker namespace Google.Protobuf.Benchmarks
39*1b3f573fSAndroid Build Coastguard Worker {
40*1b3f573fSAndroid Build Coastguard Worker     /// <summary>
41*1b3f573fSAndroid Build Coastguard Worker     /// Benchmark for serializing and deserializing of standard datasets that are also
42*1b3f573fSAndroid Build Coastguard Worker     /// measured by benchmarks in other languages.
43*1b3f573fSAndroid Build Coastguard Worker     /// Over time we may wish to test the various different approaches to serialization and deserialization separately.
44*1b3f573fSAndroid Build Coastguard Worker     /// See https://github.com/protocolbuffers/protobuf/blob/main/benchmarks/README.md
45*1b3f573fSAndroid Build Coastguard Worker     /// See https://github.com/protocolbuffers/protobuf/blob/main/docs/performance.md
46*1b3f573fSAndroid Build Coastguard Worker     /// </summary>
47*1b3f573fSAndroid Build Coastguard Worker     [MemoryDiagnoser]
48*1b3f573fSAndroid Build Coastguard Worker     public class GoogleMessageBenchmark
49*1b3f573fSAndroid Build Coastguard Worker     {
50*1b3f573fSAndroid Build Coastguard Worker         /// <summary>
51*1b3f573fSAndroid Build Coastguard Worker         /// All the datasets to be tested. Add more datasets to the array as they're available.
52*1b3f573fSAndroid Build Coastguard Worker         /// (When C# supports proto2, this will increase significantly.)
53*1b3f573fSAndroid Build Coastguard Worker         /// </summary>
54*1b3f573fSAndroid Build Coastguard Worker         public static BenchmarkDatasetConfig[] DatasetConfigurations => new[]
55*1b3f573fSAndroid Build Coastguard Worker         {
56*1b3f573fSAndroid Build Coastguard Worker             // short name is specified to make results table more readable
57*1b3f573fSAndroid Build Coastguard Worker             new BenchmarkDatasetConfig("dataset.google_message1_proto3.pb", "goog_msg1_proto3")
58*1b3f573fSAndroid Build Coastguard Worker         };
59*1b3f573fSAndroid Build Coastguard Worker 
60*1b3f573fSAndroid Build Coastguard Worker         [ParamsSource(nameof(DatasetConfigurations))]
61*1b3f573fSAndroid Build Coastguard Worker         public BenchmarkDatasetConfig Dataset { get; set; }
62*1b3f573fSAndroid Build Coastguard Worker 
63*1b3f573fSAndroid Build Coastguard Worker         private MessageParser parser;
64*1b3f573fSAndroid Build Coastguard Worker         /// <summary>
65*1b3f573fSAndroid Build Coastguard Worker         /// Each data set can contain multiple messages in a single file.
66*1b3f573fSAndroid Build Coastguard Worker         /// Each "write" operation should write each message in turn, and each "parse"
67*1b3f573fSAndroid Build Coastguard Worker         /// operation should parse each message in turn.
68*1b3f573fSAndroid Build Coastguard Worker         /// </summary>
69*1b3f573fSAndroid Build Coastguard Worker         private List<SubTest> subTests;
70*1b3f573fSAndroid Build Coastguard Worker 
71*1b3f573fSAndroid Build Coastguard Worker         [GlobalSetup]
GlobalSetup()72*1b3f573fSAndroid Build Coastguard Worker         public void GlobalSetup()
73*1b3f573fSAndroid Build Coastguard Worker         {
74*1b3f573fSAndroid Build Coastguard Worker             parser = Dataset.Parser;
75*1b3f573fSAndroid Build Coastguard Worker             subTests = Dataset.Payloads.Select(p => new SubTest(p, parser.ParseFrom(p))).ToList();
76*1b3f573fSAndroid Build Coastguard Worker         }
77*1b3f573fSAndroid Build Coastguard Worker 
78*1b3f573fSAndroid Build Coastguard Worker         [Benchmark]
WriteToStream()79*1b3f573fSAndroid Build Coastguard Worker         public void WriteToStream() => subTests.ForEach(item => item.WriteToStream());
80*1b3f573fSAndroid Build Coastguard Worker 
81*1b3f573fSAndroid Build Coastguard Worker         [Benchmark]
ToByteArray()82*1b3f573fSAndroid Build Coastguard Worker         public void ToByteArray() => subTests.ForEach(item => item.ToByteArray());
83*1b3f573fSAndroid Build Coastguard Worker 
84*1b3f573fSAndroid Build Coastguard Worker         [Benchmark]
ParseFromByteArray()85*1b3f573fSAndroid Build Coastguard Worker         public void ParseFromByteArray() => subTests.ForEach(item => item.ParseFromByteArray(parser));
86*1b3f573fSAndroid Build Coastguard Worker 
87*1b3f573fSAndroid Build Coastguard Worker         [Benchmark]
ParseFromStream()88*1b3f573fSAndroid Build Coastguard Worker         public void ParseFromStream() => subTests.ForEach(item => item.ParseFromStream(parser));
89*1b3f573fSAndroid Build Coastguard Worker 
90*1b3f573fSAndroid Build Coastguard Worker         private class SubTest
91*1b3f573fSAndroid Build Coastguard Worker         {
92*1b3f573fSAndroid Build Coastguard Worker             private readonly Stream destinationStream;
93*1b3f573fSAndroid Build Coastguard Worker             private readonly Stream sourceStream;
94*1b3f573fSAndroid Build Coastguard Worker             private readonly byte[] data;
95*1b3f573fSAndroid Build Coastguard Worker             private readonly IMessage message;
96*1b3f573fSAndroid Build Coastguard Worker 
SubTest(byte[] data, IMessage message)97*1b3f573fSAndroid Build Coastguard Worker             public SubTest(byte[] data, IMessage message)
98*1b3f573fSAndroid Build Coastguard Worker             {
99*1b3f573fSAndroid Build Coastguard Worker                 destinationStream = new MemoryStream(data.Length);
100*1b3f573fSAndroid Build Coastguard Worker                 sourceStream = new MemoryStream(data);
101*1b3f573fSAndroid Build Coastguard Worker                 this.data = data;
102*1b3f573fSAndroid Build Coastguard Worker                 this.message = message;
103*1b3f573fSAndroid Build Coastguard Worker             }
104*1b3f573fSAndroid Build Coastguard Worker 
Reset()105*1b3f573fSAndroid Build Coastguard Worker             public void Reset() => destinationStream.Position = 0;
106*1b3f573fSAndroid Build Coastguard Worker 
WriteToStream()107*1b3f573fSAndroid Build Coastguard Worker             public void WriteToStream()
108*1b3f573fSAndroid Build Coastguard Worker             {
109*1b3f573fSAndroid Build Coastguard Worker                 destinationStream.Position = 0;
110*1b3f573fSAndroid Build Coastguard Worker                 message.WriteTo(destinationStream);
111*1b3f573fSAndroid Build Coastguard Worker             }
112*1b3f573fSAndroid Build Coastguard Worker 
ToByteArray()113*1b3f573fSAndroid Build Coastguard Worker             public void ToByteArray() => message.ToByteArray();
114*1b3f573fSAndroid Build Coastguard Worker 
115*1b3f573fSAndroid Build Coastguard Worker             public void ParseFromByteArray(MessageParser parser) => parser.ParseFrom(data);
116*1b3f573fSAndroid Build Coastguard Worker 
ParseFromStream(MessageParser parser)117*1b3f573fSAndroid Build Coastguard Worker             public void ParseFromStream(MessageParser parser)
118*1b3f573fSAndroid Build Coastguard Worker             {
119*1b3f573fSAndroid Build Coastguard Worker                 sourceStream.Position = 0;
120*1b3f573fSAndroid Build Coastguard Worker                 parser.ParseFrom(sourceStream);
121*1b3f573fSAndroid Build Coastguard Worker             }
122*1b3f573fSAndroid Build Coastguard Worker         }
123*1b3f573fSAndroid Build Coastguard Worker     }
124*1b3f573fSAndroid Build Coastguard Worker }
125