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.
 
 
 
 

352 lines
12 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.Client.Options;
  9. using MQTTnet.Protocol;
  10. using MQTTnet.Server;
  11. namespace MQTTnet.TestApp.NetCore
  12. {
  13. public static class PerformanceTest
  14. {
  15. public static void RunClientOnly()
  16. {
  17. try
  18. {
  19. var options = new MqttClientOptions
  20. {
  21. ChannelOptions = new MqttClientTcpOptions
  22. {
  23. Server = "127.0.0.1"
  24. },
  25. CleanSession = true
  26. };
  27. var client = new MqttFactory().CreateMqttClient();
  28. client.ConnectAsync(options).GetAwaiter().GetResult();
  29. var message = CreateMessage();
  30. var stopwatch = new Stopwatch();
  31. for (var i = 0; i < 10; i++)
  32. {
  33. var sentMessagesCount = 0;
  34. stopwatch.Restart();
  35. while (stopwatch.ElapsedMilliseconds < 1000)
  36. {
  37. client.PublishAsync(message).GetAwaiter().GetResult();
  38. sentMessagesCount++;
  39. }
  40. Console.WriteLine($"Sending {sentMessagesCount} messages per second. #" + (i + 1));
  41. GC.Collect();
  42. }
  43. }
  44. catch (Exception exception)
  45. {
  46. Console.WriteLine(exception);
  47. }
  48. }
  49. public static async Task RunClientAndServer()
  50. {
  51. try
  52. {
  53. var mqttServer = new MqttFactory().CreateMqttServer();
  54. await mqttServer.StartAsync(new MqttServerOptions()).ConfigureAwait(false);
  55. var options = new MqttClientOptions
  56. {
  57. ChannelOptions = new MqttClientTcpOptions
  58. {
  59. Server = "127.0.0.1"
  60. },
  61. CleanSession = true,
  62. //KeepAlivePeriod = TimeSpan.FromSeconds(1)
  63. };
  64. var client = new MqttFactory().CreateMqttClient();
  65. await client.ConnectAsync(options).ConfigureAwait(false);
  66. var message = CreateMessage();
  67. var stopwatch = new Stopwatch();
  68. for (var i = 0; i < 10; i++)
  69. {
  70. stopwatch.Restart();
  71. var sentMessagesCount = 0;
  72. while (stopwatch.ElapsedMilliseconds < 1000)
  73. {
  74. await client.PublishAsync(message, CancellationToken.None).ConfigureAwait(false);
  75. sentMessagesCount++;
  76. }
  77. Console.WriteLine($"Sending {sentMessagesCount} messages per second. #" + (i + 1));
  78. }
  79. }
  80. catch (Exception exception)
  81. {
  82. Console.WriteLine(exception);
  83. }
  84. }
  85. private static Task RunClientsAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
  86. {
  87. return Task.WhenAll(Enumerable.Range(0, 3).Select(i => Task.Run(() => RunClientAsync(msgChunkSize, interval, concurrent))));
  88. }
  89. private static async Task RunClientAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
  90. {
  91. try
  92. {
  93. var options = new MqttClientOptions
  94. {
  95. ChannelOptions = new MqttClientTcpOptions { Server = "localhost" },
  96. ClientId = "Client1",
  97. CleanSession = true,
  98. CommunicationTimeout = TimeSpan.FromMinutes(10)
  99. };
  100. var client = new MqttFactory().CreateMqttClient();
  101. try
  102. {
  103. await client.ConnectAsync(options).ConfigureAwait(false);
  104. }
  105. catch (Exception exception)
  106. {
  107. Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
  108. }
  109. var message = CreateMessage();
  110. var stopwatch = Stopwatch.StartNew();
  111. var testMessageCount = 10000;
  112. for (var i = 0; i < testMessageCount; i++)
  113. {
  114. await client.PublishAsync(message);
  115. }
  116. stopwatch.Stop();
  117. Console.WriteLine($"Sent 10.000 messages within {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / (float)testMessageCount} ms / message).");
  118. var last = DateTime.Now;
  119. var msgCount = 0;
  120. while (true)
  121. {
  122. var msgs = Enumerable.Range(0, msgChunkSize)
  123. .Select(i => CreateMessage())
  124. .ToList();
  125. if (concurrent)
  126. {
  127. //send concurrent (test for raceconditions)
  128. var sendTasks = msgs
  129. .Select(msg => PublishSingleMessage(client, msg, ref msgCount))
  130. .ToList();
  131. await Task.WhenAll(sendTasks);
  132. }
  133. else
  134. {
  135. await client.PublishAsync(msgs);
  136. msgCount += msgs.Count;
  137. //send multiple
  138. }
  139. var now = DateTime.Now;
  140. if (last < now - TimeSpan.FromSeconds(1))
  141. {
  142. Console.WriteLine($"sending {msgCount} intended {msgChunkSize / interval.TotalSeconds}");
  143. msgCount = 0;
  144. last = now;
  145. }
  146. await Task.Delay(interval).ConfigureAwait(false);
  147. }
  148. }
  149. catch (Exception exception)
  150. {
  151. Console.WriteLine(exception);
  152. }
  153. }
  154. private static MqttApplicationMessage CreateMessage()
  155. {
  156. return new MqttApplicationMessage
  157. {
  158. Topic = "A/B/C",
  159. Payload = Encoding.UTF8.GetBytes("Hello World"),
  160. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  161. };
  162. }
  163. private static Task PublishSingleMessage(IMqttClient client, MqttApplicationMessage applicationMessage, ref int count)
  164. {
  165. Interlocked.Increment(ref count);
  166. return Task.Run(() => client.PublishAsync(applicationMessage));
  167. }
  168. public static async Task RunQoS2Test()
  169. {
  170. try
  171. {
  172. var mqttServer = new MqttFactory().CreateMqttServer();
  173. await mqttServer.StartAsync(new MqttServerOptions());
  174. var options = new MqttClientOptions
  175. {
  176. ChannelOptions = new MqttClientTcpOptions
  177. {
  178. Server = "127.0.0.1"
  179. },
  180. CleanSession = true
  181. };
  182. var client = new MqttFactory().CreateMqttClient();
  183. await client.ConnectAsync(options);
  184. var message = new MqttApplicationMessage
  185. {
  186. Topic = "A/B/C",
  187. Payload = Encoding.UTF8.GetBytes("Hello World"),
  188. QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
  189. };
  190. var stopwatch = new Stopwatch();
  191. var iteration = 1;
  192. while (true)
  193. {
  194. stopwatch.Restart();
  195. var sentMessagesCount = 0;
  196. while (stopwatch.ElapsedMilliseconds < 1000)
  197. {
  198. await client.PublishAsync(message).ConfigureAwait(false);
  199. sentMessagesCount++;
  200. }
  201. Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
  202. iteration++;
  203. }
  204. }
  205. catch (Exception exception)
  206. {
  207. Console.WriteLine(exception);
  208. }
  209. }
  210. public static async Task RunQoS1Test()
  211. {
  212. try
  213. {
  214. var mqttServer = new MqttFactory().CreateMqttServer();
  215. await mqttServer.StartAsync(new MqttServerOptions());
  216. var options = new MqttClientOptions
  217. {
  218. ChannelOptions = new MqttClientTcpOptions
  219. {
  220. Server = "127.0.0.1"
  221. },
  222. CleanSession = true
  223. };
  224. var client = new MqttFactory().CreateMqttClient();
  225. await client.ConnectAsync(options);
  226. var message = new MqttApplicationMessage
  227. {
  228. Topic = "A/B/C",
  229. Payload = Encoding.UTF8.GetBytes("Hello World"),
  230. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
  231. };
  232. var stopwatch = new Stopwatch();
  233. var iteration = 1;
  234. while (true)
  235. {
  236. stopwatch.Restart();
  237. var sentMessagesCount = 0;
  238. while (stopwatch.ElapsedMilliseconds < 1000)
  239. {
  240. await client.PublishAsync(message).ConfigureAwait(false);
  241. sentMessagesCount++;
  242. }
  243. Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
  244. iteration++;
  245. }
  246. }
  247. catch (Exception exception)
  248. {
  249. Console.WriteLine(exception);
  250. }
  251. }
  252. public static async Task RunQoS0Test()
  253. {
  254. try
  255. {
  256. var mqttServer = new MqttFactory().CreateMqttServer();
  257. await mqttServer.StartAsync(new MqttServerOptions());
  258. var options = new MqttClientOptions
  259. {
  260. ChannelOptions = new MqttClientTcpOptions
  261. {
  262. Server = "127.0.0.1"
  263. },
  264. CleanSession = true
  265. };
  266. var client = new MqttFactory().CreateMqttClient();
  267. await client.ConnectAsync(options);
  268. var message = new MqttApplicationMessage
  269. {
  270. Topic = "A/B/C",
  271. Payload = Encoding.UTF8.GetBytes("Hello World"),
  272. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  273. };
  274. var stopwatch = new Stopwatch();
  275. var iteration = 1;
  276. while (true)
  277. {
  278. stopwatch.Restart();
  279. var sentMessagesCount = 0;
  280. while (stopwatch.ElapsedMilliseconds < 1000)
  281. {
  282. await client.PublishAsync(message).ConfigureAwait(false);
  283. sentMessagesCount++;
  284. }
  285. Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
  286. iteration++;
  287. }
  288. }
  289. catch (Exception exception)
  290. {
  291. Console.WriteLine(exception);
  292. }
  293. }
  294. }
  295. }