Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChannelAdapterBenchmark.cs 2.3 KiB

6 лет назад
6 лет назад
6 лет назад
6 лет назад
6 лет назад
6 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, 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, TimeSpan.Zero, 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. }