Você não pode selecionar mais de 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.
 
 
 
 

112 linhas
3.1 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. for (var i = 0; i < 10000; i++)
  46. {
  47. channel.Reset();
  48. var header = MqttPacketReader.ReadFixedHeaderAsync(channel, CancellationToken.None).GetAwaiter().GetResult();
  49. var receivedPacket = new ReceivedMqttPacket(
  50. header.Flags,
  51. new MqttPacketBodyReader(_serializedPacket.Array, _serializedPacket.Count - header.RemainingLength));
  52. _serializer.Deserialize(receivedPacket);
  53. }
  54. }
  55. private class BenchmarkMqttChannel : IMqttChannel
  56. {
  57. private readonly ArraySegment<byte> _buffer;
  58. private int _position;
  59. public BenchmarkMqttChannel(ArraySegment<byte> buffer)
  60. {
  61. _buffer = buffer;
  62. _position = _buffer.Offset;
  63. }
  64. public string Endpoint { get; }
  65. public void Reset()
  66. {
  67. _position = _buffer.Offset;
  68. }
  69. public Task ConnectAsync(CancellationToken cancellationToken)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. public Task DisconnectAsync()
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  78. {
  79. Array.Copy(_buffer.Array, _position, buffer, offset, count);
  80. _position += count;
  81. return Task.FromResult(count);
  82. }
  83. public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  84. {
  85. throw new NotImplementedException();
  86. }
  87. public void Dispose()
  88. {
  89. }
  90. }
  91. }
  92. }