You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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