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.5 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using BenchmarkDotNet.Attributes;
  5. using MQTTnet.Packets;
  6. using System;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MQTTnet.Adapter;
  11. using MQTTnet.Channel;
  12. using MQTTnet.Formatter;
  13. using MQTTnet.Formatter.V3;
  14. using BenchmarkDotNet.Jobs;
  15. using MQTTnet.Diagnostics;
  16. namespace MQTTnet.Benchmarks
  17. {
  18. [SimpleJob(RuntimeMoniker.NetCoreApp50)]
  19. [RPlotExporter]
  20. [MemoryDiagnoser]
  21. public class SerializerBenchmark : BaseBenchmark
  22. {
  23. MqttPacket _packet;
  24. ArraySegment<byte> _serializedPacket;
  25. IMqttPacketFormatter _serializer;
  26. MqttBufferWriter _bufferWriter;
  27. [GlobalSetup]
  28. public void GlobalSetup()
  29. {
  30. _packet = new MqttPublishPacket
  31. {
  32. Topic = "A"
  33. };
  34. _bufferWriter = new MqttBufferWriter(4096, 65535);
  35. _serializer = new MqttV3PacketFormatter(_bufferWriter, MqttProtocolVersion.V311);
  36. _serializedPacket = _serializer.Encode(_packet).Join();
  37. }
  38. [Benchmark]
  39. public void Serialize_10000_Messages()
  40. {
  41. for (var i = 0; i < 10000; i++)
  42. {
  43. _serializer.Encode(_packet);
  44. _bufferWriter.Cleanup();
  45. }
  46. }
  47. [Benchmark]
  48. public void Deserialize_10000_Messages()
  49. {
  50. var channel = new BenchmarkMqttChannel(_serializedPacket);
  51. var reader = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(new MqttBufferWriter(4096, 65535)), null, new MqttNetEventLogger());
  52. for (var i = 0; i < 10000; i++)
  53. {
  54. channel.Reset();
  55. var header = reader.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult();
  56. }
  57. }
  58. class BenchmarkMqttChannel : IMqttChannel
  59. {
  60. readonly ArraySegment<byte> _buffer;
  61. int _position;
  62. public BenchmarkMqttChannel(ArraySegment<byte> buffer)
  63. {
  64. _buffer = buffer;
  65. _position = _buffer.Offset;
  66. }
  67. public string Endpoint { get; } = string.Empty;
  68. public bool IsSecureConnection { get; } = false;
  69. public X509Certificate2 ClientCertificate { get; }
  70. public void Reset()
  71. {
  72. _position = _buffer.Offset;
  73. }
  74. public Task ConnectAsync(CancellationToken cancellationToken)
  75. {
  76. throw new NotSupportedException();
  77. }
  78. public Task DisconnectAsync(CancellationToken cancellationToken)
  79. {
  80. throw new NotSupportedException();
  81. }
  82. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  83. {
  84. Array.Copy(_buffer.Array, _position, buffer, offset, count);
  85. _position += count;
  86. return Task.FromResult(count);
  87. }
  88. public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  89. {
  90. throw new NotSupportedException();
  91. }
  92. public void Dispose()
  93. {
  94. }
  95. }
  96. }
  97. }