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