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.
 
 
 
 

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