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.
 
 
 
 

113 lines
3.2 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. using MQTTnet.Diagnostics;
  13. namespace MQTTnet.Benchmarks
  14. {
  15. [SimpleJob(RuntimeMoniker.Net461)]
  16. [RPlotExporter]
  17. [MemoryDiagnoser]
  18. public class SerializerBenchmark
  19. {
  20. private MqttBasePacket _packet;
  21. private ArraySegment<byte> _serializedPacket;
  22. private IMqttPacketFormatter _serializer;
  23. [GlobalSetup]
  24. public void Setup()
  25. {
  26. _packet = new MqttPublishPacket
  27. {
  28. Topic = "A"
  29. };
  30. _serializer = new MqttV311PacketFormatter(new MqttPacketWriter());
  31. _serializedPacket = _serializer.Encode(_packet);
  32. }
  33. [Benchmark]
  34. public void Serialize_10000_Messages()
  35. {
  36. for (var i = 0; i < 10000; i++)
  37. {
  38. _serializer.Encode(_packet);
  39. _serializer.FreeBuffer();
  40. }
  41. }
  42. [Benchmark]
  43. public void Deserialize_10000_Messages()
  44. {
  45. var channel = new BenchmarkMqttChannel(_serializedPacket);
  46. var fixedHeader = new byte[2];
  47. var reader = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(new MqttPacketWriter()), null, new MqttNetLogger());
  48. for (var i = 0; i < 10000; i++)
  49. {
  50. channel.Reset();
  51. var header = reader.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult();
  52. }
  53. }
  54. private class BenchmarkMqttChannel : IMqttChannel
  55. {
  56. private readonly ArraySegment<byte> _buffer;
  57. private int _position;
  58. public BenchmarkMqttChannel(ArraySegment<byte> buffer)
  59. {
  60. _buffer = buffer;
  61. _position = _buffer.Offset;
  62. }
  63. public string Endpoint { get; } = string.Empty;
  64. public bool IsSecureConnection { get; } = false;
  65. public X509Certificate2 ClientCertificate { get; }
  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. }