Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

62 рядки
2.0 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Jobs;
  3. using MQTTnet.Client;
  4. using MQTTnet.Server;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace MQTTnet.Benchmarks
  8. {
  9. [MemoryDiagnoser]
  10. public class UnsubscribeBenchmark
  11. {
  12. MqttServer _mqttServer;
  13. MQTTnet.Client.MqttClient _mqttClient;
  14. const int NumPublishers = 1;
  15. const int NumTopicsPerPublisher = 10000;
  16. List<string> _topics;
  17. [GlobalSetup]
  18. public void Setup()
  19. {
  20. TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out var topicsByPublisher, out var singleWildcardTopicsByPublisher, out var multiWildcardTopicsByPublisher);
  21. _topics = topicsByPublisher.Values.First();
  22. var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  23. var factory = new MqttFactory();
  24. _mqttServer = factory.CreateMqttServer(serverOptions);
  25. _mqttClient = factory.CreateMqttClient();
  26. _mqttServer.StartAsync().GetAwaiter().GetResult();
  27. var clientOptions = new MqttClientOptionsBuilder()
  28. .WithTcpServer("localhost").Build();
  29. _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
  30. foreach (var topic in _topics)
  31. {
  32. var subscribeOptions = new MqttClientSubscribeOptionsBuilder()
  33. .WithTopicFilter(topic, Protocol.MqttQualityOfServiceLevel.AtMostOnce)
  34. .Build();
  35. _mqttClient.SubscribeAsync(subscribeOptions).GetAwaiter().GetResult();
  36. }
  37. }
  38. [Benchmark]
  39. public void Unsubscribe_10000_Topics()
  40. {
  41. foreach (var topic in _topics)
  42. {
  43. var unsubscribeOptions = new MqttClientUnsubscribeOptionsBuilder()
  44. .WithTopicFilter(topic)
  45. .Build();
  46. _mqttClient.UnsubscribeAsync(unsubscribeOptions).GetAwaiter().GetResult();
  47. }
  48. }
  49. }
  50. }