ParseRawPrimitivesBenchmark.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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;
  34. using System.Buffers.Binary;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. namespace Google.Protobuf.Benchmarks
  38. {
  39. /// <summary>
  40. /// Benchmarks throughput when parsing raw primitives.
  41. /// </summary>
  42. [MemoryDiagnoser]
  43. public class ParseRawPrimitivesBenchmark
  44. {
  45. // key is the encodedSize of varint values
  46. Dictionary<int, byte[]> varintInputBuffers;
  47. byte[] doubleInputBuffer;
  48. byte[] floatInputBuffer;
  49. byte[] fixedIntInputBuffer;
  50. Random random = new Random(417384220); // random but deterministic seed
  51. [GlobalSetup]
  52. public void GlobalSetup()
  53. {
  54. // add some extra values that we won't read just to make sure we are far enough from the end of the buffer
  55. // which allows the parser fastpath to always kick in.
  56. const int paddingValueCount = 100;
  57. varintInputBuffers = new Dictionary<int, byte[]>();
  58. for (int encodedSize = 1; encodedSize <= 10; encodedSize++)
  59. {
  60. byte[] buffer = CreateBufferWithRandomVarints(random, BytesToParse / encodedSize, encodedSize, paddingValueCount);
  61. varintInputBuffers.Add(encodedSize, buffer);
  62. }
  63. doubleInputBuffer = CreateBufferWithRandomDoubles(random, BytesToParse / sizeof(double), paddingValueCount);
  64. floatInputBuffer = CreateBufferWithRandomFloats(random, BytesToParse / sizeof(float), paddingValueCount);
  65. fixedIntInputBuffer = CreateBufferWithRandomData(random, BytesToParse / sizeof(long), sizeof(long), paddingValueCount);
  66. }
  67. // Total number of bytes that each benchmark will parse.
  68. // Measuring the time taken to parse buffer of given size makes it easier to compare parsing speed for different
  69. // types and makes it easy to calculate the througput (in MB/s)
  70. // 10800 bytes is chosen because it is divisible by all possible encoded sizes for all primitive types {1..10}
  71. [Params(10080)]
  72. public int BytesToParse { get; set; }
  73. [Benchmark]
  74. [Arguments(1)]
  75. [Arguments(2)]
  76. [Arguments(3)]
  77. [Arguments(4)]
  78. [Arguments(5)]
  79. public int ParseRawVarint32(int encodedSize)
  80. {
  81. CodedInputStream cis = new CodedInputStream(varintInputBuffers[encodedSize]);
  82. int sum = 0;
  83. for (int i = 0; i < BytesToParse / encodedSize; i++)
  84. {
  85. sum += cis.ReadInt32();
  86. }
  87. return sum;
  88. }
  89. [Benchmark]
  90. [Arguments(1)]
  91. [Arguments(2)]
  92. [Arguments(3)]
  93. [Arguments(4)]
  94. [Arguments(5)]
  95. [Arguments(6)]
  96. [Arguments(7)]
  97. [Arguments(8)]
  98. [Arguments(9)]
  99. [Arguments(10)]
  100. public long ParseRawVarint64(int encodedSize)
  101. {
  102. CodedInputStream cis = new CodedInputStream(varintInputBuffers[encodedSize]);
  103. long sum = 0;
  104. for (int i = 0; i < BytesToParse / encodedSize; i++)
  105. {
  106. sum += cis.ReadInt64();
  107. }
  108. return sum;
  109. }
  110. [Benchmark]
  111. public uint ParseFixed32()
  112. {
  113. const int encodedSize = sizeof(uint);
  114. CodedInputStream cis = new CodedInputStream(fixedIntInputBuffer);
  115. uint sum = 0;
  116. for (uint i = 0; i < BytesToParse / encodedSize; i++)
  117. {
  118. sum += cis.ReadFixed32();
  119. }
  120. return sum;
  121. }
  122. [Benchmark]
  123. public ulong ParseFixed64()
  124. {
  125. const int encodedSize = sizeof(ulong);
  126. CodedInputStream cis = new CodedInputStream(fixedIntInputBuffer);
  127. ulong sum = 0;
  128. for (int i = 0; i < BytesToParse / encodedSize; i++)
  129. {
  130. sum += cis.ReadFixed64();
  131. }
  132. return sum;
  133. }
  134. [Benchmark]
  135. public float ParseRawFloat()
  136. {
  137. const int encodedSize = sizeof(float);
  138. CodedInputStream cis = new CodedInputStream(floatInputBuffer);
  139. float sum = 0;
  140. for (int i = 0; i < BytesToParse / encodedSize; i++)
  141. {
  142. sum += cis.ReadFloat();
  143. }
  144. return sum;
  145. }
  146. [Benchmark]
  147. public double ParseRawDouble()
  148. {
  149. const int encodedSize = sizeof(double);
  150. CodedInputStream cis = new CodedInputStream(doubleInputBuffer);
  151. double sum = 0;
  152. for (int i = 0; i < BytesToParse / encodedSize; i++)
  153. {
  154. sum += cis.ReadDouble();
  155. }
  156. return sum;
  157. }
  158. private static byte[] CreateBufferWithRandomVarints(Random random, int valueCount, int encodedSize, int paddingValueCount)
  159. {
  160. MemoryStream ms = new MemoryStream();
  161. CodedOutputStream cos = new CodedOutputStream(ms);
  162. for (int i = 0; i < valueCount + paddingValueCount; i++)
  163. {
  164. cos.WriteUInt64(RandomUnsignedVarint(random, encodedSize));
  165. }
  166. cos.Flush();
  167. var buffer = ms.ToArray();
  168. if (buffer.Length != encodedSize * (valueCount + paddingValueCount))
  169. {
  170. throw new InvalidOperationException($"Unexpected output buffer length {buffer.Length}");
  171. }
  172. return buffer;
  173. }
  174. private static byte[] CreateBufferWithRandomFloats(Random random, int valueCount, int paddingValueCount)
  175. {
  176. MemoryStream ms = new MemoryStream();
  177. CodedOutputStream cos = new CodedOutputStream(ms);
  178. for (int i = 0; i < valueCount + paddingValueCount; i++)
  179. {
  180. cos.WriteFloat((float)random.NextDouble());
  181. }
  182. cos.Flush();
  183. var buffer = ms.ToArray();
  184. return buffer;
  185. }
  186. private static byte[] CreateBufferWithRandomDoubles(Random random, int valueCount, int paddingValueCount)
  187. {
  188. MemoryStream ms = new MemoryStream();
  189. CodedOutputStream cos = new CodedOutputStream(ms);
  190. for (int i = 0; i < valueCount + paddingValueCount; i++)
  191. {
  192. cos.WriteDouble(random.NextDouble());
  193. }
  194. cos.Flush();
  195. var buffer = ms.ToArray();
  196. return buffer;
  197. }
  198. private static byte[] CreateBufferWithRandomData(Random random, int valueCount, int encodedSize, int paddingValueCount)
  199. {
  200. int bufferSize = (valueCount + paddingValueCount) * encodedSize;
  201. byte[] buffer = new byte[bufferSize];
  202. random.NextBytes(buffer);
  203. return buffer;
  204. }
  205. /// <summary>
  206. /// Generate a random value that will take exactly "encodedSize" bytes when varint-encoded.
  207. /// </summary>
  208. private static ulong RandomUnsignedVarint(Random random, int encodedSize)
  209. {
  210. Span<byte> randomBytesBuffer = stackalloc byte[8];
  211. if (encodedSize < 1 || encodedSize > 10)
  212. {
  213. throw new ArgumentException("Illegal encodedSize value requested", nameof(encodedSize));
  214. }
  215. const int bitsPerByte = 7;
  216. ulong result = 0;
  217. while (true)
  218. {
  219. random.NextBytes(randomBytesBuffer);
  220. ulong randomValue = BinaryPrimitives.ReadUInt64LittleEndian(randomBytesBuffer);
  221. // only use the number of random bits we need
  222. ulong bitmask = encodedSize < 10 ? ((1UL << (encodedSize * bitsPerByte)) - 1) : ulong.MaxValue;
  223. result = randomValue & bitmask;
  224. if (encodedSize == 10)
  225. {
  226. // for 10-byte values the highest bit always needs to be set (7*9=63)
  227. result |= ulong.MaxValue;
  228. break;
  229. }
  230. // some random values won't require the full "encodedSize" bytes, check that at least
  231. // one of the top 7 bits is set. Retrying is fine since it only happens rarely
  232. if (encodedSize == 1 || (result & (0x7FUL << ((encodedSize - 1) * bitsPerByte))) != 0)
  233. {
  234. break;
  235. }
  236. }
  237. return result;
  238. }
  239. }
  240. }