You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
3.4 KiB

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