SerializationBenchmark.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 (to a MemoryStream) and deserializing (from a ByteString).
  40. /// Over time we may wish to test the various different approaches to serialization and deserialization separately.
  41. /// </summary>
  42. [MemoryDiagnoser]
  43. public class SerializationBenchmark
  44. {
  45. /// <summary>
  46. /// All the configurations to be tested. Add more datasets to the array as they're available.
  47. /// (When C# supports proto2, this will increase significantly.)
  48. /// </summary>
  49. public static SerializationConfig[] Configurations => new[]
  50. {
  51. new SerializationConfig("dataset.google_message1_proto3.pb")
  52. };
  53. [ParamsSource(nameof(Configurations))]
  54. public SerializationConfig Configuration { get; set; }
  55. private MessageParser parser;
  56. /// <summary>
  57. /// Each data set can contain multiple messages in a single file.
  58. /// Each "write" operation should write each message in turn, and each "parse"
  59. /// operation should parse each message in turn.
  60. /// </summary>
  61. private List<SubTest> subTests;
  62. [GlobalSetup]
  63. public void GlobalSetup()
  64. {
  65. parser = Configuration.Parser;
  66. subTests = Configuration.Payloads.Select(p => new SubTest(p, parser.ParseFrom(p))).ToList();
  67. }
  68. [Benchmark]
  69. public void WriteToStream() => subTests.ForEach(item => item.WriteToStream());
  70. [Benchmark]
  71. public void ToByteArray() => subTests.ForEach(item => item.ToByteArray());
  72. [Benchmark]
  73. public void ParseFromByteString() => subTests.ForEach(item => item.ParseFromByteString(parser));
  74. [Benchmark]
  75. public void ParseFromStream() => subTests.ForEach(item => item.ParseFromStream(parser));
  76. private class SubTest
  77. {
  78. private readonly Stream destinationStream;
  79. private readonly Stream sourceStream;
  80. private readonly ByteString data;
  81. private readonly IMessage message;
  82. public SubTest(ByteString data, IMessage message)
  83. {
  84. destinationStream = new MemoryStream(data.Length);
  85. sourceStream = new MemoryStream(data.ToByteArray());
  86. this.data = data;
  87. this.message = message;
  88. }
  89. public void Reset() => destinationStream.Position = 0;
  90. public void WriteToStream()
  91. {
  92. destinationStream.Position = 0;
  93. message.WriteTo(destinationStream);
  94. }
  95. public void ToByteArray() => message.ToByteArray();
  96. public void ParseFromByteString(MessageParser parser) => parser.ParseFrom(data);
  97. public void ParseFromStream(MessageParser parser)
  98. {
  99. sourceStream.Position = 0;
  100. parser.ParseFrom(sourceStream);
  101. }
  102. }
  103. }
  104. }