Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

87 Zeilen
2.4 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.Diagnostics;
  4. using MQTTnet.Internal;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Serializer;
  7. using System;
  8. using System.IO;
  9. using System.Threading;
  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. var message = new MqttApplicationMessageBuilder()
  23. .WithTopic("A")
  24. .Build();
  25. _packet = message.ToPublishPacket();
  26. var serializer = new MqttPacketSerializerAdapter(MqttProtocolVersion.V311);
  27. var serializedPacket = Join(serializer.Serialize(_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, new MqttNetLogger().CreateChildLogger(nameof(MqttChannelAdapter)));
  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(TimeSpan.Zero, 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. }