Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

71 linhas
2.1 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Client;
  3. using Microsoft.AspNetCore;
  4. using Microsoft.AspNetCore.Hosting;
  5. using MQTTnet.AspNetCore.Client;
  6. using MQTTnet.AspNetCore.Extensions;
  7. using MQTTnet.Client.Options;
  8. using MQTTnet.Diagnostics.Logger;
  9. namespace MQTTnet.Benchmarks
  10. {
  11. [MemoryDiagnoser]
  12. public class MessageProcessingMqttConnectionContextBenchmark
  13. {
  14. IWebHost _host;
  15. IMqttClient _mqttClient;
  16. MqttApplicationMessage _message;
  17. [GlobalSetup]
  18. public void Setup()
  19. {
  20. _host = WebHost.CreateDefaultBuilder()
  21. .UseKestrel(o => o.ListenAnyIP(1883, l => l.UseMqtt()))
  22. .ConfigureServices(services => {
  23. services
  24. .AddHostedMqttServer(mqttServerOptions => mqttServerOptions.WithoutDefaultEndpoint())
  25. .AddMqttConnectionHandler();
  26. })
  27. .Configure(app => {
  28. app.UseMqttServer(s => {
  29. });
  30. })
  31. .Build();
  32. var factory = new MqttFactory();
  33. _mqttClient = factory.CreateMqttClient(new MqttNetEventLogger(), new MqttClientConnectionContextFactory());
  34. _host.StartAsync().GetAwaiter().GetResult();
  35. var clientOptions = new MqttClientOptionsBuilder()
  36. .WithTcpServer("localhost").Build();
  37. _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
  38. _message = new MqttApplicationMessageBuilder()
  39. .WithTopic("A")
  40. .Build();
  41. }
  42. [GlobalCleanup]
  43. public void Cleanup()
  44. {
  45. _mqttClient.DisconnectAsync().GetAwaiter().GetResult();
  46. _mqttClient.Dispose();
  47. _host.StopAsync().GetAwaiter().GetResult();
  48. _host.Dispose();
  49. }
  50. [Benchmark]
  51. public void Send_10000_Messages()
  52. {
  53. for (var i = 0; i < 10000; i++)
  54. {
  55. _mqttClient.PublishAsync(_message).GetAwaiter().GetResult();
  56. }
  57. }
  58. }
  59. }