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.

RoundtripTimeTests.cs 1.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }