選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

235 行
8.9 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Jobs;
  3. using MQTTnet.Client;
  4. using MQTTnet.Server;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.Benchmarks
  11. {
  12. /// <summary>
  13. /// Create a number of topics, publish, subscribe, and wait for response
  14. /// </summary>
  15. [MemoryDiagnoser]
  16. public class MessageDeliveryBenchmark
  17. {
  18. List<MqttApplicationMessage> _topicPublishMessages;
  19. [Params(1, 5)]
  20. public int NumTopicsPerPublisher;
  21. [Params(1000, 10000)]
  22. public int NumPublishers;
  23. [Params(10)]
  24. public int NumSubscribers;
  25. [Params(5, 10, 20, 50)]
  26. public int NumSubscribedTopicsPerSubscriber;
  27. object _lockMsgCount;
  28. int _messagesReceivedCount;
  29. int _messagesExpectedCount;
  30. CancellationTokenSource _cancellationTokenSource;
  31. MqttServer _mqttServer;
  32. List<MQTTnet.Client.MqttClient> _mqttSubscriberClients;
  33. Dictionary<string, MQTTnet.Client.MqttClient> _mqttPublisherClientsByPublisherName;
  34. Dictionary<string, List<string>> _topicsByPublisher;
  35. Dictionary<string, string> _publisherByTopic;
  36. List<string> _allSubscribedTopics; // Keep track of the subset of topics that are subscribed
  37. [GlobalSetup]
  38. public void Setup()
  39. {
  40. _lockMsgCount = new object();
  41. Dictionary<string, List<string>> singleWildcardTopicsByPublisher;
  42. Dictionary<string, List<string>> multiWildcardTopicsByPublisher;
  43. TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out _topicsByPublisher, out singleWildcardTopicsByPublisher, out multiWildcardTopicsByPublisher);
  44. var topics = _topicsByPublisher.First().Value;
  45. _topicPublishMessages = new List<MqttApplicationMessage>();
  46. // Prepare messages, same for each publisher
  47. foreach (var topic in topics)
  48. {
  49. var message = new MqttApplicationMessageBuilder()
  50. .WithTopic(topic)
  51. .WithPayload(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 })
  52. .Build();
  53. _topicPublishMessages.Add(message);
  54. }
  55. // Create server
  56. var factory = new MqttFactory();
  57. var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  58. _mqttServer = factory.CreateMqttServer(serverOptions);
  59. _mqttServer.StartAsync().GetAwaiter().GetResult();
  60. // Create publisher clients
  61. _mqttPublisherClientsByPublisherName = new Dictionary<string, MQTTnet.Client.MqttClient>();
  62. foreach (var pt in _topicsByPublisher)
  63. {
  64. var publisherName = pt.Key;
  65. var mqttClient = factory.CreateMqttClient();
  66. var publisherOptions = new MqttClientOptionsBuilder()
  67. .WithTcpServer("localhost")
  68. .WithClientId(publisherName)
  69. .WithKeepAlivePeriod(TimeSpan.FromSeconds(30))
  70. .Build();
  71. mqttClient.ConnectAsync(publisherOptions).GetAwaiter().GetResult();
  72. _mqttPublisherClientsByPublisherName.Add(publisherName, mqttClient);
  73. }
  74. // Create subscriber clients
  75. _mqttSubscriberClients = new List<MQTTnet.Client.MqttClient>();
  76. for (var i = 0; i < NumSubscribers; i++)
  77. {
  78. var mqttSubscriberClient = factory.CreateMqttClient();
  79. _mqttSubscriberClients.Add(mqttSubscriberClient);
  80. var subscriberOptions = new MqttClientOptionsBuilder()
  81. .WithTcpServer("localhost")
  82. .WithClientId("subscriber" + i)
  83. .Build();
  84. mqttSubscriberClient.ApplicationMessageReceivedAsync += r =>
  85. {
  86. // count messages and signal cancellation when expected message count is reached
  87. lock (_lockMsgCount)
  88. {
  89. ++_messagesReceivedCount;
  90. if (_messagesReceivedCount == _messagesExpectedCount)
  91. {
  92. _cancellationTokenSource.Cancel();
  93. }
  94. }
  95. return Task.CompletedTask;
  96. };
  97. mqttSubscriberClient.ConnectAsync(subscriberOptions).GetAwaiter().GetResult();
  98. }
  99. List<string> allTopics = new List<string>();
  100. _publisherByTopic = new Dictionary<string, string>();
  101. foreach (var t in _topicsByPublisher)
  102. {
  103. foreach (var topic in t.Value)
  104. {
  105. _publisherByTopic.Add(topic, t.Key);
  106. allTopics.Add(topic);
  107. }
  108. }
  109. // Subscribe to NumSubscribedTopics topics spread across all topics
  110. _allSubscribedTopics = new List<string>();
  111. var totalNumTopics = NumPublishers * NumTopicsPerPublisher;
  112. int topicIndexStep = totalNumTopics / (NumSubscribedTopicsPerSubscriber * NumSubscribers);
  113. if (topicIndexStep * NumSubscribedTopicsPerSubscriber * NumSubscribers != totalNumTopics)
  114. {
  115. throw new System.Exception(
  116. String.Format("The total number of topics must be divisible by the number of subscribed topics across all subscribers. Total number of topics: {0}, topic step: {1}",
  117. totalNumTopics, topicIndexStep
  118. ));
  119. }
  120. var topicIndex = 0;
  121. foreach (var mqttSubscriber in _mqttSubscriberClients)
  122. {
  123. for (var i = 0; i < NumSubscribedTopicsPerSubscriber; ++i, topicIndex += topicIndexStep)
  124. {
  125. var topic = allTopics[topicIndex];
  126. _allSubscribedTopics.Add(topic);
  127. var subOptions = new Client.MqttClientSubscribeOptionsBuilder().WithTopicFilter(
  128. new Packets.MqttTopicFilter() { Topic = topic })
  129. .Build();
  130. mqttSubscriber.SubscribeAsync(subOptions).GetAwaiter().GetResult();
  131. }
  132. }
  133. Task.Delay(1000).GetAwaiter().GetResult();
  134. }
  135. /// <summary>
  136. /// Publish messages and wait for messages sent to subscribers
  137. /// </summary>
  138. [Benchmark]
  139. public void DeliverMessages()
  140. {
  141. // There should be one message received per publish for each subscribed topic
  142. _messagesExpectedCount = NumSubscribedTopicsPerSubscriber * NumSubscribers;
  143. // Loop for a while and exchange messages
  144. _messagesReceivedCount = 0;
  145. _cancellationTokenSource = new CancellationTokenSource();
  146. // same payload for all messages
  147. var payload = new byte[] { 1, 2, 3, 4 };
  148. var tasks = new List<Task>();
  149. // publish a message for each subscribed topic
  150. foreach (var topic in _allSubscribedTopics)
  151. {
  152. var message = new MqttApplicationMessageBuilder()
  153. .WithTopic(topic)
  154. .WithPayload(payload)
  155. .Build();
  156. // pick the correct publisher
  157. var publisherName = _publisherByTopic[topic];
  158. var publisherClient = _mqttPublisherClientsByPublisherName[publisherName];
  159. _ = publisherClient.PublishAsync(message);
  160. }
  161. // Wait one message per publish to be received by subscriber (in the subscriber's application message handler)
  162. try
  163. {
  164. Task.Delay(30000, _cancellationTokenSource.Token).GetAwaiter().GetResult();
  165. }
  166. catch
  167. {
  168. }
  169. _cancellationTokenSource.Dispose();
  170. if (_messagesReceivedCount < _messagesExpectedCount)
  171. {
  172. throw new Exception(string.Format("Messages Received Count mismatch, expected {0}, received {1}", _messagesExpectedCount, _messagesReceivedCount));
  173. }
  174. }
  175. [GlobalCleanup]
  176. public void Cleanup()
  177. {
  178. foreach (var mp in _mqttPublisherClientsByPublisherName)
  179. {
  180. var mqttPublisherClient = mp.Value;
  181. mqttPublisherClient.DisconnectAsync().GetAwaiter().GetResult();
  182. mqttPublisherClient.Dispose();
  183. }
  184. _mqttPublisherClientsByPublisherName.Clear();
  185. foreach (var mqttSubscriber in _mqttSubscriberClients)
  186. {
  187. mqttSubscriber.DisconnectAsync().GetAwaiter().GetResult();
  188. mqttSubscriber.Dispose();
  189. }
  190. _mqttSubscriberClients.Clear();
  191. _mqttServer.StopAsync().GetAwaiter().GetResult();
  192. _mqttServer.Dispose();
  193. _mqttServer = null;
  194. }
  195. }
  196. }