Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

114 linhas
3.3 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Serializer;
  4. using MQTTnet.Internal;
  5. using BenchmarkDotNet.Attributes.Jobs;
  6. using BenchmarkDotNet.Attributes.Exporters;
  7. using System;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MQTTnet.Adapter;
  11. using MQTTnet.Channel;
  12. namespace MQTTnet.Benchmarks
  13. {
  14. [ClrJob]
  15. [RPlotExporter]
  16. [MemoryDiagnoser]
  17. public class SerializerBenchmark
  18. {
  19. private MqttBasePacket _packet;
  20. private ArraySegment<byte> _serializedPacket;
  21. private MqttPacketSerializer _serializer;
  22. [GlobalSetup]
  23. public void Setup()
  24. {
  25. var message = new MqttApplicationMessageBuilder()
  26. .WithTopic("A")
  27. .Build();
  28. _packet = message.ToPublishPacket();
  29. _serializer = new MqttPacketSerializer();
  30. _serializedPacket = _serializer.Serialize(_packet);
  31. }
  32. [Benchmark]
  33. public void Serialize_10000_Messages()
  34. {
  35. for (var i = 0; i < 10000; i++)
  36. {
  37. _serializer.Serialize(_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 singleByteBuffer = new byte[1];
  47. for (var i = 0; i < 10000; i++)
  48. {
  49. channel.Reset();
  50. var header = MqttPacketReader.ReadFixedHeaderAsync(channel, fixedHeader, singleByteBuffer, CancellationToken.None).GetAwaiter().GetResult();
  51. var receivedPacket = new ReceivedMqttPacket(
  52. header.Flags,
  53. new MqttPacketBodyReader(_serializedPacket.Array, _serializedPacket.Count - header.RemainingLength, _serializedPacket.Array.Length));
  54. _serializer.Deserialize(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; }
  67. public void Reset()
  68. {
  69. _position = _buffer.Offset;
  70. }
  71. public Task ConnectAsync(CancellationToken cancellationToken)
  72. {
  73. throw new NotImplementedException();
  74. }
  75. public Task DisconnectAsync()
  76. {
  77. throw new NotImplementedException();
  78. }
  79. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  80. {
  81. Array.Copy(_buffer.Array, _position, buffer, offset, count);
  82. _position += count;
  83. return Task.FromResult(count);
  84. }
  85. public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. public void Dispose()
  90. {
  91. }
  92. }
  93. }
  94. }