Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

197 lignes
6.5 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Client;
  8. using MQTTnet.Protocol;
  9. using MQTTnet.Server;
  10. namespace MQTTnet.TestApp.NetCore
  11. {
  12. public static class PerformanceTest
  13. {
  14. public static void RunClientOnly()
  15. {
  16. try
  17. {
  18. var options = new MqttClientOptions
  19. {
  20. ChannelOptions = new MqttClientTcpOptions
  21. {
  22. Server = "127.0.0.1"
  23. },
  24. CleanSession = true
  25. };
  26. var client = new MqttFactory().CreateMqttClient();
  27. client.ConnectAsync(options).GetAwaiter().GetResult();
  28. var message = CreateMessage();
  29. var stopwatch = new Stopwatch();
  30. for (var i = 0; i < 10; i++)
  31. {
  32. var sentMessagesCount = 0;
  33. stopwatch.Restart();
  34. while (stopwatch.ElapsedMilliseconds < 1000)
  35. {
  36. client.PublishAsync(message).GetAwaiter().GetResult();
  37. sentMessagesCount++;
  38. }
  39. Console.WriteLine($"Sending {sentMessagesCount} messages per second. #" + (i + 1));
  40. GC.Collect();
  41. }
  42. }
  43. catch (Exception exception)
  44. {
  45. Console.WriteLine(exception);
  46. }
  47. }
  48. public static void RunClientAndServer()
  49. {
  50. try
  51. {
  52. var mqttServer = new MqttFactory().CreateMqttServer();
  53. mqttServer.StartAsync(new MqttServerOptions()).GetAwaiter().GetResult();
  54. var options = new MqttClientOptions
  55. {
  56. ChannelOptions = new MqttClientTcpOptions
  57. {
  58. Server = "127.0.0.1"
  59. },
  60. CleanSession = true
  61. };
  62. var client = new MqttFactory().CreateMqttClient();
  63. client.ConnectAsync(options).GetAwaiter().GetResult();
  64. var message = CreateMessage();
  65. var stopwatch = new Stopwatch();
  66. for (var i = 0; i < 10; i++)
  67. {
  68. stopwatch.Restart();
  69. var sentMessagesCount = 0;
  70. while (stopwatch.ElapsedMilliseconds < 1000)
  71. {
  72. client.PublishAsync(message).GetAwaiter().GetResult();
  73. sentMessagesCount++;
  74. }
  75. Console.WriteLine($"Sending {sentMessagesCount} messages per second. #" + (i + 1));
  76. }
  77. }
  78. catch (Exception exception)
  79. {
  80. Console.WriteLine(exception);
  81. }
  82. }
  83. private static Task RunClientsAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
  84. {
  85. return Task.WhenAll(Enumerable.Range(0, 3).Select(i => Task.Run(() => RunClientAsync(msgChunkSize, interval, concurrent))));
  86. }
  87. private static async Task RunClientAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
  88. {
  89. try
  90. {
  91. var options = new MqttClientOptions
  92. {
  93. ChannelOptions = new MqttClientTcpOptions { Server = "localhost" },
  94. ClientId = "Client1",
  95. CleanSession = true,
  96. CommunicationTimeout = TimeSpan.FromMinutes(10)
  97. };
  98. var client = new MqttFactory().CreateMqttClient();
  99. try
  100. {
  101. await client.ConnectAsync(options).ConfigureAwait(false);
  102. }
  103. catch (Exception exception)
  104. {
  105. Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
  106. }
  107. var message = CreateMessage();
  108. var stopwatch = Stopwatch.StartNew();
  109. var testMessageCount = 10000;
  110. for (var i = 0; i < testMessageCount; i++)
  111. {
  112. await client.PublishAsync(message);
  113. }
  114. stopwatch.Stop();
  115. Console.WriteLine($"Sent 10.000 messages within {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / (float)testMessageCount} ms / message).");
  116. var last = DateTime.Now;
  117. var msgCount = 0;
  118. while (true)
  119. {
  120. var msgs = Enumerable.Range(0, msgChunkSize)
  121. .Select(i => CreateMessage())
  122. .ToList();
  123. if (concurrent)
  124. {
  125. //send concurrent (test for raceconditions)
  126. var sendTasks = msgs
  127. .Select(msg => PublishSingleMessage(client, msg, ref msgCount))
  128. .ToList();
  129. await Task.WhenAll(sendTasks);
  130. }
  131. else
  132. {
  133. await client.PublishAsync(msgs);
  134. msgCount += msgs.Count;
  135. //send multiple
  136. }
  137. var now = DateTime.Now;
  138. if (last < now - TimeSpan.FromSeconds(1))
  139. {
  140. Console.WriteLine($"sending {msgCount} intended {msgChunkSize / interval.TotalSeconds}");
  141. msgCount = 0;
  142. last = now;
  143. }
  144. await Task.Delay(interval).ConfigureAwait(false);
  145. }
  146. }
  147. catch (Exception exception)
  148. {
  149. Console.WriteLine(exception);
  150. }
  151. }
  152. private static MqttApplicationMessage CreateMessage()
  153. {
  154. return new MqttApplicationMessage
  155. {
  156. Topic = "A/B/C",
  157. Payload = Encoding.UTF8.GetBytes("Hello World"),
  158. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  159. };
  160. }
  161. private static Task PublishSingleMessage(IMqttClient client, MqttApplicationMessage applicationMessage, ref int count)
  162. {
  163. Interlocked.Increment(ref count);
  164. return Task.Run(() => client.PublishAsync(applicationMessage));
  165. }
  166. }
  167. }