1
0

NetSerializerIntBenchmark.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Buffers.Binary;
  3. using System.IO;
  4. using BenchmarkDotNet.Attributes;
  5. using Robust.Shared.Analyzers;
  6. namespace Content.Benchmarks
  7. {
  8. [SimpleJob]
  9. [Virtual]
  10. public class NetSerializerIntBenchmark
  11. {
  12. private MemoryStream _writeStream;
  13. private MemoryStream _readStream;
  14. private readonly ushort _x16 = 5;
  15. private readonly uint _x32 = 5;
  16. private readonly ulong _x64 = 5;
  17. private ushort _read16;
  18. private uint _read32;
  19. private ulong _read64;
  20. [GlobalSetup]
  21. public void Setup()
  22. {
  23. _writeStream = new MemoryStream(64);
  24. _readStream = new MemoryStream();
  25. _readStream.Write(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 });
  26. }
  27. [Benchmark]
  28. public void BenchWrite16Span()
  29. {
  30. _writeStream.Position = 0;
  31. WriteUInt16Span(_writeStream, _x16);
  32. }
  33. [Benchmark]
  34. public void BenchWrite32Span()
  35. {
  36. _writeStream.Position = 0;
  37. WriteUInt32Span(_writeStream, _x32);
  38. }
  39. [Benchmark]
  40. public void BenchWrite64Span()
  41. {
  42. _writeStream.Position = 0;
  43. WriteUInt64Span(_writeStream, _x64);
  44. }
  45. [Benchmark]
  46. public void BenchRead16Span()
  47. {
  48. _readStream.Position = 0;
  49. _read16 = ReadUInt16Span(_readStream);
  50. }
  51. [Benchmark]
  52. public void BenchRead32Span()
  53. {
  54. _readStream.Position = 0;
  55. _read32 = ReadUInt32Span(_readStream);
  56. }
  57. [Benchmark]
  58. public void BenchRead64Span()
  59. {
  60. _readStream.Position = 0;
  61. _read64 = ReadUInt64Span(_readStream);
  62. }
  63. [Benchmark]
  64. public void BenchWrite16Byte()
  65. {
  66. _writeStream.Position = 0;
  67. WriteUInt16Byte(_writeStream, _x16);
  68. }
  69. [Benchmark]
  70. public void BenchWrite32Byte()
  71. {
  72. _writeStream.Position = 0;
  73. WriteUInt32Byte(_writeStream, _x32);
  74. }
  75. [Benchmark]
  76. public void BenchWrite64Byte()
  77. {
  78. _writeStream.Position = 0;
  79. WriteUInt64Byte(_writeStream, _x64);
  80. }
  81. [Benchmark]
  82. public void BenchRead16Byte()
  83. {
  84. _readStream.Position = 0;
  85. _read16 = ReadUInt16Byte(_readStream);
  86. }
  87. [Benchmark]
  88. public void BenchRead32Byte()
  89. {
  90. _readStream.Position = 0;
  91. _read32 = ReadUInt32Byte(_readStream);
  92. }
  93. [Benchmark]
  94. public void BenchRead64Byte()
  95. {
  96. _readStream.Position = 0;
  97. _read64 = ReadUInt64Byte(_readStream);
  98. }
  99. private static void WriteUInt16Byte(Stream stream, ushort value)
  100. {
  101. stream.WriteByte((byte) value);
  102. stream.WriteByte((byte) (value >> 8));
  103. }
  104. private static void WriteUInt32Byte(Stream stream, uint value)
  105. {
  106. stream.WriteByte((byte) value);
  107. stream.WriteByte((byte) (value >> 8));
  108. stream.WriteByte((byte) (value >> 16));
  109. stream.WriteByte((byte) (value >> 24));
  110. }
  111. private static void WriteUInt64Byte(Stream stream, ulong value)
  112. {
  113. stream.WriteByte((byte) value);
  114. stream.WriteByte((byte) (value >> 8));
  115. stream.WriteByte((byte) (value >> 16));
  116. stream.WriteByte((byte) (value >> 24));
  117. stream.WriteByte((byte) (value >> 32));
  118. stream.WriteByte((byte) (value >> 40));
  119. stream.WriteByte((byte) (value >> 48));
  120. stream.WriteByte((byte) (value >> 56));
  121. }
  122. private static ushort ReadUInt16Byte(Stream stream)
  123. {
  124. ushort a = 0;
  125. for (var i = 0; i < 16; i += 8)
  126. {
  127. var val = stream.ReadByte();
  128. if (val == -1)
  129. throw new EndOfStreamException();
  130. a |= (ushort) (val << i);
  131. }
  132. return a;
  133. }
  134. private static uint ReadUInt32Byte(Stream stream)
  135. {
  136. uint a = 0;
  137. for (var i = 0; i < 32; i += 8)
  138. {
  139. var val = stream.ReadByte();
  140. if (val == -1)
  141. throw new EndOfStreamException();
  142. a |= (uint) val << i;
  143. }
  144. return a;
  145. }
  146. private static ulong ReadUInt64Byte(Stream stream)
  147. {
  148. ulong a = 0;
  149. for (var i = 0; i < 64; i += 8)
  150. {
  151. var val = stream.ReadByte();
  152. if (val == -1)
  153. throw new EndOfStreamException();
  154. a |= (ulong) val << i;
  155. }
  156. return a;
  157. }
  158. private static void WriteUInt16Span(Stream stream, ushort value)
  159. {
  160. Span<byte> buf = stackalloc byte[2];
  161. BinaryPrimitives.WriteUInt16LittleEndian(buf, value);
  162. stream.Write(buf);
  163. }
  164. private static void WriteUInt32Span(Stream stream, uint value)
  165. {
  166. Span<byte> buf = stackalloc byte[4];
  167. BinaryPrimitives.WriteUInt32LittleEndian(buf, value);
  168. stream.Write(buf);
  169. }
  170. private static void WriteUInt64Span(Stream stream, ulong value)
  171. {
  172. Span<byte> buf = stackalloc byte[8];
  173. BinaryPrimitives.WriteUInt64LittleEndian(buf, value);
  174. stream.Write(buf);
  175. }
  176. private static ushort ReadUInt16Span(Stream stream)
  177. {
  178. Span<byte> buf = stackalloc byte[2];
  179. var wSpan = buf;
  180. while (true)
  181. {
  182. var read = stream.Read(wSpan);
  183. if (read == 0)
  184. throw new EndOfStreamException();
  185. if (read == wSpan.Length)
  186. break;
  187. wSpan = wSpan[read..];
  188. }
  189. return BinaryPrimitives.ReadUInt16LittleEndian(buf);
  190. }
  191. private static uint ReadUInt32Span(Stream stream)
  192. {
  193. Span<byte> buf = stackalloc byte[4];
  194. var wSpan = buf;
  195. while (true)
  196. {
  197. var read = stream.Read(wSpan);
  198. if (read == 0)
  199. throw new EndOfStreamException();
  200. if (read == wSpan.Length)
  201. break;
  202. wSpan = wSpan[read..];
  203. }
  204. return BinaryPrimitives.ReadUInt32LittleEndian(buf);
  205. }
  206. private static ulong ReadUInt64Span(Stream stream)
  207. {
  208. Span<byte> buf = stackalloc byte[8];
  209. var wSpan = buf;
  210. while (true)
  211. {
  212. var read = stream.Read(wSpan);
  213. if (read == 0)
  214. throw new EndOfStreamException();
  215. if (read == wSpan.Length)
  216. break;
  217. wSpan = wSpan[read..];
  218. }
  219. return BinaryPrimitives.ReadUInt64LittleEndian(buf);
  220. }
  221. }
  222. }