您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MessageProcessingBenchmark.cs 1.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Client;
  3. using MQTTnet.Server;
  4. namespace MQTTnet.Benchmarks
  5. {
  6. [ClrJob]
  7. [RPlotExporter, RankColumn]
  8. [MemoryDiagnoser]
  9. public class MessageProcessingBenchmark
  10. {
  11. private IMqttServer _mqttServer;
  12. private IMqttClient _mqttClient;
  13. private MqttApplicationMessage _message;
  14. [GlobalSetup]
  15. public void Setup()
  16. {
  17. var factory = new MqttFactory();
  18. _mqttServer = factory.CreateMqttServer();
  19. _mqttClient = factory.CreateMqttClient();
  20. var serverOptions = new MqttServerOptionsBuilder().Build();
  21. _mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult();
  22. var clientOptions = new MqttClientOptionsBuilder()
  23. .WithTcpServer("localhost").Build();
  24. _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
  25. _message = new MqttApplicationMessageBuilder()
  26. .WithTopic("A")
  27. .Build();
  28. }
  29. [Benchmark]
  30. public void Send_10000_Messages()
  31. {
  32. for (var i = 0; i < 10000; i++)
  33. {
  34. _mqttClient.PublishAsync(_message).GetAwaiter().GetResult();
  35. }
  36. }
  37. }
  38. }