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.
 
 
 
 

57 rivejä
1.8 KiB

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