Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

77 wiersze
2.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.IO;
  10. using MQTTnet.Adapter;
  11. using MQTTnet.Core.Internal;
  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. }
  39. }
  40. [Benchmark]
  41. public void Deserialize_10000_Messages()
  42. {
  43. for (var i = 0; i < 10000; i++)
  44. {
  45. using (var headerStream = new MemoryStream(Join(_serializedPacket)))
  46. {
  47. var channel = new TestMqttChannel(headerStream);
  48. var header = MqttPacketReader.ReadFixedHeaderAsync(new TestMqttChannel(headerStream), CancellationToken.None).GetAwaiter().GetResult();
  49. var bodyLength = MqttPacketReader.ReadBodyLengthAsync(channel, CancellationToken.None).GetAwaiter().GetResult();
  50. using (var bodyStream = new MemoryStream(Join(_serializedPacket), (int)headerStream.Position, bodyLength))
  51. {
  52. _serializer.Deserialize(new ReceivedMqttPacket((byte)header, bodyStream));
  53. }
  54. }
  55. }
  56. }
  57. private static byte[] Join(params ArraySegment<byte>[] chunks)
  58. {
  59. var buffer = new MemoryStream();
  60. foreach (var chunk in chunks)
  61. {
  62. buffer.Write(chunk.Array, chunk.Offset, chunk.Count);
  63. }
  64. return buffer.ToArray();
  65. }
  66. }
  67. }