選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SerializerBenchmark.cs 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Packets;
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Adapter;
  7. using MQTTnet.Channel;
  8. using MQTTnet.Formatter;
  9. using MQTTnet.Formatter.V3;
  10. namespace MQTTnet.Benchmarks
  11. {
  12. [ClrJob]
  13. [RPlotExporter]
  14. [MemoryDiagnoser]
  15. public class SerializerBenchmark
  16. {
  17. private MqttBasePacket _packet;
  18. private ArraySegment<byte> _serializedPacket;
  19. private IMqttPacketFormatter _serializer;
  20. [GlobalSetup]
  21. public void Setup()
  22. {
  23. _packet = new MqttPublishPacket
  24. {
  25. Topic = "A"
  26. };
  27. _serializer = new MqttV311PacketFormatter();
  28. _serializedPacket = _serializer.Encode(_packet);
  29. }
  30. [Benchmark]
  31. public void Serialize_10000_Messages()
  32. {
  33. for (var i = 0; i < 10000; i++)
  34. {
  35. _serializer.Encode(_packet);
  36. _serializer.FreeBuffer();
  37. }
  38. }
  39. [Benchmark]
  40. public void Deserialize_10000_Messages()
  41. {
  42. var channel = new BenchmarkMqttChannel(_serializedPacket);
  43. var fixedHeader = new byte[2];
  44. var reader = new MqttPacketReader(channel);
  45. for (var i = 0; i < 10000; i++)
  46. {
  47. channel.Reset();
  48. var header = reader.ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader;
  49. var receivedPacket = new ReceivedMqttPacket(
  50. header.Flags,
  51. new MqttPacketBodyReader(_serializedPacket.Array, (ulong)(_serializedPacket.Count - header.RemainingLength), (ulong)_serializedPacket.Array.Length), 0);
  52. _serializer.Decode(receivedPacket);
  53. }
  54. }
  55. private class BenchmarkMqttChannel : IMqttChannel
  56. {
  57. private readonly ArraySegment<byte> _buffer;
  58. private int _position;
  59. public BenchmarkMqttChannel(ArraySegment<byte> buffer)
  60. {
  61. _buffer = buffer;
  62. _position = _buffer.Offset;
  63. }
  64. public string Endpoint { get; } = string.Empty;
  65. public bool IsSecureConnection { get; } = false;
  66. public void Reset()
  67. {
  68. _position = _buffer.Offset;
  69. }
  70. public Task ConnectAsync(CancellationToken cancellationToken)
  71. {
  72. throw new NotSupportedException();
  73. }
  74. public Task DisconnectAsync(CancellationToken cancellationToken)
  75. {
  76. throw new NotSupportedException();
  77. }
  78. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  79. {
  80. Array.Copy(_buffer.Array, _position, buffer, offset, count);
  81. _position += count;
  82. return Task.FromResult(count);
  83. }
  84. public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  85. {
  86. throw new NotSupportedException();
  87. }
  88. public void Dispose()
  89. {
  90. }
  91. }
  92. }
  93. }