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.

SerializerBenchmark.cs 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Serializer;
  4. using MQTTnet.Internal;
  5. using System;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MQTTnet.Adapter;
  9. using MQTTnet.Channel;
  10. namespace MQTTnet.Benchmarks
  11. {
  12. [ClrJob]
  13. [RPlotExporter]
  14. [MemoryDiagnoser]
  15. public class SerializerBenchmark
  16. {
  17. private MqttBasePacket _packet;
  18. private ArraySegment<byte> _serializedPacket;
  19. private MqttPacketSerializer _serializer;
  20. [GlobalSetup]
  21. public void Setup()
  22. {
  23. var message = new MqttApplicationMessageBuilder()
  24. .WithTopic("A")
  25. .Build();
  26. _packet = message.ToPublishPacket();
  27. _serializer = new MqttPacketSerializer();
  28. _serializedPacket = _serializer.Serialize(_packet);
  29. }
  30. [Benchmark]
  31. public void Serialize_10000_Messages()
  32. {
  33. for (var i = 0; i < 10000; i++)
  34. {
  35. _serializer.Serialize(_packet);
  36. _serializer.FreeBuffer();
  37. }
  38. }
  39. [Benchmark]
  40. public void Deserialize_10000_Messages()
  41. {
  42. var channel = new BenchmarkMqttChannel(_serializedPacket);
  43. var fixedHeader = new byte[2];
  44. var singleByteBuffer = new byte[1];
  45. for (var i = 0; i < 10000; i++)
  46. {
  47. channel.Reset();
  48. var header = MqttPacketReader.ReadFixedHeaderAsync(channel, fixedHeader, singleByteBuffer, CancellationToken.None).GetAwaiter().GetResult();
  49. var receivedPacket = new ReceivedMqttPacket(
  50. header.Flags,
  51. new MqttPacketBodyReader(_serializedPacket.Array, _serializedPacket.Count - header.RemainingLength, _serializedPacket.Array.Length));
  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. }