You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PerformanceTest.cs 6.5 KiB

7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. }