Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

RoundtripTimeTests.cs 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Client;
  7. using MQTTnet.Server;
  8. namespace MQTTnet.Core.Tests
  9. {
  10. [TestClass]
  11. public class RoundtripTimeTests
  12. {
  13. [TestMethod]
  14. public async Task Round_Trip_Time()
  15. {
  16. var factory = new MqttFactory();
  17. var server = factory.CreateMqttServer();
  18. var receiverClient = factory.CreateMqttClient();
  19. var senderClient = factory.CreateMqttClient();
  20. await server.StartAsync(new MqttServerOptionsBuilder().Build());
  21. await receiverClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  22. await receiverClient.SubscribeAsync("#");
  23. await senderClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  24. TaskCompletionSource<string> response = null;
  25. receiverClient.ApplicationMessageReceived += (sender, args) =>
  26. {
  27. response?.SetResult(args.ApplicationMessage.ConvertPayloadToString());
  28. };
  29. var times = new List<TimeSpan>();
  30. var stopwatch = Stopwatch.StartNew();
  31. await Task.Delay(1000);
  32. for (var i = 0; i < 100; i++)
  33. {
  34. response = new TaskCompletionSource<string>();
  35. await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
  36. response.Task.GetAwaiter().GetResult();
  37. stopwatch.Stop();
  38. times.Add(stopwatch.Elapsed);
  39. stopwatch.Restart();
  40. }
  41. await server.StopAsync();
  42. }
  43. }
  44. }