WriteRawPrimitivesBenchmark.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. for (int i = 0; i < values.Length; i++)
  109. {
  110. cos.WriteRawVarint32(values[i]);
  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. for (int i = 0; i < values.Length; i++)
  127. {
  128. ctx.WriteUInt32(values[i]);
  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. for (int i = 0; i < values.Length; i++)
  149. {
  150. cos.WriteRawVarint64(values[i]);
  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. for (int i = 0; i < values.Length; i++)
  172. {
  173. ctx.WriteUInt64(values[i]);
  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 WriteRawTag_OneByte_WriteContext()
  230. {
  231. const int encodedSize = 1;
  232. var span = new Span<byte>(outputBuffer);
  233. WriteContext.Initialize(ref span, out WriteContext ctx);
  234. for (uint i = 0; i < BytesToWrite / encodedSize; i++)
  235. {
  236. ctx.WriteRawTag(16);
  237. }
  238. ctx.Flush();
  239. ctx.CheckNoSpaceLeft();
  240. }
  241. [Benchmark]
  242. public void WriteRawTag_TwoBytes_WriteContext()
  243. {
  244. const int encodedSize = 2;
  245. var span = new Span<byte>(outputBuffer);
  246. WriteContext.Initialize(ref span, out WriteContext ctx);
  247. for (uint i = 0; i < BytesToWrite / encodedSize; i++)
  248. {
  249. ctx.WriteRawTag(137, 6);
  250. }
  251. ctx.Flush();
  252. ctx.CheckNoSpaceLeft();
  253. }
  254. [Benchmark]
  255. public void WriteRawTag_ThreeBytes_WriteContext()
  256. {
  257. const int encodedSize = 3;
  258. var span = new Span<byte>(outputBuffer);
  259. WriteContext.Initialize(ref span, out WriteContext ctx);
  260. for (uint i = 0; i < BytesToWrite / encodedSize; i++)
  261. {
  262. ctx.WriteRawTag(160, 131, 1);
  263. }
  264. ctx.Flush();
  265. ctx.CheckNoSpaceLeft();
  266. }
  267. [Benchmark]
  268. public void Baseline_WriteContext()
  269. {
  270. var span = new Span<byte>(outputBuffer);
  271. WriteContext.Initialize(ref span, out WriteContext ctx);
  272. ctx.state.position = outputBuffer.Length;
  273. ctx.Flush();
  274. ctx.CheckNoSpaceLeft();
  275. }
  276. [Benchmark]
  277. public void WriteRawFloat_CodedOutputStream()
  278. {
  279. var cos = new CodedOutputStream(outputBuffer);
  280. foreach (var value in floatValues)
  281. {
  282. cos.WriteFloat(value);
  283. }
  284. cos.Flush();
  285. cos.CheckNoSpaceLeft();
  286. }
  287. [Benchmark]
  288. public void WriteRawFloat_WriteContext()
  289. {
  290. var span = new Span<byte>(outputBuffer);
  291. WriteContext.Initialize(ref span, out WriteContext ctx);
  292. foreach (var value in floatValues)
  293. {
  294. ctx.WriteFloat(value);
  295. }
  296. ctx.Flush();
  297. ctx.CheckNoSpaceLeft();
  298. }
  299. [Benchmark]
  300. public void WriteRawDouble_CodedOutputStream()
  301. {
  302. var cos = new CodedOutputStream(outputBuffer);
  303. foreach (var value in doubleValues)
  304. {
  305. cos.WriteDouble(value);
  306. }
  307. cos.Flush();
  308. cos.CheckNoSpaceLeft();
  309. }
  310. [Benchmark]
  311. public void WriteRawDouble_WriteContext()
  312. {
  313. var span = new Span<byte>(outputBuffer);
  314. WriteContext.Initialize(ref span, out WriteContext ctx);
  315. foreach (var value in doubleValues)
  316. {
  317. ctx.WriteDouble(value);
  318. }
  319. ctx.Flush();
  320. ctx.CheckNoSpaceLeft();
  321. }
  322. [Benchmark]
  323. [ArgumentsSource(nameof(StringEncodedSizes))]
  324. public void WriteString_CodedOutputStream(int encodedSize)
  325. {
  326. var values = stringValues[encodedSize];
  327. var cos = new CodedOutputStream(outputBuffer);
  328. foreach (var value in values)
  329. {
  330. cos.WriteString(value);
  331. }
  332. cos.Flush();
  333. cos.CheckNoSpaceLeft();
  334. }
  335. [Benchmark]
  336. [ArgumentsSource(nameof(StringEncodedSizes))]
  337. public void WriteString_WriteContext(int encodedSize)
  338. {
  339. var values = stringValues[encodedSize];
  340. var span = new Span<byte>(outputBuffer);
  341. WriteContext.Initialize(ref span, out WriteContext ctx);
  342. foreach (var value in values)
  343. {
  344. ctx.WriteString(value);
  345. }
  346. ctx.Flush();
  347. ctx.CheckNoSpaceLeft();
  348. }
  349. [Benchmark]
  350. [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
  351. public void WriteNonAsciiString_CodedOutputStream(int encodedSize)
  352. {
  353. var values = nonAsciiStringValues[encodedSize];
  354. var cos = new CodedOutputStream(outputBuffer);
  355. foreach (var value in values)
  356. {
  357. cos.WriteString(value);
  358. }
  359. cos.Flush();
  360. cos.CheckNoSpaceLeft();
  361. }
  362. [Benchmark]
  363. [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
  364. public void WriteNonAsciiString_WriteContext(int encodedSize)
  365. {
  366. var values = nonAsciiStringValues[encodedSize];
  367. var span = new Span<byte>(outputBuffer);
  368. WriteContext.Initialize(ref span, out WriteContext ctx);
  369. foreach (var value in values)
  370. {
  371. ctx.WriteString(value);
  372. }
  373. ctx.Flush();
  374. ctx.CheckNoSpaceLeft();
  375. }
  376. [Benchmark]
  377. [ArgumentsSource(nameof(StringEncodedSizes))]
  378. public void WriteBytes_CodedOutputStream(int encodedSize)
  379. {
  380. var values = byteStringValues[encodedSize];
  381. var cos = new CodedOutputStream(outputBuffer);
  382. foreach (var value in values)
  383. {
  384. cos.WriteBytes(value);
  385. }
  386. cos.Flush();
  387. cos.CheckNoSpaceLeft();
  388. }
  389. [Benchmark]
  390. [ArgumentsSource(nameof(StringEncodedSizes))]
  391. public void WriteBytes_WriteContext(int encodedSize)
  392. {
  393. var values = byteStringValues[encodedSize];
  394. var span = new Span<byte>(outputBuffer);
  395. WriteContext.Initialize(ref span, out WriteContext ctx);
  396. foreach (var value in values)
  397. {
  398. ctx.WriteBytes(value);
  399. }
  400. ctx.Flush();
  401. ctx.CheckNoSpaceLeft();
  402. }
  403. private static uint[] CreateRandomVarints32(Random random, int valueCount, int encodedSize)
  404. {
  405. var result = new uint[valueCount];
  406. for (int i = 0; i < valueCount; i++)
  407. {
  408. result[i] = (uint) ParseRawPrimitivesBenchmark.RandomUnsignedVarint(random, encodedSize, true);
  409. }
  410. return result;
  411. }
  412. private static ulong[] CreateRandomVarints64(Random random, int valueCount, int encodedSize)
  413. {
  414. var result = new ulong[valueCount];
  415. for (int i = 0; i < valueCount; i++)
  416. {
  417. result[i] = ParseRawPrimitivesBenchmark.RandomUnsignedVarint(random, encodedSize, false);
  418. }
  419. return result;
  420. }
  421. private static float[] CreateRandomFloats(Random random, int valueCount)
  422. {
  423. var result = new float[valueCount];
  424. for (int i = 0; i < valueCount; i++)
  425. {
  426. result[i] = (float)random.NextDouble();
  427. }
  428. return result;
  429. }
  430. private static double[] CreateRandomDoubles(Random random, int valueCount)
  431. {
  432. var result = new double[valueCount];
  433. for (int i = 0; i < valueCount; i++)
  434. {
  435. result[i] = random.NextDouble();
  436. }
  437. return result;
  438. }
  439. private static string[] CreateStrings(int valueCount, int encodedSize)
  440. {
  441. var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
  442. var result = new string[valueCount];
  443. for (int i = 0; i < valueCount; i++)
  444. {
  445. result[i] = str;
  446. }
  447. return result;
  448. }
  449. private static string[] CreateNonAsciiStrings(int valueCount, int encodedSize)
  450. {
  451. var str = ParseRawPrimitivesBenchmark.CreateNonAsciiStringWithEncodedSize(encodedSize);
  452. var result = new string[valueCount];
  453. for (int i = 0; i < valueCount; i++)
  454. {
  455. result[i] = str;
  456. }
  457. return result;
  458. }
  459. private static ByteString[] CreateByteStrings(int valueCount, int encodedSize)
  460. {
  461. var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
  462. var result = new ByteString[valueCount];
  463. for (int i = 0; i < valueCount; i++)
  464. {
  465. result[i] = ByteString.CopyFrom(Encoding.UTF8.GetBytes(str));
  466. }
  467. return result;
  468. }
  469. }
  470. }