Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

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