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.
 
 
 
 

54 lignes
1.7 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.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. for (var i = 0; i < 100; i++)
  32. {
  33. response = new TaskCompletionSource<string>();
  34. await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
  35. response.Task.GetAwaiter().GetResult();
  36. stopwatch.Stop();
  37. times.Add(stopwatch.Elapsed);
  38. stopwatch.Restart();
  39. }
  40. await server.StopAsync();
  41. }
  42. }
  43. }