WriteRawPrimitivesBenchmark.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. using System.Buffers;
  38. using System.Text;
  39. namespace Google.Protobuf.Benchmarks
  40. {
  41. /// <summary>
  42. /// Benchmarks throughput when writing raw primitives.
  43. /// </summary>
  44. [MemoryDiagnoser]
  45. public class WriteRawPrimitivesBenchmark
  46. {
  47. // key is the encodedSize of varint values
  48. Dictionary<int, uint[]> varint32Values;
  49. Dictionary<int, ulong[]> varint64Values;
  50. double[] doubleValues;
  51. float[] floatValues;
  52. // key is the encodedSize of string values
  53. Dictionary<int, string[]> stringValues;
  54. // key is the encodedSize of string values
  55. Dictionary<int, string[]> nonAsciiStringValues;
  56. // key is the encodedSize of string values
  57. Dictionary<int, ByteString[]> byteStringValues;
  58. // the buffer to which all the data will be written
  59. byte[] outputBuffer;
  60. Random random = new Random(417384220); // random but deterministic seed
  61. public IEnumerable<int> StringEncodedSizes => new[] { 1, 4, 10, 105, 10080 };
  62. public IEnumerable<int> NonAsciiStringEncodedSizes => new[] { 4, 10, 105, 10080 };
  63. [GlobalSetup]
  64. public void GlobalSetup()
  65. {
  66. outputBuffer = new byte[BytesToWrite];
  67. varint32Values = new Dictionary<int, uint[]>();
  68. varint64Values = new Dictionary<int, ulong[]>();
  69. for (int encodedSize = 1; encodedSize <= 10; encodedSize++)
  70. {
  71. if (encodedSize <= 5)
  72. {
  73. varint32Values.Add(encodedSize, CreateRandomVarints32(random, BytesToWrite / encodedSize, encodedSize));
  74. }
  75. varint64Values.Add(encodedSize, CreateRandomVarints64(random, BytesToWrite / encodedSize, encodedSize));
  76. }
  77. doubleValues = CreateRandomDoubles(random, BytesToWrite / sizeof(double));
  78. floatValues = CreateRandomFloats(random, BytesToWrite / sizeof(float));
  79. stringValues = new Dictionary<int, string[]>();
  80. byteStringValues = new Dictionary<int, ByteString[]>();
  81. foreach(var encodedSize in StringEncodedSizes)
  82. {
  83. stringValues.Add(encodedSize, CreateStrings(BytesToWrite / encodedSize, encodedSize));
  84. byteStringValues.Add(encodedSize, CreateByteStrings(BytesToWrite / encodedSize, encodedSize));
  85. }
  86. nonAsciiStringValues = new Dictionary<int, string[]>();
  87. foreach(var encodedSize in NonAsciiStringEncodedSizes)
  88. {
  89. nonAsciiStringValues.Add(encodedSize, CreateNonAsciiStrings(BytesToWrite / encodedSize, encodedSize));
  90. }
  91. }
  92. // Total number of bytes that each benchmark will write.
  93. // Measuring the time taken to write buffer of given size makes it easier to compare parsing speed for different
  94. // types and makes it easy to calculate the througput (in MB/s)
  95. // 10800 bytes is chosen because it is divisible by all possible encoded sizes for all primitive types {1..10}
  96. [Params(10080)]
  97. public int BytesToWrite { get; set; }
  98. [Benchmark]
  99. [Arguments(1)]
  100. [Arguments(2)]
  101. [Arguments(3)]
  102. [Arguments(4)]
  103. [Arguments(5)]
  104. public void WriteRawVarint32_CodedOutputStream(int encodedSize)
  105. {
  106. var values = varint32Values[encodedSize];
  107. var cos = new CodedOutputStream(outputBuffer);
  108. foreach (var value in values)
  109. {
  110. cos.WriteRawVarint32(value);
  111. }
  112. cos.Flush();
  113. cos.CheckNoSpaceLeft();
  114. }
  115. [Benchmark]
  116. [Arguments(1)]
  117. [Arguments(2)]
  118. [Arguments(3)]
  119. [Arguments(4)]
  120. [Arguments(5)]
  121. public void WriteRawVarint32_WriteContext(int encodedSize)
  122. {
  123. var values = varint32Values[encodedSize];
  124. var span = new Span<byte>(outputBuffer);
  125. WriteContext.Initialize(ref span, out WriteContext ctx);
  126. foreach (var value in values)
  127. {
  128. ctx.WriteUInt32(value);
  129. }
  130. ctx.Flush();
  131. ctx.CheckNoSpaceLeft();
  132. }
  133. [Benchmark]
  134. [Arguments(1)]
  135. [Arguments(2)]
  136. [Arguments(3)]
  137. [Arguments(4)]
  138. [Arguments(5)]
  139. [Arguments(6)]
  140. [Arguments(7)]
  141. [Arguments(8)]
  142. [Arguments(9)]
  143. [Arguments(10)]
  144. public void WriteRawVarint64_CodedOutputStream(int encodedSize)
  145. {
  146. var values = varint64Values[encodedSize];
  147. var cos = new CodedOutputStream(outputBuffer);
  148. foreach (var value in values)
  149. {
  150. cos.WriteRawVarint64(value);
  151. }
  152. cos.Flush();
  153. cos.CheckNoSpaceLeft();
  154. }
  155. [Benchmark]
  156. [Arguments(1)]
  157. [Arguments(2)]
  158. [Arguments(3)]
  159. [Arguments(4)]
  160. [Arguments(5)]
  161. [Arguments(6)]
  162. [Arguments(7)]
  163. [Arguments(8)]
  164. [Arguments(9)]
  165. [Arguments(10)]
  166. public void WriteRawVarint64_WriteContext(int encodedSize)
  167. {
  168. var values = varint64Values[encodedSize];
  169. var span = new Span<byte>(outputBuffer);
  170. WriteContext.Initialize(ref span, out WriteContext ctx);
  171. foreach (var value in values)
  172. {
  173. ctx.WriteUInt64(value);
  174. }
  175. ctx.Flush();
  176. ctx.CheckNoSpaceLeft();
  177. }
  178. [Benchmark]
  179. public void WriteFixed32_CodedOutputStream()
  180. {
  181. const int encodedSize = sizeof(uint);
  182. var cos = new CodedOutputStream(outputBuffer);
  183. for(int i = 0; i < BytesToWrite / encodedSize; i++)
  184. {
  185. cos.WriteFixed32(12345);
  186. }
  187. cos.Flush();
  188. cos.CheckNoSpaceLeft();
  189. }
  190. [Benchmark]
  191. public void WriteFixed32_WriteContext()
  192. {
  193. const int encodedSize = sizeof(uint);
  194. var span = new Span<byte>(outputBuffer);
  195. WriteContext.Initialize(ref span, out WriteContext ctx);
  196. for (uint i = 0; i < BytesToWrite / encodedSize; i++)
  197. {
  198. ctx.WriteFixed32(12345);
  199. }
  200. ctx.Flush();
  201. ctx.CheckNoSpaceLeft();
  202. }
  203. [Benchmark]
  204. public void WriteFixed64_CodedOutputStream()
  205. {
  206. const int encodedSize = sizeof(ulong);
  207. var cos = new CodedOutputStream(outputBuffer);
  208. for(int i = 0; i < BytesToWrite / encodedSize; i++)
  209. {
  210. cos.WriteFixed64(123456789);
  211. }
  212. cos.Flush();
  213. cos.CheckNoSpaceLeft();
  214. }
  215. [Benchmark]
  216. public void WriteFixed64_WriteContext()
  217. {
  218. const int encodedSize = sizeof(ulong);
  219. var span = new Span<byte>(outputBuffer);
  220. WriteContext.Initialize(ref span, out WriteContext ctx);
  221. for (uint i = 0; i < BytesToWrite / encodedSize; i++)
  222. {
  223. ctx.WriteFixed64(123456789);
  224. }
  225. ctx.Flush();
  226. ctx.CheckNoSpaceLeft();
  227. }
  228. [Benchmark]
  229. public void WriteRawFloat_CodedOutputStream()
  230. {
  231. var cos = new CodedOutputStream(outputBuffer);
  232. foreach (var value in floatValues)
  233. {
  234. cos.WriteFloat(value);
  235. }
  236. cos.Flush();
  237. cos.CheckNoSpaceLeft();
  238. }
  239. [Benchmark]
  240. public void WriteRawFloat_WriteContext()
  241. {
  242. var span = new Span<byte>(outputBuffer);
  243. WriteContext.Initialize(ref span, out WriteContext ctx);
  244. foreach (var value in floatValues)
  245. {
  246. ctx.WriteFloat(value);
  247. }
  248. ctx.Flush();
  249. ctx.CheckNoSpaceLeft();
  250. }
  251. [Benchmark]
  252. public void WriteRawDouble_CodedOutputStream()
  253. {
  254. var cos = new CodedOutputStream(outputBuffer);
  255. foreach (var value in doubleValues)
  256. {
  257. cos.WriteDouble(value);
  258. }
  259. cos.Flush();
  260. cos.CheckNoSpaceLeft();
  261. }
  262. [Benchmark]
  263. public void WriteRawDouble_WriteContext()
  264. {
  265. var span = new Span<byte>(outputBuffer);
  266. WriteContext.Initialize(ref span, out WriteContext ctx);
  267. foreach (var value in doubleValues)
  268. {
  269. ctx.WriteDouble(value);
  270. }
  271. ctx.Flush();
  272. ctx.CheckNoSpaceLeft();
  273. }
  274. [Benchmark]
  275. [ArgumentsSource(nameof(StringEncodedSizes))]
  276. public void WriteString_CodedOutputStream(int encodedSize)
  277. {
  278. var values = stringValues[encodedSize];
  279. var cos = new CodedOutputStream(outputBuffer);
  280. foreach (var value in values)
  281. {
  282. cos.WriteString(value);
  283. }
  284. cos.Flush();
  285. cos.CheckNoSpaceLeft();
  286. }
  287. [Benchmark]
  288. [ArgumentsSource(nameof(StringEncodedSizes))]
  289. public void WriteString_WriteContext(int encodedSize)
  290. {
  291. var values = stringValues[encodedSize];
  292. var span = new Span<byte>(outputBuffer);
  293. WriteContext.Initialize(ref span, out WriteContext ctx);
  294. foreach (var value in values)
  295. {
  296. ctx.WriteString(value);
  297. }
  298. ctx.Flush();
  299. ctx.CheckNoSpaceLeft();
  300. }
  301. [Benchmark]
  302. [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
  303. public void WriteNonAsciiString_CodedOutputStream(int encodedSize)
  304. {
  305. var values = nonAsciiStringValues[encodedSize];
  306. var cos = new CodedOutputStream(outputBuffer);
  307. foreach (var value in values)
  308. {
  309. cos.WriteString(value);
  310. }
  311. cos.Flush();
  312. cos.CheckNoSpaceLeft();
  313. }
  314. [Benchmark]
  315. [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
  316. public void WriteNonAsciiString_WriteContext(int encodedSize)
  317. {
  318. var values = nonAsciiStringValues[encodedSize];
  319. var span = new Span<byte>(outputBuffer);
  320. WriteContext.Initialize(ref span, out WriteContext ctx);
  321. foreach (var value in values)
  322. {
  323. ctx.WriteString(value);
  324. }
  325. ctx.Flush();
  326. ctx.CheckNoSpaceLeft();
  327. }
  328. [Benchmark]
  329. [ArgumentsSource(nameof(StringEncodedSizes))]
  330. public void WriteBytes_CodedOutputStream(int encodedSize)
  331. {
  332. var values = byteStringValues[encodedSize];
  333. var cos = new CodedOutputStream(outputBuffer);
  334. foreach (var value in values)
  335. {
  336. cos.WriteBytes(value);
  337. }
  338. cos.Flush();
  339. cos.CheckNoSpaceLeft();
  340. }
  341. [Benchmark]
  342. [ArgumentsSource(nameof(StringEncodedSizes))]
  343. public void WriteBytes_WriteContext(int encodedSize)
  344. {
  345. var values = byteStringValues[encodedSize];
  346. var span = new Span<byte>(outputBuffer);
  347. WriteContext.Initialize(ref span, out WriteContext ctx);
  348. foreach (var value in values)
  349. {
  350. ctx.WriteBytes(value);
  351. }
  352. ctx.Flush();
  353. ctx.CheckNoSpaceLeft();
  354. }
  355. private static uint[] CreateRandomVarints32(Random random, int valueCount, int encodedSize)
  356. {
  357. var result = new uint[valueCount];
  358. for (int i = 0; i < valueCount; i++)
  359. {
  360. result[i] = (uint) ParseRawPrimitivesBenchmark.RandomUnsignedVarint(random, encodedSize, true);
  361. }
  362. return result;
  363. }
  364. private static ulong[] CreateRandomVarints64(Random random, int valueCount, int encodedSize)
  365. {
  366. var result = new ulong[valueCount];
  367. for (int i = 0; i < valueCount; i++)
  368. {
  369. result[i] = ParseRawPrimitivesBenchmark.RandomUnsignedVarint(random, encodedSize, false);
  370. }
  371. return result;
  372. }
  373. private static float[] CreateRandomFloats(Random random, int valueCount)
  374. {
  375. var result = new float[valueCount];
  376. for (int i = 0; i < valueCount; i++)
  377. {
  378. result[i] = (float)random.NextDouble();
  379. }
  380. return result;
  381. }
  382. private static double[] CreateRandomDoubles(Random random, int valueCount)
  383. {
  384. var result = new double[valueCount];
  385. for (int i = 0; i < valueCount; i++)
  386. {
  387. result[i] = random.NextDouble();
  388. }
  389. return result;
  390. }
  391. private static string[] CreateStrings(int valueCount, int encodedSize)
  392. {
  393. var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
  394. var result = new string[valueCount];
  395. for (int i = 0; i < valueCount; i++)
  396. {
  397. result[i] = str;
  398. }
  399. return result;
  400. }
  401. private static string[] CreateNonAsciiStrings(int valueCount, int encodedSize)
  402. {
  403. var str = ParseRawPrimitivesBenchmark.CreateNonAsciiStringWithEncodedSize(encodedSize);
  404. var result = new string[valueCount];
  405. for (int i = 0; i < valueCount; i++)
  406. {
  407. result[i] = str;
  408. }
  409. return result;
  410. }
  411. private static ByteString[] CreateByteStrings(int valueCount, int encodedSize)
  412. {
  413. var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
  414. var result = new ByteString[valueCount];
  415. for (int i = 0; i < valueCount; i++)
  416. {
  417. result[i] = ByteString.CopyFrom(Encoding.UTF8.GetBytes(str));
  418. }
  419. return result;
  420. }
  421. }
  422. }