Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RoundtripTimeTests.cs 1.7 KiB

6 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  41. }
  42. }