Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

6 роки тому
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 MqttPacketSerializer();
  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.SendPacketsAsync(TimeSpan.FromSeconds(15), new[] { _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. }