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.
 
 
 
 

75 lignes
2.1 KiB

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