Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

87 řádky
2.2 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.Diagnostics;
  4. using MQTTnet.Internal;
  5. using MQTTnet.Packets;
  6. using System;
  7. using System.IO;
  8. using System.Threading;
  9. using MQTTnet.Formatter;
  10. namespace MQTTnet.Benchmarks
  11. {
  12. [MemoryDiagnoser]
  13. public class ChannelAdapterBenchmark
  14. {
  15. private MqttChannelAdapter _channelAdapter;
  16. private int _iterations;
  17. private MemoryStream _stream;
  18. private MqttPublishPacket _packet;
  19. [GlobalSetup]
  20. public void Setup()
  21. {
  22. _packet = new MqttPublishPacket
  23. {
  24. Topic = "A"
  25. };
  26. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  27. var serializedPacket = Join(serializer.Encode(_packet));
  28. _iterations = 10000;
  29. _stream = new MemoryStream(_iterations * serializedPacket.Length);
  30. for (var i = 0; i < _iterations; i++)
  31. {
  32. _stream.Write(serializedPacket, 0, serializedPacket.Length);
  33. }
  34. _stream.Position = 0;
  35. var channel = new TestMqttChannel(_stream);
  36. _channelAdapter = new MqttChannelAdapter(channel, serializer, null, new MqttNetLogger());
  37. }
  38. [Benchmark]
  39. public void Receive_10000_Messages()
  40. {
  41. _stream.Position = 0;
  42. for (var i = 0; i < 10000; i++)
  43. {
  44. _channelAdapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult();
  45. }
  46. _stream.Position = 0;
  47. }
  48. [Benchmark]
  49. public void Send_10000_Messages()
  50. {
  51. _stream.Position = 0;
  52. for (var i = 0; i < 10000; i++)
  53. {
  54. _channelAdapter.SendPacketAsync(_packet, CancellationToken.None).GetAwaiter().GetResult();
  55. }
  56. _stream.Position = 0;
  57. }
  58. private static byte[] Join(params ArraySegment<byte>[] chunks)
  59. {
  60. var buffer = new MemoryStream();
  61. foreach (var chunk in chunks)
  62. {
  63. buffer.Write(chunk.Array, chunk.Offset, chunk.Count);
  64. }
  65. return buffer.ToArray();
  66. }
  67. }
  68. }