GoogleMessageBenchmark.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using BenchmarkDotNet.Attributes;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using System.Linq;
  36. namespace Google.Protobuf.Benchmarks
  37. {
  38. /// <summary>
  39. /// Benchmark for serializing and deserializing of standard datasets that are also
  40. /// measured by benchmarks in other languages.
  41. /// Over time we may wish to test the various different approaches to serialization and deserialization separately.
  42. /// See https://github.com/protocolbuffers/protobuf/blob/master/benchmarks/README.md
  43. /// See https://github.com/protocolbuffers/protobuf/blob/master/docs/performance.md
  44. /// </summary>
  45. [MemoryDiagnoser]
  46. public class GoogleMessageBenchmark
  47. {
  48. /// <summary>
  49. /// All the datasets to be tested. Add more datasets to the array as they're available.
  50. /// (When C# supports proto2, this will increase significantly.)
  51. /// </summary>
  52. public static BenchmarkDatasetConfig[] DatasetConfigurations => new[]
  53. {
  54. // short name is specified to make results table more readable
  55. new BenchmarkDatasetConfig("dataset.google_message1_proto3.pb", "goog_msg1_proto3")
  56. };
  57. [ParamsSource(nameof(DatasetConfigurations))]
  58. public BenchmarkDatasetConfig Dataset { get; set; }
  59. private MessageParser parser;
  60. /// <summary>
  61. /// Each data set can contain multiple messages in a single file.
  62. /// Each "write" operation should write each message in turn, and each "parse"
  63. /// operation should parse each message in turn.
  64. /// </summary>
  65. private List<SubTest> subTests;
  66. [GlobalSetup]
  67. public void GlobalSetup()
  68. {
  69. parser = Dataset.Parser;
  70. subTests = Dataset.Payloads.Select(p => new SubTest(p, parser.ParseFrom(p))).ToList();
  71. }
  72. [Benchmark]
  73. public void WriteToStream() => subTests.ForEach(item => item.WriteToStream());
  74. [Benchmark]
  75. public void ToByteArray() => subTests.ForEach(item => item.ToByteArray());
  76. [Benchmark]
  77. public void ParseFromByteArray() => subTests.ForEach(item => item.ParseFromByteArray(parser));
  78. [Benchmark]
  79. public void ParseFromStream() => subTests.ForEach(item => item.ParseFromStream(parser));
  80. private class SubTest
  81. {
  82. private readonly Stream destinationStream;
  83. private readonly Stream sourceStream;
  84. private readonly byte[] data;
  85. private readonly IMessage message;
  86. public SubTest(byte[] data, IMessage message)
  87. {
  88. destinationStream = new MemoryStream(data.Length);
  89. sourceStream = new MemoryStream(data);
  90. this.data = data;
  91. this.message = message;
  92. }
  93. public void Reset() => destinationStream.Position = 0;
  94. public void WriteToStream()
  95. {
  96. destinationStream.Position = 0;
  97. message.WriteTo(destinationStream);
  98. }
  99. public void ToByteArray() => message.ToByteArray();
  100. public void ParseFromByteArray(MessageParser parser) => parser.ParseFrom(data);
  101. public void ParseFromStream(MessageParser parser)
  102. {
  103. sourceStream.Position = 0;
  104. parser.ParseFrom(sourceStream);
  105. }
  106. }
  107. }
  108. }